![Page 1: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/1.jpg)
1
CSC212 Data Structure - Section AB
CSC212 Data Structure - Section AB
Lecture 22
Recursive Sorting, Heapsort &
STL Quicksort
Instructor: Edgardo Molina
Department of Computer Science
City College of New York
![Page 2: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/2.jpg)
2
TopicsTopics
Recursive Sorting Algorithms Divide and Conquer technique
An O(NlogN) Sorting Alg. using a Heap making use of the heap properties
STL Sorting Functions C++ sort function Original C version of qsort
![Page 3: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/3.jpg)
3
The Divide-and-Conquer TechniqueThe Divide-and-Conquer Technique
Basic Idea: If the problem is small, simply solve it. Otherwise,
divide the problem into two smaller sub-problems, each of which is about half of the original problem
Solve each sub-problem, and then Combine the solutions of the sub-problems
![Page 4: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/4.jpg)
4
The Divide-and-Conquer Sorting ParadigmThe Divide-and-Conquer Sorting Paradigm
1. Divide the elements to be sorted into two groups of (almost) equal size
2. Sort each of these smaller groups of elements (by recursive calls)
3. Combine the two sorted groups into one large sorted list
![Page 5: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/5.jpg)
5
MergesortMergesort
Divide the array in the middle
Sort the two half-arrays by recursion
Merge the two halves
void mergesort(int data[ ], size_t n){ size_t n1; // Size of the first subarray size_t n2; // Size of the second subarray
if (n > 1) { // Compute sizes of the subarrays. n1 = n / 2; n2 = n - n1;
// Sort from data[0] through data[n1-1] mergesort(data, n1); // Sort from data[n1] to the end mergesort((data + n1), n2);
// Merge the two sorted halves. merge(data, n1, n2); }}
![Page 6: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/6.jpg)
6
Mergesort – an ExampleMergesort – an Example
16 12 7 6 3 2 18 10
[0] [1] [2] [3] [4] [5] [6] [7]
![Page 7: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/7.jpg)
7
Mergesort – an ExampleMergesort – an Example
2 3 6 7 10 12 16 18
16 12 7 6 3 2 18 10
?
![Page 8: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/8.jpg)
8
Mergesort – an ExampleMergesort – an Example
16 12 7 6 3 2 18 10
16 12 7 6 3 2 18 10divide
![Page 9: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/9.jpg)
9
Mergesort – an ExampleMergesort – an Example
16 12 7 6 3 2 18 10
16 12 7 6 3 2 18 10
16 12 7 6 3 2 18 10
divide
divide
![Page 10: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/10.jpg)
10
Mergesort – an ExampleMergesort – an Example
16 12 7 6 3 2 18 10
16 12 7 6 3 2 18 10
16 12 7 6 3 2 18 10
16 12 7 6 3 2 18 10
divide
divide
divide
![Page 11: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/11.jpg)
11
Mergesort – an ExampleMergesort – an Example
16 12 7 6 3 2 18 10
16 12 7 6 3 2 18 10
16 12 7 6 3 2 18 10
12 16 6 7 2 3 10 18
16 12 7 6 3 2 18 10
divide
divide
divide
merge
![Page 12: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/12.jpg)
12
Mergesort – an ExampleMergesort – an Example
16 12 7 6 3 2 18 10
16 12 7 6 3 2 18 10
16 12 7 6 3 2 18 10
12 16 6 7 2 3 10 18
16 12 7 6 3 2 18 10
6 7 12 16 2 3 10 18
divide
divide
divide
merge
merge
![Page 13: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/13.jpg)
13
Mergesort – an ExampleMergesort – an Example
16 12 7 6 3 2 18 10
16 12 7 6 3 2 18 10
16 12 7 6 3 2 18 10
12 16 6 7 2 3 10 18
16 12 7 6 3 2 18 10
6 7 12 16 2 3 10 18
2 3 6 7 10 12 16 18
divide
divide
divide
merge
merge
merge
![Page 14: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/14.jpg)
14
Mergesort – two issuesMergesort – two issues
Specifying a subarray with pointer arithmetic int data[10]; (data+i)[0] is the same of data[i] (data+i][1] is the same as data[i+1]
Merging two sorted subarrays into a sorted list need a temporary array (by new and then delete) step through the two sub-arrays with two cursors,
and copy the elements in the right order
![Page 15: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/15.jpg)
15
Mergesort - mergeMergesort - merge
6 7 12 16 2 3 10 18
? ? ? ? ? ? ? ?
data
temp
[0] [1] [2] [3] [4] [5] [6] [7]
[0] [1] [2] [3] [4] [5] [6] [7]
c1 c2
d
![Page 16: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/16.jpg)
16
Mergesort - mergeMergesort - merge
6 7 12 16 2 3 10 18
2 ? ? ? ? ? ? ?
data
temp
[0] [1] [2] [3] [4] [5] [6] [7]
[0] [1] [2] [3] [4] [5] [6] [7]
c1 c2
d
![Page 17: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/17.jpg)
17
Mergesort - mergeMergesort - merge
6 7 12 16 2 3 10 18
2 3 ? ? ? ? ? ?
data
temp
[0] [1] [2] [3] [4] [5] [6] [7]
[0] [1] [2] [3] [4] [5] [6] [7]
c1 c2
d
![Page 18: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/18.jpg)
18
Mergesort - mergeMergesort - merge
6 7 12 16 2 3 10 18
2 3 6 ? ? ? ? ?
data
temp
[0] [1] [2] [3] [4] [5] [6] [7]
[0] [1] [2] [3] [4] [5] [6] [7]
c1 c2
d
![Page 19: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/19.jpg)
19
Mergesort - mergeMergesort - merge
6 7 12 16 2 3 10 18
2 3 6 7 ? ? ? ?
data
temp
[0] [1] [2] [3] [4] [5] [6] [7]
[0] [1] [2] [3] [4] [5] [6] [7]
c1 c2
d
![Page 20: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/20.jpg)
20
Mergesort - mergeMergesort - merge
6 7 12 16 2 3 10 18
2 3 6 7 10 ? ? ?
data
temp
[0] [1] [2] [3] [4] [5] [6] [7]
[0] [1] [2] [3] [4] [5] [6] [7]
c1 c2
d
![Page 21: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/21.jpg)
21
Mergesort - mergeMergesort - merge
6 7 12 16 2 3 10 18
2 3 6 7 10 12 ? ?
data
temp
[0] [1] [2] [3] [4] [5] [6] [7]
[0] [1] [2] [3] [4] [5] [6] [7]
c1 c2
d
![Page 22: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/22.jpg)
22
Mergesort - mergeMergesort - merge
6 7 12 16 2 3 10 18
2 3 6 7 10 12 16 ?
data
temp
[0] [1] [2] [3] [4] [5] [6] [7]
[0] [1] [2] [3] [4] [5] [6] [7]
c1 c2
d
![Page 23: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/23.jpg)
23
Mergesort - mergeMergesort - merge
6 7 12 16 2 3 10 18
2 3 6 7 10 12 16 18
data
temp
[0] [1] [2] [3] [4] [5] [6] [7]
[0] [1] [2] [3] [4] [5] [6] [7]
c1 c2
d
![Page 24: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/24.jpg)
24
Mergesort - mergeMergesort - merge
2 3 6 7 10 12 16 18
data
temp
[0] [1] [2] [3] [4] [5] [6] [7]
[0] [1] [2] [3] [4] [5] [6] [7]
2 3 6 7 10 12 16 18
![Page 25: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/25.jpg)
25
Mergesort - mergeMergesort - merge
data
[0] [1] [2] [3] [4] [5] [6] [7]
2 3 6 7 10 12 16 18
![Page 26: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/26.jpg)
26
Mergesort – Time AnalysisMergesort – Time Analysis
The worst-case running time, the average-case running time and the best-case running time for mergesort are all O(n log n)
![Page 27: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/27.jpg)
27
Mergesort – an ExampleMergesort – an Example
16 12 7 6 3 2 18 10
16 12 7 6 3 2 18 10
16 12 7 6 3 2 18 10
12 16 6 7 2 3 10 18
16 12 7 6 3 2 18 10
6 7 12 16 2 3 10 18
2 3 6 7 10 12 16 18
divide
divide
divide
merge
merge
merge
![Page 28: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/28.jpg)
28
Mergesort – Time AnalysisMergesort – Time Analysis
At the top (0) level, 1 call to merge creates an array with n elements At the 1st level, 2 calls to merge creates 2 arrays, each with n/2 elements At the 2nd level, 4 calls to merge creates 4 arrays, each with n/4 elements At the 3rd level, 8 calls to merge creates 8 arrays, each with n/8 elements
At the dth level, 2d calls to merge creates 2d arrays, each with n/2d elements
Each level does total work proportional to n => c n, where c is a constant Assume at the dth level, the size of the subarrays is n/2d =1, which means all the
work is done at this level, therefore the number of levels d = log2 n
The total cost of the mergesort is c nd = c n log2 n therefore the Big-O is O(n log2 n)
![Page 29: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/29.jpg)
29
HeapsortHeapsort
Heapsort – Why a Heap? (two properties) Heapsort – How to? (two steps) Heapsort – How good? (time analysis)
![Page 30: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/30.jpg)
30
Heap DefinitionHeap Definition
A heap is a binary tree where the entries of the nodes can be compared with the less than operator of a strict weak ordering.
In addition, two rules are followed: The entry contained by the node is NEVER less
than the entries of the node’s children The tree is a COMPLETE tree.
![Page 31: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/31.jpg)
31
Why a Heap for Sorting?Why a Heap for Sorting?
Two properties The largest element is always at the root Adding and removing an entry from a heap is
O(log n)
![Page 32: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/32.jpg)
32
Heapsort – Basic IdeaHeapsort – Basic Idea
Step 1. Make a heap from elements add an entry to the heap one at a time reheapification upward n times – O(n log n)
Step 2. Make a sorted list from the heap Remove the root of the heap to a sorted list and Reheapification downward to re-organize into a
updated heap n times – O(n log n)
![Page 33: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/33.jpg)
33
Heapsort – Step 1: Make a HeapHeapsort – Step 1: Make a Heap
16 12 7 6 3 2 18 10
[0] [1] [2] [3] [4] [5] [6] [7]
add an entry to the heap one at a time
![Page 34: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/34.jpg)
34
Heapsort – Step 1: Make a HeapHeapsort – Step 1: Make a Heap
16 12 7 6 3 2 18 10
[0] [1] [2] [3] [4] [5] [6] [7]
add an entry to the heap one at a time
16
![Page 35: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/35.jpg)
35
Heapsort – Step 1: Make a HeapHeapsort – Step 1: Make a Heap
16 12 7 6 3 2 18 10
[0] [1] [2] [3] [4] [5] [6] [7]
add an entry to the heap one at a time
16
12
![Page 36: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/36.jpg)
36
Heapsort – Step 1: Make a HeapHeapsort – Step 1: Make a Heap
16 12 7 6 3 2 18 10
[0] [1] [2] [3] [4] [5] [6] [7]
add an entry to the heap one at a time
16
12 7
![Page 37: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/37.jpg)
37
Heapsort – Step 1: Make a HeapHeapsort – Step 1: Make a Heap
16 12 7 6 3 2 18 10
[0] [1] [2] [3] [4] [5] [6] [7]
add an entry to the heap one at a time
16
12 7
6
![Page 38: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/38.jpg)
38
Heapsort – Step 1: Make a HeapHeapsort – Step 1: Make a Heap
16 12 7 6 3 2 18 10
[0] [1] [2] [3] [4] [5] [6] [7]
add an entry to the heap one at a time
16
12 7
6 3
![Page 39: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/39.jpg)
39
Heapsort – Step 1: Make a HeapHeapsort – Step 1: Make a Heap
16 12 7 6 3 2 18 10
[0] [1] [2] [3] [4] [5] [6] [7]
add an entry to the heap one at a time
16
12 7
6 3 2
![Page 40: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/40.jpg)
40
Heapsort – Step 1: Make a HeapHeapsort – Step 1: Make a Heap
16 12 7 6 3 2 18 10
[0] [1] [2] [3] [4] [5] [6] [7]
add an entry to the heap one at a time
16
12 7
6 3 2 18
reheapification upward: push the out-of-place node upward
![Page 41: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/41.jpg)
41
Heapsort – Step 1: Make a HeapHeapsort – Step 1: Make a Heap
16 12 18 6 3 2 7 10
[0] [1] [2] [3] [4] [5] [6] [7]
add an entry to the heap one at a time
16
12 18
6 3 2 7
reheapification upward: push the out-of-place node upward
![Page 42: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/42.jpg)
42
Heapsort – Step 1: Make a HeapHeapsort – Step 1: Make a Heap
18 12 16 6 3 2 7 10
[0] [1] [2] [3] [4] [5] [6] [7]
add an entry to the heap one at a time
18
12 16
6 3 2 7
reheapification upward: push the out-of-place node upward until it is in the right place
![Page 43: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/43.jpg)
43
Heapsort – Step 1: Make a HeapHeapsort – Step 1: Make a Heap
18 12 16 6 3 2 7 10
[0] [1] [2] [3] [4] [5] [6] [7]
add an entry to the heap one at a time
18
12 16
6 3 2 7
10
reheapification upward: push the out-of-place node upward until it is in the right place
![Page 44: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/44.jpg)
44
Heapsort – Step 1: Make a HeapHeapsort – Step 1: Make a Heap
add an entry to the heap one at a time
18
12 16
10 3 2 7
6
reheapification upward: push the out-of-place node upward until it is in the right place
18 12 16 10 3 2 7 6
[0] [1] [2] [3] [4] [5] [6] [7]
![Page 45: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/45.jpg)
45
Heapsort – Step 1: Make a HeapHeapsort – Step 1: Make a Heap
A heap is created: it is saved in the original array- the tree on the right is only for illustration!
18
12 16
10 3 2 7
6
18 12 16 10 3 2 7 6
[0] [1] [2] [3] [4] [5] [6] [7]
Sorted???
![Page 46: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/46.jpg)
46
Heapsort – Step 2: Sorting from HeapHeapsort – Step 2: Sorting from Heap
18
12 16
10 3 2 7
6
18 12 16 10 3 2 7 6
[0] [1] [2] [3] [4] [5] [6] [7]
heap ->
sorted list from smallest to largest
Q: where is the largest entry?
![Page 47: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/47.jpg)
47
Heapsort – Step 2: Sorting from HeapHeapsort – Step 2: Sorting from Heap
18
12 16
10 3 2 7
6
18 12 16 10 3 2 7 6
[0] [1] [2] [3] [4] [5] [6] [7]
Idea: remove the root of the heap and place it in the sorted list
=> recall: how to remove the root?
![Page 48: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/48.jpg)
48
Heapsort – Step 2: Sorting from HeapHeapsort – Step 2: Sorting from Heap
6
12 16
10 3 2 7
6 12 16 10 3 2 7 18
[0] [1] [2] [3] [4] [5] [6] [7]
How to remove the root?
move the last entry in the root...
and for the sake of sorting, put the root entry in the “sorted side”
almost a heap... sorted side
![Page 49: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/49.jpg)
49
Heapsort – Step 2: Sorting from HeapHeapsort – Step 2: Sorting from Heap
6
12 16
10 3 2 7
6 12 16 10 3 2 7 18
[0] [1] [2] [3] [4] [5] [6] [7]
How to remove the root?
move the last entry in the root...
then reposition the out-of place node to update the heap
sorted sidealmost a heap...
![Page 50: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/50.jpg)
50
Heapsort – Step 2: Sorting from HeapHeapsort – Step 2: Sorting from Heap
16
12 6
10 3 2 7
16 12 6 10 3 2 7 18
[0] [1] [2] [3] [4] [5] [6] [7]
How to remove the root?
move the last entry in the root...
then reposition the out-of place node to update the heap
sorted side
reheapification downward
almost a heap...
![Page 51: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/51.jpg)
51
Heapsort – Step 2: Sorting from HeapHeapsort – Step 2: Sorting from Heap
16
12 7
10 3 2 6
16 12 7 10 3 2 6 18
[0] [1] [2] [3] [4] [5] [6] [7]
How to remove the root?
move the last entry in the root...
then reposition the out-of place node to update the heap
sorted side
reheapification downward
a heap in the unsorted side
![Page 52: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/52.jpg)
52
Heapsort – Step 2: Sorting from HeapHeapsort – Step 2: Sorting from Heap
16
12 7
10 3 2 6
16 12 7 10 3 2 6 18
[0] [1] [2] [3] [4] [5] [6] [7]
do the same thing again for the heap in the unsorted side until all the entries have been moved to the sorted side
sorted sidea heap in the unsorted side
![Page 53: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/53.jpg)
53
Heapsort – Step 2: Sorting from HeapHeapsort – Step 2: Sorting from Heap
6
12 7
10 3 2
6 12 7 10 3 2 16 18
[0] [1] [2] [3] [4] [5] [6] [7]
do the same thing again for the heap in the unsorted side until all the entries have been moved to the sorted side
sorted sidealmost a heap...
![Page 54: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/54.jpg)
54
Heapsort – Step 2: Sorting from HeapHeapsort – Step 2: Sorting from Heap
12
6 7
10 3 2
12 6 7 10 3 2 16 18
[0] [1] [2] [3] [4] [5] [6] [7]
do the same thing again for the heap in the unsorted side until all the entries have been moved to the sorted side
sorted sidealmost a heap...
![Page 55: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/55.jpg)
55
Heapsort – Step 2: Sorting from HeapHeapsort – Step 2: Sorting from Heap
12
10 7
6 3 2
12 10 7 6 3 2 16 18
[0] [1] [2] [3] [4] [5] [6] [7]
do the same thing again for the heap in the unsorted side until all the entries have been moved to the sorted side
sorted sidealmost a heap...
![Page 56: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/56.jpg)
56
Heapsort – Step 2: Sorting from HeapHeapsort – Step 2: Sorting from Heap
12
10 7
6 3 2
12 10 7 6 3 2 16 18
[0] [1] [2] [3] [4] [5] [6] [7]
do the same thing again for the heap in the unsorted side until all the entries have been moved to the sorted side
sorted sidea heap again!
![Page 57: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/57.jpg)
57
Heapsort – Step 2: Sorting from HeapHeapsort – Step 2: Sorting from Heap
2
10 7
6 3
2 10 7 6 3 12 16 18
[0] [1] [2] [3] [4] [5] [6] [7]
do the same thing again for the heap in the unsorted side until all the entries have been moved to the sorted side
sorted sidealmost a heap...
![Page 58: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/58.jpg)
58
Heapsort – Step 2: Sorting from HeapHeapsort – Step 2: Sorting from Heap
10
2 7
6 3
10 2 7 6 3 12 16 18
[0] [1] [2] [3] [4] [5] [6] [7]
do the same thing again for the heap in the unsorted side until all the entries have been moved to the sorted side
sorted sidealmost a heap...
![Page 59: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/59.jpg)
59
Heapsort – Step 2: Sorting from HeapHeapsort – Step 2: Sorting from Heap
10
6 7
2 3
10 6 7 2 3 12 16 18
[0] [1] [2] [3] [4] [5] [6] [7]
do the same thing again for the heap in the unsorted side until all the entries have been moved to the sorted side
sorted sidea heap again!
![Page 60: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/60.jpg)
60
Heapsort – Step 2: Sorting from HeapHeapsort – Step 2: Sorting from Heap
3
6 7
2
3 6 7 2 10 12 16 18
[0] [1] [2] [3] [4] [5] [6] [7]
do the same thing again for the heap in the unsorted side until all the entries have been moved to the sorted side
sorted sidealmost a heap...
![Page 61: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/61.jpg)
61
Heapsort – Step 2: Sorting from HeapHeapsort – Step 2: Sorting from Heap
7
6 3
2
7 6 3 2 10 12 16 18
[0] [1] [2] [3] [4] [5] [6] [7]
do the same thing again for the heap in the unsorted side until all the entries have been moved to the sorted side
sorted sidea heap again !
![Page 62: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/62.jpg)
62
Heapsort – Step 2: Sorting from HeapHeapsort – Step 2: Sorting from Heap
2
6 3
2 6 3 7 10 12 16 18
[0] [1] [2] [3] [4] [5] [6] [7]
do the same thing again for the heap in the unsorted side until all the entries have been moved to the sorted side
a heap ?? sorted side
![Page 63: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/63.jpg)
63
Heapsort – Step 2: Sorting from HeapHeapsort – Step 2: Sorting from Heap
6
2 3
6 2 3 7 10 12 16 18
[0] [1] [2] [3] [4] [5] [6] [7]
do the same thing again for the heap in the unsorted side until all the entries have been moved to the sorted side
sorted sidea heap !!
![Page 64: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/64.jpg)
64
Heapsort – Step 2: Sorting from HeapHeapsort – Step 2: Sorting from Heap
3
2
3 2 6 7 10 12 16 18
[0] [1] [2] [3] [4] [5] [6] [7]
do the same thing again for the heap in the unsorted side until all the entries have been moved to the sorted side
sorted sideheap !
![Page 65: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/65.jpg)
65
Heapsort – Step 2: Sorting from HeapHeapsort – Step 2: Sorting from Heap
2 2 3 6 7 10 12 16 18
[0] [1] [2] [3] [4] [5] [6] [7]
do the same thing again for the heap in the unsorted side until all the entries have been moved to the sorted side
sorted sideheap !
![Page 66: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/66.jpg)
66
Heapsort – Step 2: Sorting from HeapHeapsort – Step 2: Sorting from Heap
2 3 6 7 10 12 16 18
[0] [1] [2] [3] [4] [5] [6] [7]
do the same thing again for the heap in the unsorted side until all the entries have been moved to the sorted side
sorted side
DONE!
![Page 67: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/67.jpg)
67
Heapsort – Time AnalysisHeapsort – Time Analysis
Step 1. Make a heap from elements add an entry to the heap one at a time reheapification upward n times – O(n log n)
Step 2. Make a sorted list from the heap Remove the root of the heap to a sorted list and Reheapification downward to re-organize the unsorted
side into a updated heap do this n times – O(n log n)
The running time is O(n log n)
![Page 68: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/68.jpg)
68
C++ STL Sorting FunctionsC++ STL Sorting Functions
The C++ sort function void sort(Iterator begin, Iterator end);
The original C version of qsortvoid qsort(
void *base,
size_t number_of_elements,
size_t element_size,
int compare(const void*, const void*)
);
![Page 69: 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City](https://reader036.vdocuments.us/reader036/viewer/2022062409/5697bf841a28abf838c86a96/html5/thumbnails/69.jpg)
69
Summary & HomeworkSummary & Homework
Recursive Sorting Algorithms Divide and Conquer technique
An O(NlogN) Sorting Alg. using a Heap making use of the heap properties
STL Sorting Functions C++ sort function Original C version of qsort