05 - 18 jan - the sorting wrap-up
TRANSCRIPT
CS 321. Algorithm Analysis & Design Lecture 5
The Sorting Wrap-Up
19 1 2 13 8 5 6 3 12 11 7 16
19 1 2 13 8 5 6 3 12 11 7 16
19
19 1 2 13 8 5 6 3 12 11 7 16
19
1 2
19 1 2 13 8 5 6 3 12 11 7 16
19
1 2
13 8 5 6
19 1 2 13 8 5 6 3 12 11 7 16
19
1 2
13 8 5 6
3 12 11 7 16
19 1 2 13 8 5 6 3 12 11 7 16
19
1 2
13 8 5 6
3 12 11 7 16
19 1 2 13 8 5 6 3 12 11 7 16
19
1 2
13 8 5 6
3 12 11 7 16
19 1 2 13 8 5 6 3 12 11 7 16
19
1 2
13 8 5 6
3 12 11 7 16
19 1 2 13 8 5 6 3 12 11 7 16
19
1 2
13 8
5
6
3 12 11 7
16
19 1 2 13 8 56 3 12 11 716
19
1 2
13 8
5
6
3 12 11 7
16
19 1 2 13 8 56 3 12 11 716
19
1 2
13 8
5
6
3 12 11 7
16
19 1 2 13 8 56 3 1211 716
19
1 2
13
8 5
6
3 12
11
7
16
19 1 2 13 8 56 3 1211 716
19
1 2
13
8 5
6
3 12
11
7
16
19 1 2 13 8 56 3 1211 716
19
1 2
13
8 5
6
3 12
11
7
16
19 1 213 8 56 3 1211 716
19
1
213
8 5
6
3 12
11
7
16
19 1 213 85 6 3 1211 716
19
1
2
13
8
5 6
3 12
11
7
16
19 1 213 85 6 3 1211 716
19
1
2
13
8
5 6
3 12
11
7
16
19 1 213 85 6 3 1211 716
19
1
2
13
8
5 6
3 12
11
7
16
19 1 213 85 6 312 11 716
19
1 2
13
8
5 6
3
12 11
7
16
19 1 213 85 6 312 11 716
19
1 2
13
8
5 6
3
12 11
7
16
Node at height h has to move at most h steps to find its place.
Node at height h has to move at most h steps to find its place.
Number of nodes at height h?
Node at height h has to move at most h steps to find its place.
Number of nodes at height h?
2(log n - h)
Node at height h has to move at most h steps to find its place.
Number of nodes at height h?
2(log n - h)
Overall running time (summed over h):
Node at height h has to move at most h steps to find its place.
Number of nodes at height h?
2(log n - h)
n(h/2(h))
Overall running time (summed over h):
Node at height h has to move at most h steps to find its place.
Number of nodes at height h?
2(log n - h)
n(h/2(h))
Overall running time (summed over h):
Heapify: Turns out to be a linear effort overall.
19 1 2 13 8 5 6 3 12 11 7 16
19
13 16
12 11 5 6
3 1 8 7 2
19 1 2 13 8 5 6 3 12 11 7 16
19
13 16
12 11 5 6
3 1 8 7 2
To sort, repeatedly extract the
maximum element to the end of the array.
An O(n log n) effort.
Finding Medians
Finding Mediansusing heaps
19
13 16
12 11 5 6
3 1 8 7 2
19 13 16 12 11 5 6 3 1 8 7 2
Store the first (n/2) elements in a max-heap.
Store the last (n/2) elements in a min-heap.