Lecture 24: Memory and Locality
CSE 373: Data Structures and Algorithms
1
Warm Up
CSE 373 21 SP – CHAMPION
2
Worst case runtime?
Best case runtime?
In-practice runtime?
Stable?
In-place?
No
Yes
Worst case runtime?
Best case runtime?
In-practice runtime?
Stable?
In-place?
No
No
Worst case runtime?
Best case runtime?
In-practice runtime?
Stable?
In-place?
Yes
No
Merge Sort
Quick Sort
first element as pivot
Quick Sort
in-place
median of values as pivot
Announcements
CSE 373 21 SP – CHAMPION
3
Quick Sort (v1)
quickSort(list) {
if (list.length == 1):
return list
else:
pivot = choosePivot(list)
smallerHalf = quickSort(getSmaller(pivot, list))
largerHalf = quickSort(getBigger(pivot, list))
return smallerHalf + pivot + largerHalf
}
Worst case runtime?
Best case runtime?
In-practice runtime?
Stable?
In-place?
No
Can be done!
0 | 1 | 2 | 3 |
2 | 1 | 7 | 6 |
0 | 1 |
7 | 6 |
0 |
1 |
0 |
2 |
PIVOT
PIVOT
0 |
6 |
0 |
7 |
0 | 1 | 2 | 3 |
1 | 2 | 6 | 7 |
0 | 1 |
6 | 7 |
Worst case: Pivot only chops off one value
Best case: Pivot divides each array in half
Can we do better?
Strategies for Choosing a Pivot
Most commonly used
Quick Sort (v2: In-Place)
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
8 | 1 | 4 | 9 | 0 | 3 | 5 | 2 | 7 | 6 |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
6 | 1 | 4 | 9 | 0 | 3 | 5 | 2 | 7 | 8 |
Low
X < 6
High
X >= 6
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
6 | 1 | 4 | 2 | 0 | 3 | 5 | 9 | 7 | 8 |
Low
X < 6
High
X >= 6
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
5 | 1 | 4 | 2 | 0 | 3 | 6 | 9 | 7 | 8 |
PIVOT?
PIVOT?
PIVOT?
PIVOT!
Select a pivot
Move pivot out of the way
Bring low and high pointers together, swapping elements if needed
Meeting point is where pivot belongs; swap in. Now recurse on smaller portions of same array!
Divide
Quick Sort (v2: In-Place)
quickSort(list) {
if (list.length == 1):
return list
else:
pivot = choosePivot(list)
smallerPart, largerPart = partition(pivot, list)
smallerPart = quickSort(smallerPart)
largerPart = quickSort(largerPart)
return smallerPart + pivot + largerPart
}
Worst case runtime?
Best case runtime?
In-practice runtime?
Stable?
In-place?
No
Yes
0 | 1 | 2 | 3 | 4 | 5 |
0 | 3 | 6 | 9 | 7 | 8 |
choosePivot:
- Use one of the pivot selection strategies
partition:
Can we do better?
We’d really like to avoid hitting the worst case.
Key to getting a good running time, is always cutting the array (about) in half.
How do we choose a good pivot?
CSE 373 19 SU - ROBBIE WEBER
9
Pivots
CSE 373 19 SU - ROBBIE WEBER
10
Median of three is a common choice in practice
Sorting: Summary
| Best-Case | Worst-Case | Space | Stable |
Selection Sort | Θ(n2) | Θ(n2) | Θ(1) | No |
Insertion Sort | Θ(n) | Θ(n2) | Θ(1) | Yes |
Heap Sort | Θ(n) | Θ(nlogn) | Θ(n) | No |
In-Place Heap Sort | Θ(n) | Θ(nlogn) | Θ(1) | No |
Merge Sort | Θ(nlogn) | Θ(nlogn) | Θ(nlogn) Θ(n)* optimized | Yes |
Quick Sort | Θ(nlogn) | Θ(n2) | Θ(n) | No |
In-place Quick Sort | Θ(nlogn) | Θ(n2) | Θ(1) | No |
What does Java do?
Key Takeaway: No single sorting algorithm is “the best”!
* They actually use Tim Sort, which is very similar to Merge Sort in theory, but has some minor details different
Insertion Sort
STRATEGY 1:
ITERATIVE IMPROVEMENT
STRATEGY 2:
IMPOSE STRUCTURE
STRATEGY 3:
DIVIDE AND CONQUER
Selection Sort
Heap Sort
Merge Sort
Quick Sort
WORST
BEST
STABLE
IN-PLACE
IN-PLACE
IN-PLACE
IN-PLACE
STABLE
WORST
BEST
WORST
BEST
WORST
BEST
WORST
BEST
Minimizes array writes, otherwise never preferred.
Simple, stable, low-overhead, great if already sorted.
Always good runtimes
Stable, very reliable! In-place variant is slower.
Fastest in practice (constant factors), bad worst case.
SPACE
SPACE
SPACE
SPACE
SPACE
Insertion Sort
STRATEGY 1:
ITERATIVE IMPROVEMENT
STRATEGY 2:
IMPOSE STRUCTURE
STRATEGY 3:
DIVIDE AND CONQUER
Selection Sort
Heap Sort
Merge Sort
Quick Sort
WORST
BEST
STABLE
IN-PLACE
IN-PLACE
IN-PLACE
IN-PLACE
STABLE
WORST
BEST
WORST
BEST
WORST
BEST
WORST
BEST
Minimizes array writes, otherwise never preferred.
Simple, stable, low-overhead, great if already sorted.
Always good runtimes
Stable, very reliable! In-place variant is slower.
Fastest in practice (constant factors), bad worst case.
SPACE
SPACE
SPACE
SPACE
SPACE
Can we do better than n log n?
But Don’t Take it From Me…
DANCE EDITION
Here are some excellent visualizations for the sorting algorithms we’ve talked about!
Comparing Sorting Algorithms
Comparing Sorting Algorithms
Memory & Locality!
CSE 373 20 SP – CHAMPION & CHUN
15
Review: Binary, Bits and Bytes
CSE 373 SP 18 - KASEY CHAMPION
16
Decimal | Decimal Break Down | Binary | Binary Break Down |
0 | | 0 | |
1 | | 1 | |
10 | | 1010 | |
12 | | 1100 | |
127 | | 01111111 | |
Thought experiment
CSE 373 SP 18 - KASEY CHAMPION
17
What do these two methods do?
What is the big-Θ
Θ(n*m)
Incorrect Assumptions
CSE 373 SP 18 - KASEY CHAMPION
18
Lies!
RAM (Random-Access Memory)
- RAM goes by a ton of different names: memory, main memory, RAM are all names for this same thing.
CSE 373 SP 19 - KASEY CHAMPION
19
RAM can be represented as a huge array
CSE 373 SP 19 - KASEY CHAMPION
20
=
This is a main takeaway
If you’re interested in deeper than this : https://www.youtube.com/watch?v=fpnE6UAfbtU or take some EE classes?
RAM:
Arrays
A rough view of arrays and linked lists
CSE 373 SP 19 - KASEY CHAMPION
21
int[] array = new int[3];
array[0] = 3;
array[1] = 7;
array[2] = 3;
Node front = new Node(3);
front.next = new Node(7);
front.next.next = new Node(3);
3
7
3
3
7
3
(drawing singly linked list instead of doubly because drawings are hard / the two are similar)
Memory Architecture
CSE 373 SP 18 - KASEY CHAMPION
22
CPU Register
L1 Cache
L2 Cache
RAM
Disk
What is it? | Typical Size | Time |
The brain of the computer! | 32 bits | ≈free |
Extra memory to make accessing it faster | 128KB | 0.5 ns |
Extra memory to make accessing it faster | 2MB | 7 ns |
Working memory, what your programs need | 8GB | 100 ns |
Large, longtime storage | 1 TB | 8,000,000 ns |
Memory Architecture
- accessing the disk is very slow
Computer Design Decisions
CSE 373 SP 18 - KASEY CHAMPION
23
Locality
CSE 373 SP 18 - KASEY CHAMPION
24
Leveraging Spatial Locality
CSE 373 SP 18 - KASEY CHAMPION
25
How memory is used and moves around
CSE 373 SP 19 - KASEY CHAMPION
26
CSE 373 SP 19 - KASEY CHAMPION
27
CSE 373 SP 19 - KASEY CHAMPION
28
CSE 373 SP 19 - KASEY CHAMPION
29
CSE 373 SP 19 - KASEY CHAMPION
30
CSE 373 SP 19 - KASEY CHAMPION
31
CSE 373 SP 19 - KASEY CHAMPION
32
CSE 373 SP 19 - KASEY CHAMPION
33
CSE 373 SP 19 - KASEY CHAMPION
34
CSE 373 SP 19 - KASEY CHAMPION
35
Solution to Mercy’s traveling problem
CSE 373 SP 19 - KASEY CHAMPION
36
CSE 373 SP 19 - KASEY CHAMPION
37
CSE 373 SP 19 - KASEY CHAMPION
38
CSE 373 SP 19 - KASEY CHAMPION
39
CSE 373 SP 19 - KASEY CHAMPION
40
RAM
CPU
CPU – kind of like the home / brain of your computer. Pretty much all computation is done here and data needs to move here to do anything significant with it (math, if checks, normal statement execution).
Data travels between RAM and the CPU, but it’s slow
Before
CSE 373 SP 19 - KASEY CHAMPION
41
RAM
CPU
Cache!
Bring a bunch of data back when you go all the way to RAM
Bring a bunch of food back when you go all the way to the store
After
Cache
CSE 373 SP 19 - KASEY CHAMPION
42
CSE 373 SP 19 - KASEY CHAMPION
43
RAM
CPU
Cache!
Bring a bunch of data back when you go all the way to RAM
Bring a bunch of food back when you go all the way to the store
After
This is a big idea!
How is a bunch of memory taken from RAM?
CSE 373 SP 19 - KASEY CHAMPION
44
This is a big idea (continued)!
How is a bunch of memory taken from RAM?�(continued)
CSE 373 SP 19 - KASEY CHAMPION
45
cache
original data (the 1) we wanted to look up gets passed back to the cpu
CPU
all the data from the block gets brought to the cache
How does this pattern of memory grabbing affect our programs?
Imagine that the below memory is just an entire array of length 13, with some data in it.
CSE 373 SP 19 - KASEY CHAMPION
46
Just by accessing one element we bring the nearby elements back with us to the cache. In this case, it’s almost all of the array!
Leveraging Temporal Locality
CSE 373 SP 18 - KASEY CHAMPION
47
Moving Memory
CSE 373 SP 18 - KASEY CHAMPION
48
Thought Experiment
CSE 373 SP 18 - KASEY CHAMPION
49
Why does sum1 run so much faster than sum2?
sum1 takes advantage of spatial and temporal locality
0 | 1 | 2 | 3 | 4 |
| | | | |
0 | 1 | 2 |
‘a’ | ‘b’ | ‘c’ |
0 | 1 | 2 |
‘d’ | ‘e’ | ‘f’ |
0 | 1 | 2 |
‘g’ | ‘h’ | ‘i’ |
0 | 1 | 2 |
‘j’ | ‘k’ | ‘l’ |
0 | 1 | 2 |
‘m’ | ‘n’ | ‘o’ |
Java and Memory
CSE 373 SP 18 - KASEY CHAMPION
50
What happens when you create a new array?
What happens when you create a new object?
What happens when you read an array index?
What happens when we open and read data from a file?
Array v Linked List
CSE 373 SP 18 - KASEY CHAMPION
51