Array / Linked list

์ตœ์ œ์›ยท2023๋…„ 1์›” 23์ผ
0

์ž๋ฃŒ๊ตฌ์กฐ

๋ชฉ๋ก ๋ณด๊ธฐ
1/4
post-thumbnail

๐Ÿ’ก Array vs Liked list

Array๋Š” ๋ฉ”๋ชจ๋ฆฌ ์ƒ์—์„œ ์—ฐ์†์ ์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ์ž๋ฃŒ๊ตฌ์กฐ์ด๋‹ค

Linked List๋Š” ๋ฉ”๋ชจ๋ฆฌ์ƒ์—์„œ๋Š” ์—ฐ์†์ ์ด์ง€ ์•Š์ง€๋งŒ, ๋…ผ๋ฆฌ์  ์—ฐ์†์„ฑ์„ ์œ ์ง€ํ•œ๋‹ค

๊ทธ๋ž˜์„œ ๊ฐ operation์˜ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ๋‹ค๋ฅด๋‹ค.

operationArray time complexityLinked List time complexity
accessO(1)O(n)
searchO(1)O(n)
insertionO(n)O(1)
deletionO(n)O(1)

์œ ๋ฆฌํ•œ ์‚ฌ์šฉ๋ฐฉ๋ฒ•
Array - ์–ผ๋งˆ๋งŒํผ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ• ์ง€ ๋ฏธ๋ฆฌ ์•Œ๊ณ ์žˆ๊ณ , ์กฐํšŒ๊ฐ€ ์žฆ๋‹ค, ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ ๊ฒŒ ์‚ฌ์šฉํ•˜๋Š”๊ฒŒ ์ค‘์š”ํ•˜๋‹ค

Linked list - ๋ช‡๊ฐœ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•  ์ง€ ๋ถˆํ™•์‹คํ•˜๊ณ  ์‚ฝ์ž… ์‚ญ์ œ๊ฐ€ ์žฆ๋‹ค


Array vs Linked list

๐Ÿ’ก ์กฐํšŒ(lookup)

Array๋Š” ๋ฉ”๋ชจ๋ฆฌ์ƒ์— ์—ฐ์†์ ์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ธฐ ๋•Œ๋ฌธ์—

์ €์žฅ๋œ ๋ฐ์ดํ„ฐ์— ์ฆ‰์‹œ์ ‘๊ทผ(radom access O(1))ํ•  ์ˆ˜ ์žˆ์Œ

๋ฐ˜๋ฉด์— Linked List๋Š” ๋ฉ”๋ชจ๋ฆฌ์— ๋ถˆ์—ฐ์†์ ์ธ ๋ฐ์ดํ„ฐ ์ €์žฅ ๋ฐฉ๋ฒ•์„ ํƒํ•˜์—ฌ

์ˆœ์ฐจ ์ ‘๊ทผ(sequetial Access)๋งŒ์ด ๊ฐ€๋Šฅ, ์ฆ‰ ํŠน์ • index์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•˜๊ธฐ ์œ„ํ•ด O(n)์˜ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฌ๊ฒŒ ๋จ

๐Ÿ’ก ์‚ฝ์ž…/์‚ญ์ œ(insert/delete)

Array์˜ ๊ฒฝ์šฐ ๋งจ ๋งˆ์ง€๋ง‰ ์›์†Œ๋ฅผ ์ถ”๊ฐ€/์‚ญ์ œํ•˜๋ฉด ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ O(1)์ด๋‹ค

ํ•˜์ง€๋งŒ ๋งจ ๋งˆ์ง€๋ง‰ ์›์†Œ๊ฐ€ ์•„๋‹Œ ์ค‘๊ฐ„์— ์žˆ๋Š” ์›์†Œ๋ฅผ ์‚ฝ์ž…/์‚ญ์ œํ•˜๋ฉด

ํ•ด๋‹น ์›์†Œ๋ณด๋‹ค ํฐ ์ธ๋ฑ์Šค์˜ ์›์†Œ๋“ค์„ ํ•œ ์นธ์”ฉ shift ํ•ด์ค˜์•ผ ํ•˜๋Š” ๋น„์šฉ์ด ๋ฐœ์ƒํ•จ

