BEFORE: Percolate Up to make a (min) Heap: heap[1]..heap[n]
NOW: Percolate Down to make a (max) Heap: a[1]..a[n]
Percolate Down(i,n): makes a heap out of a[i]..a[n]
Compare each node with its children.
How many children does n/2 have? n/2+1? ... n?
BuildHeap:
PercDown(5,10)
PercDown(4,10) ...
PercDown(1,10)
HeapSort:
Swap(1,10); PercDown(1,9)
Swap(1,9); PercDown(1,8) ...
Swap(1,2); PercDown(1,1)
Heapsort: Build (max)Heap [160]
Heapsort: Swap and PercDown [161]
Heapsort: Swap and PercDown [162]
Heapsort: Array Values [163]
BUILDHEAP by PercDown
21 32 19 13 16 65 31 19 68 26 <-- unsorted
PercDown(5,10): 21 32 19 13 26 65 31 19 68 16
PercDown(4,10): 21 32 19 68 26 65 31 19 13 16
PercDown(3,10): 21 32 65 68 26 19 31 19 13 16
PercDown(2,10): 21 68 65 32 26 19 31 19 13 16
PercDown(1,10): 68 32 65 21 26 19 31 19 13 16 <-- (max)heap
SORT by Swap and PercDown
Swap(1,10); PercDown(1,9) : 65 32 31 21 26 19 16 19 13 68
Swap(1,9); PercDown(1,8) : 32 26 31 21 13 19 16 19 65 68
Swap(1,8); PercDown(1,7) : 31 26 19 21 13 19 16 32 65 68
Swap(1,7); PercDown(1,6) : 26 21 19 16 13 19 31 32 65 68
Swap(1,6); PercDown(1,5) : 21 19 19 16 13 26 31 32 65 68
Swap(1,5); PercDown(1,4) : 19 16 19 13 21 26 31 32 65 68
Swap(1,4); PercDown(1,3) : 19 16 13 19 21 26 31 32 65 68
Swap(1,3); PercDown(1,2) : 16 13 19 19 21 26 31 32 65 68
Swap(1,2); PercDown(1,1) : 13 16 19 19 21 26 31 32 65 68 <-- sorted
Heapsort: heap [164]
% gcc -o heap heap.c > tcc heap.c
Data File with Test Case: heap.dat: 10 21 32 19 13 16 65 31 19 68 26
% heap heap.dat 10 73 22 [ 13 16 19 19 21 26 31 32 65 68 ] 0 0 % heap heap.dat step n move comp 1 2 3 4 5 6 7 8 9 10 i j 10 0 0 [ 21 32 19 13 16 65 31 19 68 26 ] 0 0 10 3 1 [ 21 32 19 13 26 65 31 19 68 16 ] 5 10 10 6 2 [ 21 32 19 68 26 65 31 19 13 16 ] 4 10 10 9 3 [ 21 32 65 68 26 19 31 19 13 16 ] 3 10 10 12 5 [ 21 68 65 32 26 19 31 19 13 16 ] 2 10 10 16 8 [ 68 32 65 21 26 19 31 19 13 16 ] 1 10 10 23 10 [ 65 32 31 21 26 19 16 19 13 68 ] 1 9 10 30 12 [ 32 26 31 21 13 19 16 19 65 68 ] 1 8 10 36 14 [ 31 26 19 21 13 19 16 32 65 68 ] 1 7 10 43 16 [ 26 21 19 16 13 19 31 32 65 68 ] 1 6 10 49 18 [ 21 19 19 16 13 26 31 32 65 68 ] 1 5 10 56 20 [ 19 16 19 13 21 26 31 32 65 68 ] 1 4 10 62 21 [ 19 16 13 19 21 26 31 32 65 68 ] 1 3 10 68 22 [ 16 13 19 19 21 26 31 32 65 68 ] 1 2 10 73 22 [ 13 16 19 19 21 26 31 32 65 68 ] 1 1
Heapsort: heap.c [165]
void perc_down(input_type a[], int i, int n) {
int child; input_type tmp;
for (tmp=a[i]; i*2 <= n; i=child) {
child = i*2;
if ((child != n) && (a[child+1] > a[child]))
child++;
if (tmp < a[child])
a[i] = a[child];
else
break;
}
a[i] = tmp;
}
void heapsort(input_type a[], int n) {
int i, j;
j = n;
for (i=n/2; i>0; i--) /* BuildHeap */
perc_down(a,i,j);
i = 1;
for (j=n; j>=2; j--) {
swap(&a[i],&a[j]); /* DeleteMax */
perc_down(a,i,j-1);
}
}
Heapsort: Analysis [166]
How deep is the tree? O( )
To build a heap:
How many steps does it take to do a PercDown? O( )
How many PercDowns? O( )
To build a heap is O( )
After the heap is built:
How many steps does it take to do a Swap? O( )
How many steps does it take to do a PercDown? O( )
How many Swaps and PercDowns? O( )
To sort a heap is O( )
What is big-oh for Heapsort? O( )