Array
A list of elements stored next to each other in memory. Since each takes the same space, you can jump straight to any position.
Example
In an array of 1,000 numbers, reading position 500 is 1 step. Inserting one at the start forces all 1,000 to shift one place.
Test yourself: Array
Arrays
In most languages, an array's first element is at position ___.
In [7, 3, 9], 7 is at position 0, 3 at 1 and 9 at 2. The last element of an n-element array is at n − 1.
Arrays
How long does an array take to read the element at position 5,000?
Address = start + 5,000 × size: 1 calculation and 1 read, same as for position 5.
Arrays
Inserting or deleting in the middle of an array costs O(___) on average.
In a 1,000-element array, inserting at position 500 shifts the 500 after it. Inserting at the end shifts nothing.
0/3
â–¶ PlayLessons that use it
RAID
Many disks, one team
What is an algorithm
You already use algorithms
Measuring efficiency
Fast and light on memory
Types of structures
How they relate and where they live
Basic data types
The bricks of everything
How letters are stored
Every letter is a number
Arrays
Adjacent cells
Linked lists
Links of a chain
Dynamic lists
An array that grows by itself
Memory and cache
Close is fast
Stacks
Last in, first out
Queues
First in, first out
Deques
In and out at both ends
Hash tables
From key to slot in 1 calculation
Hash collisions
When 2 keys want the same spot
Trees in an array
No references, just math
Binary search trees
Smaller on the left, larger on the right
Heaps
The smallest, always on top
Building a heap
Bottom-up is cheaper
Graph operations
Adding and removing vertices and edges
Binary search in depth
2 pointers closing in
Boundaries with binary search
Where to insert and where a run starts
Searching with a hash
From O(n²) to O(n) with a table
Choosing how to search
Brute force or using the order
What makes a good sort
5 things you ask of it
Selection sort
Picking the smallest, over and over
Bubble sort
The largest rises like a bubble
Insertion sort
Like arranging the cards in your hand
Quick sort
A pivot and 2 sides
Merge sort
Split in half and merge in order
Heap sort
Take the maximum and leave it at the end
Bucket sort
Split into buckets and sort each one
Counting sort
Sorting by counting
Radix sort
Digit by digit
Divide and conquer
Split, solve and combine
Permutations
Every possible order
Subset sum
Groups that add up exactly
Dynamic programming
Solve each thing only once