๋”ฐ๋ผ์„œ ์ด ๊ฒฝ์šฐ์—๋Š” ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ O(n)์ด ๋จ

Linked List์˜ ๊ฒฝ์šฐ ์–ด๋Š ์›์†Œ๋ฅผ ์ถ”๊ฐ€/์‚ญ์ œ ํ•˜๋”๋ผ๋„

node์— Next Address์˜ ์ˆ˜์ •๋งŒ์ด ์ผ์–ด๋‚˜๊ธฐ ๋•Œ๋ฌธ์— shiftํ•  ํ•„์š” ์—†์ด O(1)์ž„

ํ•˜์ง€๋งŒ Linked list์˜ ๊ฒฝ์šฐ ์ถ”๊ฐ€/์‚ญ์ œ๋ฅผ ํ•˜๋ ค๋Š” index๊นŒ์ง€ ๋„๋‹ฌํ•˜๋Š”๋ฐ

O(n)์˜ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์— ์‹ค์ง์ ์œผ๋กœ Linked List๋„ ์ถ”๊ฐ€/์‚ญ์ œ ์‹œ์—

O(n)์˜ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฐ๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Œ

Memory

Array์˜ ์ฃผ๋œ ์žฅ์ ์€ ๋ฐ์ดํ„ฐ ์ ‘๊ทผ๊ณผ append๊ฐ€ ๋น ๋ฅด๋‹ค๋Š” ๊ฒƒ

ํ•˜์ง€๋งŒ ๋ฉ”๋ชจ๋ฆฌ ๋‚ญ๋น„๋ผ๋Š” ๋‹จ์ ์ด ์žˆ๋‹ค

๋ฐฐ์—ด์€ ์„ ์–ธ์‹œ์— fixed size๋ฅผ ์„ค์ •ํ•˜์—ฌ ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น์„ ํ•ด์คŒ

์ฆ‰ ๋ฐ์ดํ„ฐ๊ฐ€ ์ €์žฅ๋˜์ง€ ์•Š๋”๋ผ๋„ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์„ ์ ํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค

์ด์™€ ๋ฐ˜๋ฉด์— Linked List๋Š” runtime์ค‘์—๋„ size๋ฅผ ๋Š˜๋ฆฌ๊ณ  ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค

๋”ฐ๋ผ์„œ initial size๋ฅผ ๊ณ ๋ฏผํ•  ํ•„์š” ์—†๊ณ ,

ํ•„์š”ํ•œ ๋งŒํผ memorry allocation์„ ํ•˜์—ฌ ๋ฉ”๋ชจ๋ฆฌ ๋‚ญ๋น„๊ฐ€ ์—†๋‹ค

๋ฐ˜๋ฉด์— Linked List๋Š” value์™ธ์— Next Address๋ฅผ ์ถ”๊ฐ€๋กœ ์ €์žฅํ•˜๊ธฐ ๋•Œ๋ฌธ์—

๋ฐ์ดํ„ฐ ๊ฐ’์ด Array๋ณด๋‹ค ๋ฌด๊ฒ๋‹ค

memory allocation

  • Array

compile ๋‹จ๊ณ„์—์„œ (๋ฏธ๋ฆฌ fixed-size ์„ ์–ธ)

memory allocation์ด ์ผ์–ด๋‚œ๋‹ค ์ด๋ฅผ Static Memory Allocation ์ด๋ผํ•จ

์ด ๊ฒฝ์šฐ Stack memory์˜์—ญ์— ํ• ๋‹น์ด ๋จ

  • Linked list

runtime๋‹จ๊ณ„์—์„œ ์ƒˆ๋กœ์šด node๊ฐ€ ์ถ”๊ฐ€๋˜๋Š” ๊ฒฝ์šฐ memory allocation์ด ์ผ์–ด๋‚˜๋ฉฐ

์ด๋ฅผ Dynamic Memory Allocation์ด๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค

์ฆ‰ ์‚ฌ์šฉ์ž์˜ ๋™์  ํ• ๋‹น์ด ๊ฐ€๋Šฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— Heap๋ฉ”๋ชจ๋ฆฌ ์˜์—ญ์— ํ• ๋‹น์ด ๋œ๋‹ค

0๊ฐœ์˜ ๋Œ“๊ธ€