-
Notifications
You must be signed in to change notification settings - Fork 0
/
HeapSort.cpp
121 lines (102 loc) · 3.57 KB
/
HeapSort.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//referenced lecture/discussion slides and Stepik problem 9.1 resources
#include "HeapSort.h"
vector<int> HeapifyDown(vector<int>& arr, int root, int size){
//indices
int leftIndex = root * 2 + 1;
int rightIndex = root * 2 + 2;
int swapIndex = root;
//values
int leftChild = arr[(root * 2) + 1];
int rightChild = arr[(root * 2) + 2];
int swapValue = arr[root];
//check for largest left-wise and check if it's a valid child index
if(leftChild > swapValue && leftIndex < size){
swapValue = leftChild;
swapIndex = (root * 2) + 1;
}
//check for largest right-wise and check if it's a valid child index
if(rightChild > swapValue && rightIndex < size){
swapValue = rightChild;
swapIndex = (root * 2) + 2;
}
//if one of children is larger than root, swap them and heapify
if(swapValue != arr[root]){
int temp = arr[root];
arr[root] = swapValue;
arr[swapIndex] = temp;
HeapifyDown(arr,swapIndex, size);
}
return arr;
}
vector<pair<string,int>> HeapifyDown(vector<pair<string,int>>& arr, int root, int size){
//indices
int leftIndex = root * 2 + 1;
int rightIndex = root * 2 + 2;
int swapIndex = root;
//values
int leftChild = arr[(root * 2) + 1].second;
int rightChild = arr[(root * 2) + 2].second;
int swapValue = arr[root].second;
//check for largest left-wise and check if it's a valid child index
if(leftChild > swapValue && leftIndex < size){
swapValue = leftChild;
swapIndex = (root * 2) + 1;
}
//check for largest right-wise and check if it's a valid child index
if(rightChild > swapValue && rightIndex < size){
swapValue = rightChild;
swapIndex = (root * 2) + 2;
}
//if one of children is larger than root, swap them and heapify
if(swapValue != arr[root].second){
pair<string, int> temp = arr[root];
arr[root] = arr[swapIndex];
arr[swapIndex] = temp;
HeapifyDown(arr,swapIndex, size);
}
return arr;
}
//Average Time Complexity - O(n log n)
vector<int> HeapSort(vector<int>& numbers){
int size = numbers.size();
vector<int> sorted;
//1. build heap in place - O(n)
for(int i = size/2; i >= 0; i--){
HeapifyDown(numbers ,i, size);
}
int heapSize = size;
//loop through tree - O(log n)
for(int m = size - 1; m >= 0; m--){
//2. while heap size > 1, swap root with last element and reduce size by 1
int temp = numbers[0];
numbers[0] = numbers[m];
numbers[m] = temp;
heapSize--;
//3. Make sure heap is still a heap
numbers = HeapifyDown(numbers,0,heapSize);
//4.repeat step 2 - for loop
}
return numbers;
}
//USE THIS FUNCTION
vector<pair<string,int>> HeapSort(vector<pair<string,int>>& numbers){
int size = numbers.size();
vector<int> sorted;
//1. build heap in place - O(n)
for(int i = size/2; i >= 0; i--){
HeapifyDown(numbers ,i, size);
}
int heapSize = size;
//loop through tree - O(log n)
for(int m = size - 1; m >= 0; m--){
//2. while heap size > 1, swap root with last element and reduce size by 1
pair<string, int> temp = numbers[0];
numbers[0] = numbers[m];
numbers[m] = temp;
heapSize--;
//3. Make sure heap is still a heap
numbers = HeapifyDown(numbers,0,heapSize);
//4.repeat step 2 - for loop
}
return numbers;
}