-
Notifications
You must be signed in to change notification settings - Fork 0
/
heap_sort.cpp
255 lines (223 loc) · 5.31 KB
/
heap_sort.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Heap Sort =================================================================
void max_heap(int arr[], int n, int i)
{
// make largest element the root of tree
int largest = i;
// left child index is twice the parent's
int l = 2 * i;
// right child index is the parent's + 1
int r = 2 * i + 1;
// if left child exists in the array and is larger than root
if (l < n && arr[l] > arr[largest])
{
largest = l;
}
// if right child exists in the array and is larger than root
if (r < n && arr[r] > arr[largest])
{
largest = r;
}
// if current largest element is not the root
if (largest != i)
{
swap(arr[i], arr[largest]);
// then keep recursively heapifying until it is the largest
max_heap(arr, n, largest);
}
}
void min_heap(int arr[], int n, int i)
{
// make smallest element the root of tree
int smallest = i;
// left child index is twice the parent's
int l = 2 * i;
// right child index is the parent's
int r = 2 * i + 1;
// if left child exists in the array and is smaller than root
if (l < n && arr[l] < arr[smallest])
{
smallest = l;
}
// if right child exists in the array and is smaller than root
if (r < n && arr[r] < arr[smallest])
{
smallest = r;
}
// if current smallest element is not the root
if (smallest != i)
{
swap(arr[i], arr[smallest]);
// then keep recursively heapifying until it is the smallest
min_heap(arr, n, smallest);
}
}
void heapSortA(int arr[], int n)
{
// Build initial heap
for (int i = n / 2 - 1; i >= 0; i--)
{
max_heap(arr, n, i);
}
// until the loop goes thru all elements in the array
for (int i = n - 1; i >= 0; i--)
{
// move the max element to the back as its sorted
swap(arr[0], arr[i]);
// then heapify the rest of the array
max_heap(arr, i, 0);
}
}
void heapSortB(int arr[], int n)
{
// Build initial heap
for (int i = n / 2 - 1; i >= 0; i--)
{
min_heap(arr, n, i);
}
// until the loop goes thru all elements in the array
for (int i = n - 1; i >= 0; i--)
{
// move the min element to the back as its sorted
swap(arr[0], arr[i]);
// then heapify the rest of the array
min_heap(arr, i, 0);
}
}
// Versions with Steps===================================================
void max_heapS(int arr[], int n, int i)
{
steps++; // +1 to intialize variable
int largest = i;
steps++; // +1 to intialize variable
int l = 2 * i;
steps += 2; // +2 for variable assignment
int r = 2 * i + 1;
steps += 3; // +3 for variable assignment
if (l < n && r < n && arr[l] > arr[largest] && arr[r] > arr[largest])
{
if (arr[l] >= arr[r])
{
largest = l;
}
else
{
largest = r;
}
steps += 4; // +4 for if
}
else
{
if (l < n && arr[l] > arr[largest])
{
largest = l;
steps++; // +1 to intialize variable
}
steps += 2; // +2 for if statement
if (r < n && arr[r] > arr[largest])
{
largest = r;
steps++; // +1 to intialize variable
}
}
steps += 2; // +2 for if statement
if (largest != i)
{
swap(arr[i], arr[largest]);
steps += 3; // +3 for swap
max_heapS(arr, n, largest);
steps++; // +1 for function call
}
steps++; // +1 for if
}
void heapSortAS(int arr[], int n)
{
steps = 0;
steps++; // initialize step
for (int i = n / 2 - 1; i >= 0; i--)
{
steps += 3; // +3 for loop
max_heapS(arr, n, i);
steps++; // +1 for function call
}
steps++; // +1 for end of loop
for (int i = n - 1; i >= 0; i--)
{
steps += 3; // +3 for loop
swap(arr[0], arr[i]);
steps += 3; // +3 for swap
max_heapS(arr, i, 0);
steps++; // +1 for function call
}
steps++; // +1 for end of loop
cout << "heapSortAS steps:" << steps << endl;
}
void min_heapS(int arr[], int n, int i)
{
steps++; // +1 to intialize variable
int smallest = i;
steps++; // +1 to intialize variable
int l = 2 * i;
steps += 2; // +2 for variable assignment
int r = 2 * i + 1;
steps += 3; // +3 for variable assignment
if (l > n && r > n && arr[l] < arr[smallest] && arr[r] > arr[smallest])
{
if (arr[l] <= arr[r])
{
smallest = l;
steps++; // +1 to assign variable
}
else
{
smallest = r;
steps++; // +1 to assign variable
}
steps++; // +1 for if
}
else
{
if (l < n && arr[l] < arr[smallest])
{
smallest = l;
steps++; // +1 to assign variable
}
steps++; // +1 for if
if (r < n && arr[r] < arr[smallest])
{
smallest = r;
steps++; // +1 to intialize variable
}
steps += 2; // +2 for if
}
steps += 4; // +4 for if
if (smallest != i)
{
swap(arr[i], arr[smallest]);
steps += 3; // +3 for swap
steps++; // +1 for function call
min_heap(arr, n, smallest);
}
steps++; // +1 for if
}
void heapSortBS(int arr[], int n)
{
steps = 0;
steps++; // initialize step
for (int i = n / 2 - 1; i >= 0; i--)
{
steps += 3; // +3 for loop
min_heapS(arr, n, i);
steps++; // +1 for function call
}
steps++; // +1 for end of loop
for (int i = n - 1; i >= 0; i--)
{
steps += 3; // +3 for loop
swap(arr[0], arr[i]);
steps += 3; // +3 for swap
steps++; // +1 for function call
min_heapS(arr, i, 0);
}
steps++; // +1 for end of loop
cout << "heapSortBS steps:" << steps << endl;
}