edubun 한국어

Arrays

Reading the millionth value of an array takes one step, but putting a new value at the front of the same array moves a million values.

What a data structure is

A program that keeps a list of names, a queue of print jobs or a table of prices has to store that data somewhere, and it has to answer questions about it: what is the third item, is this name in the list, which job is next? A data structure is a way of storing data together with the methods that answer those questions. MIT's 6.006 course separates two ideas here. The interface says which operations you can ask for, such as "get the item at position i" or "add an item at the end". The data structure is how those operations are actually carried out in memory. One interface can have several data structures behind it, and they can differ hugely in speed.

This course counts steps, as the algorithms course does, so that two structures can be compared without a stopwatch. In this unit a step is reading or writing one cell of memory. The lab below counts them for you.

One block of cells, side by side

An array stores its values in one unbroken block of memory, each value in a cell of the same size, numbered from 0. That layout is what makes arrays fast to read. If the block starts at address s and each cell takes c bytes, then a[i] lives at address s + i × c. The computer works that out with one multiplication and one addition and jumps straight there, whether i is 3 or 3,000,000. MIT's notes list "get at(i)" and "set at(i, x)" for an array as Θ(1), meaning the cost does not grow with the length.

The same layout makes changes in the middle slow. Suppose the array holds 7, 3, 9, 4, 6 and you want 1 at the front. There is no gap in front of 7, so every value has to move one cell to the right first, starting from the last one so nothing is overwritten. That is 5 moves and 1 write: 6 steps. Removing the front value is the mirror image: read it, then move the other 4 values one cell left. Inserting at position i in an array of n values costs n − i moves plus one write, so the front is the worst place and the end is the best.

Loading

Drag to turn. Scroll or pinch to zoom.

An array of five values in a block of five cells. Move the slider to choose i, then read, insert or remove. Pushing onto the full block copies everything into a block twice as large.

When the block is full

A block of memory has a fixed size, so what happens when you add a value and every cell is taken? The array has to ask for a new, larger block and copy every value across before it can write the new one. In the lab, pushing onto the full block of 5 costs 5 copies and 1 write. If the array grew by just one cell each time, every push would copy everything, and n pushes would cost about n²/2 copies.

The fix described in MIT's notes is to grow by a lot at once, for example by doubling. Starting from capacity 1, the copies happen when the array holds 1, 2, 4, 8 and 16 values, so 17 pushes cost 1 + 2 + 4 + 8 + 16 = 31 copies in total, fewer than 2 per push. A single push can still be slow, but any long run of pushes averages out to a constant cost per push. This is called amortized Θ(1) time. A JavaScript array behaves this way from the outside: MDN describes arrays as resizable, and push and pop work at the end, which is the cheap end.

Operation on n valuesSteps in the labGrows like
Read or write a[i]1Θ(1)
Insert at the frontn moves + 1 writeΘ(n)
Remove the front1 read + (n − 1) movesΘ(n)
Push at the end (room left)1Θ(1)
Push at the end (block full)n copies + 1 writeΘ(n) once, Θ(1) amortized
const a = [7, 3, 9, 4, 6];
console.log(a[3]);      // 4, one step
a.push(8);              // cheap: the end
a.unshift(1);           // every value moves one cell right
console.log(a.join(" ")); // 1 7 3 9 4 6 8
BackgroundArrays are older than most programming languages. Early machines stored a table as numbered words in memory, and programmers computed the address of entry i by hand, exactly as the formula above does. Fortran, released by IBM in 1957, made arrays part of the language so that people could write A(I) and let the compiler do the address arithmetic. Fortran counted from 1. C and JavaScript count from 0, which fits the formula: a[0] sits at the start address itself.
InterfaceThe list of operations you may ask for, such as get at(i) or insert last(x).
Data structureHow those operations are carried out in memory.
ArrayValues in one block of equal cells; a[i] is at start + i × cell size.
Dynamic arrayAn array that grows by copying into a larger block, usually twice the size.
Amortized costThe average cost per operation over a long run, even if some single operations are slow.
  1. In this unit's step count, how many steps does reading a[700000] from an array of 1,000,000 values take?
    • 1
    • 20
    • 700,000
    • 1,000,000
  2. An array holds 7, 3, 9, 4, 6 with room to spare. How many steps does inserting a value at the front take (moves plus the write)?
  3. Same array of 5 values: how many steps does removing the front value take (1 read plus the moves)?
  4. Inserting at position 2 into an array of 5 values (positions 0 to 4) with room: how many steps?
  5. A doubling array holds 5 values and its capacity is 5. You push one more value. How many steps does that push take?
  6. After that push, what is the capacity?
  7. Starting from capacity 1 and doubling, how many copies do 17 pushes make in total?
  8. Why is push on a doubling array called amortized Θ(1)?
    • Every single push takes exactly one step
    • Copies are rare, so the total over n pushes stays below a constant times n
    • The computer copies in the background for free
    • JavaScript never copies arrays
  9. In MIT 6.006 terms, "insert last(x) and delete last()" is
    • a data structure
    • an interface
    • a memory address
    • a sorting method
  10. An array has length n and counts from 0. What is the index of its last value?
    • n
    • n − 1
    • n + 1
    • 1
Key points
  • An interface lists operations; a data structure is how they are done.
  • An array keeps values in one block, so a[i] is one step.
  • Inserting or removing near the front moves about n values.
  • A doubling array makes push amortized Θ(1): 17 pushes from capacity 1 copy 31 values.
Sources for this course (5)
  • MIT OpenCourseWare 6.006 Introduction to Algorithms, Spring 2020 (Demaine, Ku, Solomon), lecture notes 2 Data Structures (interface vs data structure, Sequence and Set interfaces, stack = insert_last/delete_last, queue = insert_last/delete_first, array, linked list, dynamic array, amortized Θ(1) append), 4 Hashing (chaining, pigeonhole collisions, chain length n/m), 6 Binary Trees Part 1 (height, traversal order, O(h) operations) and 7 Binary Trees Part 2: AVL (Adelson-Velsky and Landis, 1962) at ocw.mit.edu/courses/6-006-introduction-to-algorithms-spring-2020/pages/lecture-notes
  • MDN Web Docs, Array.prototype.shift (shifts all values left by 1), Array.prototype.unshift, Map (insertion order, Map vs Object, spec requires sublinear average access) and Set (developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects)
  • ECMAScript 2025 Language Specification (tc39.es/ecma262), 24.1 Map Objects and 24.2 Set Objects ("must be implemented using either hash tables or other mechanisms that, on average, provide access times that are sublinear on the number of elements"), Array.prototype.shift steps (Repeat, while k < len: move element k to k - 1)
  • D. E. Knuth, The Art of Computer Programming vol. 3 (2nd ed. 1998) section 6.4 History (H. P. Luhn, IBM internal memorandum January 1953, hashing with chaining; digit pairs added mod 10, 31415926 -> 4548) via gwern.net copy, and IEEE Spectrum, "Hans Peter Luhn and the Birth of the Hashing Algorithm" (2018)
  • "A brief history of the stack", SIGCIS (sigcis.org/files/A brief history.pdf: Bauer and Samelson applied for the patent in 1957, US 3,047,228 granted July 31, 1962; IPL list processing by Newell, Simon and Shaw at RAND 1956) and Wikipedia, Information Processing Language (Newell, Shaw, Simon, RAND and Carnegie Institute of Technology, about 1956).

Checked: 2026-10-18