![Page 1: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/1.jpg)
Section 8.8
Heapsort
![Page 2: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/2.jpg)
Heapsort
Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations
Heapsort does not require any additional storage
As its name implies, heapsort uses a heap to store the array
![Page 3: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/3.jpg)
First Version of a Heapsort Algorithm
When used as a priority queue, a heap maintains a smallest value at the top
The following algorithm places an array's data into a heap, then removes each heap item (O(n log n)) and moves it back
into the array This version of the algorithm requires n extra storage
locationsHeapsort Algorithm: First Version
1. Insert each value from the array to be sorted into a priority queue (heap).
2. Set i to 0
3. while the priority queue is not empty
4. Remove an item from the queue and insert it back into the array at position i
5. Increment i
![Page 4: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/4.jpg)
Revising the Heapsort Algorithm
In heaps we've used so far, each parent node value was less than the values of its children
We can build a heap so that each parent node value is larger than its children
Then, move the top item to the bottom of the
heap reheap, ignoring the item moved to the
bottom
![Page 5: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/5.jpg)
Trace of Heapsort
89
76 74
37 32 39 66
20 26 18 28 29 6
Go to End
![Page 6: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/6.jpg)
Trace of Heapsort (cont.)
89
76 74
37 32 39 66
20 26 18 28 29 6
![Page 7: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/7.jpg)
Trace of Heapsort (cont.)
89
76 74
37 32 39 66
20 26 18 28 29 6
![Page 8: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/8.jpg)
Trace of Heapsort (cont.)
6
76 74
37 32 39 66
20 26 18 28 29 89
![Page 9: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/9.jpg)
Trace of Heapsort (cont.)
76
6 74
37 32 39 66
20 26 18 28 29 89
![Page 10: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/10.jpg)
Trace of Heapsort (cont.)
76
37 74
6 32 39 66
20 26 18 28 29 89
![Page 11: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/11.jpg)
Trace of Heapsort (cont.)
76
37 74
26 32 39 66
20 6 18 28 29 89
![Page 12: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/12.jpg)
Trace of Heapsort (cont.)
76
37 74
26 32 39 66
20 6 18 28 29 89
![Page 13: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/13.jpg)
Trace of Heapsort (cont.)
76
37 74
26 32 39 66
20 6 18 28 29 89
![Page 14: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/14.jpg)
Trace of Heapsort (cont.)
76
37 74
26 32 39 66
20 6 18 28 29 89
![Page 15: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/15.jpg)
Trace of Heapsort (cont.)
29
37 74
26 32 39 66
20 6 18 28 76 89
![Page 16: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/16.jpg)
Trace of Heapsort (cont.)
74
37 29
26 32 39 66
20 6 18 28 76 89
![Page 17: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/17.jpg)
Trace of Heapsort (cont.)
74
37 66
26 32 39 29
20 6 18 28 76 89
![Page 18: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/18.jpg)
Trace of Heapsort (cont.)
74
37 66
26 32 39 29
20 6 18 28 76 89
![Page 19: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/19.jpg)
Trace of Heapsort (cont.)
74
37 66
26 32 39 29
20 6 18 28 76 89
![Page 20: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/20.jpg)
Trace of Heapsort (cont.)
74
37 66
26 32 39 29
20 6 18 28 76 89
![Page 21: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/21.jpg)
Trace of Heapsort (cont.)
28
37 66
26 32 39 29
20 6 18 74 76 89
![Page 22: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/22.jpg)
Trace of Heapsort (cont.)
66
37 28
26 32 39 29
20 6 18 74 76 89
![Page 23: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/23.jpg)
Trace of Heapsort (cont.)
66
37 39
26 32 28 29
20 6 18 74 76 89
![Page 24: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/24.jpg)
Trace of Heapsort (cont.)
66
37 39
26 32 28 29
20 6 18 74 76 89
![Page 25: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/25.jpg)
Trace of Heapsort (cont.)
66
37 39
26 32 28 29
20 6 18 74 76 89
![Page 26: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/26.jpg)
Trace of Heapsort (cont.)
66
37 39
26 32 28 29
20 6 18 74 76 89
![Page 27: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/27.jpg)
Trace of Heapsort (cont.)
18
37 39
26 32 28 29
20 6 66 74 76 89
![Page 28: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/28.jpg)
Trace of Heapsort (cont.)
39
37 18
26 32 28 29
20 6 66 74 76 89
![Page 29: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/29.jpg)
Trace of Heapsort (cont.)
39
37 29
26 32 28 18
20 6 66 74 76 89
![Page 30: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/30.jpg)
Trace of Heapsort (cont.)
39
37 29
26 32 28 18
20 6 66 74 76 89
![Page 31: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/31.jpg)
Trace of Heapsort (cont.)
39
37 29
26 32 28 18
20 6 66 74 76 89
![Page 32: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/32.jpg)
Trace of Heapsort (cont.)
39
37 29
26 32 28 18
20 6 66 74 76 89
![Page 33: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/33.jpg)
Trace of Heapsort (cont.)
6
37 29
26 32 28 18
20 39 66 74 76 89
![Page 34: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/34.jpg)
Trace of Heapsort (cont.)
37
6 29
26 32 28 18
20 39 66 74 76 89
![Page 35: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/35.jpg)
Trace of Heapsort (cont.)
37
32 29
26 6 28 18
20 39 66 74 76 89
![Page 36: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/36.jpg)
Trace of Heapsort (cont.)
37
32 29
26 6 28 18
20 39 66 74 76 89
![Page 37: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/37.jpg)
Trace of Heapsort (cont.)
37
32 29
26 6 28 18
20 39 66 74 76 89
![Page 38: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/38.jpg)
Trace of Heapsort (cont.)
37
32 29
26 6 28 18
20 39 66 74 76 89
![Page 39: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/39.jpg)
Trace of Heapsort (cont.)
20
32 29
26 6 28 18
37 39 66 74 76 89
![Page 40: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/40.jpg)
Trace of Heapsort (cont.)
32
20 29
26 6 28 18
37 39 66 74 76 89
![Page 41: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/41.jpg)
Trace of Heapsort (cont.)
32
26 29
20 6 28 18
37 39 66 74 76 89
![Page 42: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/42.jpg)
Trace of Heapsort (cont.)
32
26 29
20 6 28 18
37 39 66 74 76 89
![Page 43: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/43.jpg)
Trace of Heapsort (cont.)
32
26 29
20 6 28 18
37 39 66 74 76 89
![Page 44: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/44.jpg)
Trace of Heapsort (cont.)
32
26 29
20 6 28 18
37 39 66 74 76 89
![Page 45: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/45.jpg)
Trace of Heapsort (cont.)
18
26 29
20 6 28 32
37 39 66 74 76 89
![Page 46: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/46.jpg)
Trace of Heapsort (cont.)
29
26 18
20 6 28 32
37 39 66 74 76 89
![Page 47: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/47.jpg)
Trace of Heapsort (cont.)
29
26 28
20 6 18 32
37 39 66 74 76 89
![Page 48: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/48.jpg)
Trace of Heapsort (cont.)
29
26 28
20 6 18 32
37 39 66 74 76 89
![Page 49: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/49.jpg)
Trace of Heapsort (cont.)
29
26 28
20 6 18 32
37 39 66 74 76 89
![Page 50: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/50.jpg)
Trace of Heapsort (cont.)
29
26 28
20 6 18 32
37 39 66 74 76 89
![Page 51: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/51.jpg)
Trace of Heapsort (cont.)
18
26 28
20 6 29 32
37 39 66 74 76 89
![Page 52: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/52.jpg)
Trace of Heapsort (cont.)
28
26 18
20 6 29 32
37 39 66 74 76 89
![Page 53: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/53.jpg)
Trace of Heapsort (cont.)
28
26 18
20 6 29 32
37 39 66 74 76 89
![Page 54: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/54.jpg)
Trace of Heapsort (cont.)
28
26 18
20 6 29 32
37 39 66 74 76 89
![Page 55: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/55.jpg)
Trace of Heapsort (cont.)
28
26 18
20 6 29 32
37 39 66 74 76 89
![Page 56: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/56.jpg)
Trace of Heapsort (cont.)
6
26 18
20 28 29 32
37 39 66 74 76 89
![Page 57: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/57.jpg)
Trace of Heapsort (cont.)
26
6 18
20 28 29 32
37 39 66 74 76 89
![Page 58: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/58.jpg)
Trace of Heapsort (cont.)
26
20 18
6 28 29 32
37 39 66 74 76 89
![Page 59: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/59.jpg)
Trace of Heapsort (cont.)
26
20 18
6 28 29 32
37 39 66 74 76 89
![Page 60: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/60.jpg)
Trace of Heapsort (cont.)
26
20 18
6 28 29 32
37 39 66 74 76 89
![Page 61: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/61.jpg)
Trace of Heapsort (cont.)
26
20 18
6 28 29 32
37 39 66 74 76 89
![Page 62: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/62.jpg)
Trace of Heapsort (cont.)
6
20 18
26 28 29 32
37 39 66 74 76 89
![Page 63: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/63.jpg)
Trace of Heapsort (cont.)
20
6 18
26 28 29 32
37 39 66 74 76 89
![Page 64: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/64.jpg)
Trace of Heapsort (cont.)
20
6 18
26 28 29 32
37 39 66 74 76 89
![Page 65: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/65.jpg)
Trace of Heapsort (cont.)
20
6 18
26 28 29 32
37 39 66 74 76 89
![Page 66: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/66.jpg)
Trace of Heapsort (cont.)
20
6 18
26 28 29 32
37 39 66 74 76 89
![Page 67: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/67.jpg)
Trace of Heapsort (cont.)
18
6 20
26 28 29 32
37 39 66 74 76 89
![Page 68: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/68.jpg)
Trace of Heapsort (cont.)
18
6 20
26 28 29 32
37 39 66 74 76 89
![Page 69: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/69.jpg)
Trace of Heapsort (cont.)
18
6 20
26 28 29 32
37 39 66 74 76 89
![Page 70: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/70.jpg)
Trace of Heapsort (cont.)
18
6 20
26 28 29 32
37 39 66 74 76 89
![Page 71: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/71.jpg)
Trace of Heapsort (cont.)
6
18 20
26 28 29 32
37 39 66 74 76 89
![Page 72: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/72.jpg)
Trace of Heapsort (cont.)
6
18 20
26 28 29 32
37 39 66 74 76 89
![Page 73: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/73.jpg)
Revising the Heapsort Algorithm
If we implement the heap as an array each element
removed will be placed at the end of the array, and
the heap part of the array decreases by one elementGoto
Initial Heap
![Page 74: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/74.jpg)
Algorithm for In-Place Heapsort
Algorithm for In-Place Heapsort
1. Build a heap by rearranging the elements in an unsorted array
2. while the heap is not empty
3. Remove the first item from the heap by swapping it with the last item in the heap and restoring the heap property
![Page 75: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/75.jpg)
Algorithm to Build a Heap Start with an array table of length table.length
Consider the first item to be a heap of one item
Next, consider the general case where the items in array table from 0 through n-1 form a heap and the items from n through table.length – 1 are not in the heap
![Page 76: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/76.jpg)
Algorithm to Build a Heap (cont.)
Refinement of Step 1 for In-Place Heapsort
1.1 while n is less than table.length
1.2 Increment n by 1. This inserts a new item into the heap
1.3 Restore the heap property
![Page 77: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/77.jpg)
Analysis of Heapsort
Because a heap is a complete binary tree, has log n levels
Building a heap of size n requires finding the correct location for an item in a heap with log n levels
Each insert (or remove) is O(log n) With n items, building a heap is O(n log
n) No extra storage is needed
![Page 78: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/78.jpg)
Code for Heapsort
Listing 8.7 (HeapSort.java, pages 449 -451)
![Page 79: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/79.jpg)
Section 8.9
Quicksort
![Page 80: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/80.jpg)
Quicksort
Developed in 1962 Quicksort selects a specific value called a
pivot and rearranges the array into two parts (called partioning) all the elements in the left subarray are less than
or equal to the pivot all the elements in the right subarray are larger
than the pivot The pivot is placed between the two subarrays
The process is repeated until the array is sorted
![Page 81: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/81.jpg)
Trace of Quicksort
44 75 23 43 55 12 64 77 33
![Page 82: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/82.jpg)
Trace of Quicksort (cont.)
44 75 23 43 55 12 64 77 33
Arbitrarily select the first element
as the pivot
![Page 83: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/83.jpg)
Trace of Quicksort (cont.)
55 75 23 43 44 12 64 77 33
Swap the pivot with the element in the middle
![Page 84: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/84.jpg)
Trace of Quicksort (cont.)
55 75 23 43 44 12 64 77 33
Partition the elements so that all values less than or equal to the pivot are to the left,
and all values greater than the pivot are to
the right
![Page 85: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/85.jpg)
Trace of Quicksort (cont.)
12 33 23 43 44 55 64 77 75
Partition the elements so that all values less than or equal to the pivot are to the left,
and all values greater than the pivot are to
the right
![Page 86: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/86.jpg)
Quicksort Example(cont.)
12 33 23 43 44 55 64 77 75
44 is now in its correct position
![Page 87: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/87.jpg)
Trace of Quicksort (cont.)
12 33 23 43 44 55 64 77 75
Now apply quicksort recursively to the
two subarrays
![Page 88: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/88.jpg)
Trace of Quicksort (cont.)
12 33 23 43 44 55 64 77 75
Pivot value = 12
![Page 89: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/89.jpg)
Trace of Quicksort (cont.)
12 33 23 43 44 55 64 77 75
Pivot value = 12
![Page 90: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/90.jpg)
Trace of Quicksort (cont.)
12 33 23 43 44 55 64 77 75
Pivot value = 33
![Page 91: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/91.jpg)
Trace of Quicksort (cont.)
12 23 33 43 44 55 64 77 75
Pivot value = 33
![Page 92: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/92.jpg)
Trace of Quicksort (cont.)
12 23 33 43 44 55 64 77 75
Pivot value = 33
![Page 93: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/93.jpg)
Trace of Quicksort (cont.)
12 23 33 43 44 55 64 77 75
Pivot value = 33
Left and right subarrays have
single values; they are sorted
![Page 94: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/94.jpg)
Trace of Quicksort (cont.)
12 23 33 43 44 55 64 77 75
Pivot value = 33
Left and right subarrays have
single values; they are sorted
![Page 95: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/95.jpg)
Trace of Quicksort (cont.)
12 23 33 43 44 55 64 77 75
Pivot value = 55
![Page 96: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/96.jpg)
Trace of Quicksort (cont.)
Pivot value = 64
12 23 33 43 44 55 64 77 75
![Page 97: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/97.jpg)
Trace of Quicksort (cont.)
12 23 33 43 44 55 64 77 75
Pivot value = 77
![Page 98: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/98.jpg)
Trace of Quicksort (cont.)
12 23 33 43 44 55 64 75 77
Pivot value = 77
![Page 99: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/99.jpg)
Trace of Quicksort (cont.)
12 23 33 43 44 55 64 75 77
Pivot value = 77
![Page 100: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/100.jpg)
Trace of Quicksort (cont.)
12 23 33 43 44 55 64 75 77
Left subarray has single value;
it is sorted
![Page 101: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/101.jpg)
Trace of Quicksort (cont.)
12 23 33 43 44 55 64 75 77
![Page 102: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/102.jpg)
Algorithm for Quicksort
We describe how to do the partitioning later The indexes first and last are the end
points of the array being sorted The index of the pivot after partitioning is pivIndex
Algorithm for Quicksort
1. if first < last then
2. Partition the elements in the subarray first . . . last so that the pivot value is in its correct place (subscript pivIndex)
3. Recursively apply quicksort to the subarray first . . . pivIndex - 1
4. Recursively apply quicksort to the subarray pivIndex + 1 . . . last
![Page 103: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/103.jpg)
Analysis of Quicksort
If the pivot value is a random value selected from the current subarray, then statistically half of the items in the subarray
will be less than the pivot and half will be greater If both subarrays have the same number of
elements (best case), there will be log n levels of recursion
At each recursion level, the partitioning process involves moving every element to its correct position—n moves
Quicksort is O(n log n), just like merge sort
![Page 104: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/104.jpg)
Analysis of Quicksort (cont.) The array split may not be the best case,
i.e. 50-50 An exact analysis is difficult (and beyond
the scope of this class), but, the running time will be bound by a constant x n log n
![Page 105: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/105.jpg)
Analysis of Quicksort (cont.) A quicksort will give very poor behavior
if, each time the array is partitioned, a subarray is empty.
In that case, the sort will be O(n2) Under these circumstances, the
overhead of recursive calls and the extra run-time stack storage required by these calls makes this version of quicksort a poor performer relative to the quadratic sorts We’ll discuss a solution later
![Page 106: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/106.jpg)
Code for Quicksort
Listing 8.8 (QuickSort.java, pages 453 - 454)
![Page 107: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/107.jpg)
Algorithm for Partitioning
44 75 23 43 55 12 64 77 33
If the array is randomly ordered, it does not matter which element is the pivot.
For simplicity we pick the element with subscript first
![Page 108: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/108.jpg)
Trace of Partitioning (cont.)
44 75 23 43 55 12 64 77 33
If the array is randomly ordered, it does not matter which element is the pivot.
For simplicity we pick the element with subscript first
![Page 109: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/109.jpg)
Trace of Partitioning (cont.)
44 75 23 43 55 12 64 77 33
For visualization purposes, items less than or equal to
the pivot will be colored blue; items greater than the
pivot will be colored light purple
![Page 110: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/110.jpg)
Trace of Partitioning (cont.)
44 75 23 43 55 12 64 77 33
For visualization purposes, items less than or equal to
the pivot will be colored light green; items greater
than the pivot will be colored light red
![Page 111: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/111.jpg)
Trace of Partitioning (cont.)
Search for the first value at the left end of the array that
is greater than the pivot value
44 75 23 43 55 12 64 77 33
![Page 112: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/112.jpg)
Trace of Partitioning (cont.)
Search for the first value at the left end of the array that
is greater than the pivot value
up
44 75 23 43 55 12 64 77 33
![Page 113: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/113.jpg)
Trace of Partitioning (cont.)
Then search for the first value at the right end of the
array that is less than or equal to the pivot value
up down
44 75 23 43 55 12 64 77 33
![Page 114: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/114.jpg)
Trace of Partitioning (cont.)
Exchange these values
up down
44 75 23 43 55 12 64 77 33
![Page 115: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/115.jpg)
Trace of Partitioning (cont.)
Exchange these values
44 33 23 43 55 12 64 77 75
![Page 116: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/116.jpg)
Trace of Partitioning (cont.)
Repeat
44 33 23 43 55 12 64 77 75
![Page 117: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/117.jpg)
Trace of Partitioning (cont.)
Find first value at left end greater than pivot
up
44 33 23 43 55 12 64 77 75
![Page 118: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/118.jpg)
Trace of Partitioning (cont.)
Find first value at right end less than or equal to pivot
up down
44 33 23 43 55 12 64 77 75
![Page 119: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/119.jpg)
Trace of Partitioning (cont.)
Exchange
44 33 23 43 12 55 64 77 75
![Page 120: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/120.jpg)
Trace of Partitioning (cont.)
Repeat
44 33 23 43 12 55 64 77 75
![Page 121: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/121.jpg)
Trace of Partitioning (cont.)
Find first element at left end greater than pivot
up
44 33 23 43 12 55 64 77 75
![Page 122: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/122.jpg)
Trace of Partitioning (cont.)
Find first element at right end less than or equal to
pivot
updown
44 33 23 43 12 55 64 77 75
![Page 123: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/123.jpg)
Trace of Partitioning (cont.)
Since down has "passed" up, do not exchange
updown
44 33 23 43 12 55 64 77 75
![Page 124: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/124.jpg)
Trace of Partitioning (cont.)
Exchange the pivot value with the value at down
updown
44 33 23 43 12 55 64 77 75
![Page 125: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/125.jpg)
Trace of Partitioning (cont.)
Exchange the pivot value with the value at down
down
12 33 23 43 44 55 64 77 75
![Page 126: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/126.jpg)
Trace of Partitioning (cont.)
The pivot value is in the correct position; return the value of down and assign it to the pivot index pivIndex
down
12 33 23 43 44 55 64 77 75
![Page 127: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/127.jpg)
Algorithm for Partitioning
![Page 128: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/128.jpg)
Code for partition
![Page 129: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/129.jpg)
Code for partition (cont.)
Listing 8.9 (QuickSort1, page 457)
![Page 130: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/130.jpg)
Revised Partition Algorithm Quicksort is O(n2) when each split yields
one empty subarray, which is the case when the array is presorted
A better solution is to pick the pivot value in a way that is less likely to lead to a bad split Use three references: first, middle, last Select the median of the these items as the
pivot
![Page 131: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/131.jpg)
Trace of Revised Partitioning
44 75 23 43 55 12 64 77 33
![Page 132: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/132.jpg)
Trace of Revised Partitioning (cont.)
44 75 23 43 55 12 64 77 33
middlefirst last
![Page 133: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/133.jpg)
Trace of Revised Partitioning (cont.)
44 75 23 43 55 12 64 77 33
Sort these values
middlefirst last
![Page 134: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/134.jpg)
Trace of Revised Partitioning (cont.)
33 75 23 43 44 12 64 77 55
Sort these values
middlefirst last
![Page 135: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/135.jpg)
Trace of Revised Partitioning (cont.)
33 75 23 43 44 12 64 77 55
Exchange middle with
first
middlefirst last
![Page 136: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/136.jpg)
Trace of Revised Partitioning (cont.)
44 75 23 43 33 12 64 77 55
Exchange middle with
first
middlefirst last
![Page 137: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/137.jpg)
Trace of Revised Partitioning (cont.)
44 75 23 43 33 12 64 77 55
Run the partition algorithm using the first element
as the pivot
![Page 138: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/138.jpg)
Algorithm for Revised partition Method
Algorithm for Revised partition Method
1. Sort table[first], table[middle], and table[last]
2. Move the median value to table[first] (the pivot value) by exchanging table[first] and table[middle].
3. Initialize up to first and down to last
4. do
5. Increment up until up selects the first element greater than the pivot value or up has reached last
6. Decrement down until down selects the first element less than or equal to the pivot value or down has reached first
7. if up < down then
8. Exchange table[up] and table[down]
9. while up is to the left of down
10. Exchange table[first] and table[down]
11. Return the value of down to pivIndex
![Page 139: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/139.jpg)
Code for Revised partition Method
Listing 8.10 (QuickSort2, page 459)
![Page 140: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/140.jpg)
Summary
Comparison of Sort Algorithms
![Page 141: Section 8.8 Heapsort. Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations Heapsort does not require any additional](https://reader036.vdocument.in/reader036/viewer/2022081513/56649d215503460f949f6130/html5/thumbnails/141.jpg)
Sort Review