buildheapand heap sort - college of...
TRANSCRIPT
![Page 1: BuildHeapand Heap Sort - College of Engineeringweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · Heap Implementation: Sort Descending Sorts the data in descending order: 1](https://reader034.vdocuments.net/reader034/viewer/2022042222/5ec95e9c918cbb6baa072539/html5/thumbnails/1.jpg)
CS 261 – Data Structures
Build�Heap and Heap Sort
1
![Page 2: BuildHeapand Heap Sort - College of Engineeringweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · Heap Implementation: Sort Descending Sorts the data in descending order: 1](https://reader034.vdocuments.net/reader034/viewer/2022042222/5ec95e9c918cbb6baa072539/html5/thumbnails/2.jpg)
HW6#include <stdio.h>FILE *filePtr;char filename[100];
filePtr = fopen(filename, "w");
if (filePtr == NULL)printf("Cannot open %s\n", filename);
fprintf(filePtr, "%d\t%s\n", task.priority,task.description);
fclose(filePtr);
2
![Page 3: BuildHeapand Heap Sort - College of Engineeringweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · Heap Implementation: Sort Descending Sorts the data in descending order: 1](https://reader034.vdocuments.net/reader034/viewer/2022042222/5ec95e9c918cbb6baa072539/html5/thumbnails/3.jpg)
HW6#include <stdio.h>FILE *filePtr;char filename[100];int priority;
filePtr = fopen(filename, "r");
if (filePointer == NULL)printf("Cannot open %s\n", filename);
while(fscanf(filePtr,"%d\t",&priority) != EOF){ ... }
fclose(filePtr);
3
![Page 4: BuildHeapand Heap Sort - College of Engineeringweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · Heap Implementation: Sort Descending Sorts the data in descending order: 1](https://reader034.vdocuments.net/reader034/viewer/2022042222/5ec95e9c918cbb6baa072539/html5/thumbnails/4.jpg)
HW6#include <stdio.h>FILE *filePtr;char filename[100];char desc[TASK_DESC_SIZE];
....while(fscanf(filePtr,"%d\t",&priority) != EOF){
...
fgets(desc, sizeof(desc), filePtr);}
fclose(filePtr);
4
![Page 5: BuildHeapand Heap Sort - College of Engineeringweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · Heap Implementation: Sort Descending Sorts the data in descending order: 1](https://reader034.vdocuments.net/reader034/viewer/2022042222/5ec95e9c918cbb6baa072539/html5/thumbnails/5.jpg)
Heap Implementation: Constructors
Given an array of data,
construct the heap
5
![Page 6: BuildHeapand Heap Sort - College of Engineeringweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · Heap Implementation: Sort Descending Sorts the data in descending order: 1](https://reader034.vdocuments.net/reader034/viewer/2022042222/5ec95e9c918cbb6baa072539/html5/thumbnails/6.jpg)
Heap Implementation: Constructorsvoid buildHeap(struct dynArray * da) {
int maxIdx = da->size;
int i;
for (i = )
}
6
![Page 7: BuildHeapand Heap Sort - College of Engineeringweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · Heap Implementation: Sort Descending Sorts the data in descending order: 1](https://reader034.vdocuments.net/reader034/viewer/2022042222/5ec95e9c918cbb6baa072539/html5/thumbnails/7.jpg)
Heap Implementation: Constructorsvoid buildHeap(struct dynArray * da) {
int maxIdx = da->size;
int i;
for (i = maxIdx / 2 - 1; i >= 0; i--)
/* Make the heap from the subtree rooted at i */_adjustHeap(da, maxIdx, i);
}
7
![Page 8: BuildHeapand Heap Sort - College of Engineeringweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · Heap Implementation: Sort Descending Sorts the data in descending order: 1](https://reader034.vdocuments.net/reader034/viewer/2022042222/5ec95e9c918cbb6baa072539/html5/thumbnails/8.jpg)
Heap Implementation: Constructorsvoid buildHeap(struct dynArray * da) {
int maxIdx = da->size;
int i;
for (i = maxIdx / 2 - 1; i >= 0; i--)
/* Make the heap from the subtree rooted at i */_adjustHeap(da, maxIdx, i);
}
Why?
8
![Page 9: BuildHeapand Heap Sort - College of Engineeringweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · Heap Implementation: Sort Descending Sorts the data in descending order: 1](https://reader034.vdocuments.net/reader034/viewer/2022042222/5ec95e9c918cbb6baa072539/html5/thumbnails/9.jpg)
Heap Implementation: Constructorsvoid buildHeap(struct dynArray * da) {
int maxIdx = da->size;
int i;
for (i = maxIdx / 2 - 1; i >= 0; i--)
/* Make the heap from the subtree rooted at i */_adjustHeap(da, maxIdx, i);
}
Why?
At the beginning, only the leaves are proper heaps:Leaves are all nodes with indices > maxIdx / 2
9
![Page 10: BuildHeapand Heap Sort - College of Engineeringweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · Heap Implementation: Sort Descending Sorts the data in descending order: 1](https://reader034.vdocuments.net/reader034/viewer/2022042222/5ec95e9c918cbb6baa072539/html5/thumbnails/10.jpg)
Heap Implementation: Constructorsvoid buildHeap(struct dynArray * da) {
int maxIdx = da->size;
int i;
for (i = maxIdx / 2 - 1; i >= 0; i--)
/* Make the heap from the subtree rooted at i */_adjustHeap(da, maxIdx, i);
}
At each step, the subtree rooted at i becomes a heap10
![Page 11: BuildHeapand Heap Sort - College of Engineeringweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · Heap Implementation: Sort Descending Sorts the data in descending order: 1](https://reader034.vdocuments.net/reader034/viewer/2022042222/5ec95e9c918cbb6baa072539/html5/thumbnails/11.jpg)
Heap Implementation: Build heapvoid buildHeap(struct dynArray * da) {
int maxIdx = da->size;
int i;
for (i = maxIdx / 2 - 1; i >= 0; i--)
_adjustHeap(da, maxIdx, i); /* Make subtree rooted at i a heap */}
• For all subtrees that are are not already heaps:
–Call _adjustHeap with the largest node index that is not already guaranteed to be a heap
–Iterate until the root node becomes a heap
11
![Page 12: BuildHeapand Heap Sort - College of Engineeringweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · Heap Implementation: Sort Descending Sorts the data in descending order: 1](https://reader034.vdocuments.net/reader034/viewer/2022042222/5ec95e9c918cbb6baa072539/html5/thumbnails/12.jpg)
Heap Implementation: Build heapvoid buildHeap(struct dynArray * da) {
int maxIdx = da->size;
int i;
for (i = maxIdx / 2 - 1; i >= 0; i--)
_adjustHeap(da, maxIdx, i); /* Make subtree rooted at i a heap */}
• Why call _adjustHeap with the largest node index ? –Because its children, having larger indices, are
already guaranteed to be heaps
12
![Page 13: BuildHeapand Heap Sort - College of Engineeringweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · Heap Implementation: Sort Descending Sorts the data in descending order: 1](https://reader034.vdocuments.net/reader034/viewer/2022042222/5ec95e9c918cbb6baa072539/html5/thumbnails/13.jpg)
Heap Implementation: _adjustHeap12
5
4
7
78 3
9 11 10 217
25
38
43
012
11
max
57
64
79
811
910
102
i = (max/2-1)
Already heaps (leaf nodes)
adjustHeap
First iteration: adjustlargest non-leafnode (index 4)
13
![Page 14: BuildHeapand Heap Sort - College of Engineeringweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · Heap Implementation: Sort Descending Sorts the data in descending order: 1](https://reader034.vdocuments.net/reader034/viewer/2022042222/5ec95e9c918cbb6baa072539/html5/thumbnails/14.jpg)
Heap Implementation: adjustHeap (cont.)12
5
4
7
78 2
9 11 10 317
25
38
42
012
11
max
57
64
79
811
910
103
i (no adjustment needed)
Already heaps
adjustHeap
Second iteration: adjust largest non-heapnode (index 3)
14
![Page 15: BuildHeapand Heap Sort - College of Engineeringweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · Heap Implementation: Sort Descending Sorts the data in descending order: 1](https://reader034.vdocuments.net/reader034/viewer/2022042222/5ec95e9c918cbb6baa072539/html5/thumbnails/15.jpg)
Heap Implementation: _adjustHeap (cont.)12
5
4
7
78 2
9 11 10 317
25
38
42
012
11
max
57
64
79
811
910
103
i
Already heaps
adjustHeap
Third iteration: adjustlargest non-heapnode (index 2)
15
![Page 16: BuildHeapand Heap Sort - College of Engineeringweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · Heap Implementation: Sort Descending Sorts the data in descending order: 1](https://reader034.vdocuments.net/reader034/viewer/2022042222/5ec95e9c918cbb6baa072539/html5/thumbnails/16.jpg)
Heap Implementation: adjustHeap (cont.)12
4
5
7
78 2
9 11 10 317
24
38
42
012
11
max
57
65
79
811
910
103
i Already heaps
adjustHeap
Fourth iteration: adjustlargest non-heapnode (index 1)
16
![Page 17: BuildHeapand Heap Sort - College of Engineeringweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · Heap Implementation: Sort Descending Sorts the data in descending order: 1](https://reader034.vdocuments.net/reader034/viewer/2022042222/5ec95e9c918cbb6baa072539/html5/thumbnails/17.jpg)
Heap Implementation: adjustHeap (cont.)12
4
5
2
78 3
9 11 10 712
24
38
43
012
11
max
57
65
79
811
910
107
i Already heaps
Fifth iteration: adjust largest non-heapnode (index 0 à root)
adjustHeap
17
![Page 18: BuildHeapand Heap Sort - College of Engineeringweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · Heap Implementation: Sort Descending Sorts the data in descending order: 1](https://reader034.vdocuments.net/reader034/viewer/2022042222/5ec95e9c918cbb6baa072539/html5/thumbnails/18.jpg)
Heap Implementation: adjustHeap (cont.)2
4
5
3
78 7
9 11 10 1213
24
38
47
02
57
65
79
811
910
1012
Already heaps (entire tree)18
![Page 19: BuildHeapand Heap Sort - College of Engineeringweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · Heap Implementation: Sort Descending Sorts the data in descending order: 1](https://reader034.vdocuments.net/reader034/viewer/2022042222/5ec95e9c918cbb6baa072539/html5/thumbnails/19.jpg)
Heap Implementation: Sort Descending
Sorts the data in descending order:
1. Builds heap from initial (unsorted) data
2. Iteratively swaps the smallest element (at index 0) with last unsorted element
3. Adjust the heap after each swap, but only considers the unsorted data
19
![Page 20: BuildHeapand Heap Sort - College of Engineeringweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · Heap Implementation: Sort Descending Sorts the data in descending order: 1](https://reader034.vdocuments.net/reader034/viewer/2022042222/5ec95e9c918cbb6baa072539/html5/thumbnails/20.jpg)
Heap Implementation: Sort Descendingvoid heapSort(struct dyArray * data) {
int i;
buildHeap(data);
for (i = sizeDynArr(data)–1; i > 0; i--){
swapDynArr(data,i,0);/*Swap last el. with the first*/
_adjustHeap(data,i,0); /* build heap property*/}
}
20
![Page 21: BuildHeapand Heap Sort - College of Engineeringweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · Heap Implementation: Sort Descending Sorts the data in descending order: 1](https://reader034.vdocuments.net/reader034/viewer/2022042222/5ec95e9c918cbb6baa072539/html5/thumbnails/21.jpg)
Heap Analysis: Sort• Execution time:
–Build heap: •n calls to adjustHeap = n log n
–Loop: •n calls to adjustHeap = n log n
–Total: •2n log n = O(n log n)
21
![Page 22: BuildHeapand Heap Sort - College of Engineeringweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · Heap Implementation: Sort Descending Sorts the data in descending order: 1](https://reader034.vdocuments.net/reader034/viewer/2022042222/5ec95e9c918cbb6baa072539/html5/thumbnails/22.jpg)
Heap Analysis: Sort
• Advantages/disadvantages:
–Same average as merge sort and quick sort
–Doesn’t require extra space as the merge
sort does
–Doesn’t suffer if data is already sorted or
mostly sorted22