You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: Heapsort.md
+3-166
Original file line number
Diff line number
Diff line change
@@ -163,7 +163,7 @@ The code below shows the operation.
163
163
heapify(arr, i, 0);
164
164
}
165
165
166
-
Performance
166
+
Performance:-
167
167
168
168
Heap Sort has O(nlogn) time complexities for all the cases ( best case, average case and worst case).
169
169
@@ -185,169 +185,6 @@ Application of Heap Sort
185
185
Systems concerned with security and embedded system such as Linux Kernel uses Heap Sort because of the O(n log n) upper bound on Heapsort's running time and constant O(1) upper bound on its auxiliary storage.
186
186
187
187
Although Heap Sort has O(n log n) time complexity even for worst case, it doesn’t have more applications ( compared to other sorting algorithms like Quick Sort, Merge Sort ). However, its underlying data structure, heap, can be efficiently used if we want to extract smallest (or largest) from the list of items without the overhead of keeping the remaining items in the sorted order. For e.g Priority Queues.
188
-
Heap Sort Implementation in different Programming Languages
189
-
C++ Implementation
190
-
191
-
// C++ program for implementation of Heap Sort
192
-
#include <iostream>
193
-
using namespace std;
194
-
void heapify(int arr[], int n, int i)
195
-
{
196
-
// Find largest among root, left child and right child
197
-
int largest = i;
198
-
int l = 2*i + 1;
199
-
int r = 2*i + 2;
200
-
if (l < n && arr[l] > arr[largest])
201
-
largest = l;
202
-
if (right < n && arr[r] > arr[largest])
203
-
largest = r;
204
-
// Swap and continue heapifying if root is not largest
205
-
if (largest != i)
206
-
{
207
-
swap(arr[i], arr[largest]);
208
-
heapify(arr, n, largest);
209
-
}
210
-
}
211
-
// main function to do heap sort
212
-
void heapSort(int arr[], int n)
213
-
{
214
-
// Build max heap
215
-
for (int i = n / 2 - 1; i >= 0; i--)
216
-
heapify(arr, n, i);
217
-
// Heap sort
218
-
for (int i=n-1; i>=0; i--)
219
-
{
220
-
swap(arr[0], arr[i]);
221
-
222
-
// Heapify root element to get highest element at root again
223
-
heapify(arr, i, 0);
224
-
}
225
-
}
226
-
void printArray(int arr[], int n)
227
-
{
228
-
for (int i=0; i<n; ++i)
229
-
cout << arr[i] << " ";
230
-
cout << "\n";
231
-
}
232
-
int main()
233
-
{
234
-
int arr[] = {1,12,9,5,6,10};
235
-
int n = sizeof(arr)/sizeof(arr[0]);
236
-
heapSort(arr, n);
237
-
cout << "Sorted array is \n";
238
-
printArray(arr, n);
239
-
}
240
-
241
-
Java program for implementation of Heap Sort
242
-
243
-
// Java program for implementation of Heap Sort
244
-
public class HeapSort
245
-
{
246
-
247
-
public void sort(int arr[])
248
-
{
249
-
int n = arr.length;
250
-
251
-
// Build max heap
252
-
for (int i = n / 2 - 1; i >= 0; i--) {
253
-
heapify(arr, n, i);
254
-
}
255
-
256
-
257
-
// Heap sort
258
-
for (int i=n-1; i>=0; i--)
259
-
{
260
-
int temp = arr[0];
261
-
arr[0] = arr[i];
262
-
arr[i] = temp;
263
-
264
-
// Heapify root element
265
-
heapify(arr, i, 0);
266
-
}
267
-
}
268
-
269
-
void heapify(int arr[], int n, int i)
270
-
{
271
-
// Find largest among root, left child and right child
272
-
int largest = i;
273
-
int l = 2*i + 1;
274
-
int r = 2*i + 2;
275
-
276
-
if (l < n && arr[l] > arr[largest])
277
-
largest = l;
278
-
279
-
if (r < n && arr[r] > arr[largest])
280
-
largest = r;
281
-
282
-
// Swap and continue heapifying if root is not largest
283
-
if (largest != i)
284
-
{
285
-
int swap = arr[i];
286
-
arr[i] = arr[largest];
287
-
arr[largest] = swap;
288
-
289
-
heapify(arr, n, largest);
290
-
}
291
-
}
292
-
293
-
static void printArray(int arr[])
294
-
{
295
-
int n = arr.length;
296
-
for (int i=0; i < n; ++i)
297
-
System.out.print(arr[i]+" ");
298
-
System.out.println();
299
-
}
300
-
301
-
public static void main(String args[])
302
-
{
303
-
int arr[] = {1,12,9,5,6,10};
304
-
HeapSort hs = new HeapSort();
305
-
hs.sort(arr);
306
-
307
-
System.out.println("Sorted array is");
308
-
printArray(arr);
309
-
}
310
-
}
311
-
312
-
Python program for implementation of heap sort (Python 3)
313
-
314
-
def heapify(arr, n, i):
315
-
# Find largest among root and children
316
-
largest = i
317
-
l = 2 * i + 1
318
-
r = 2 * i + 2
319
-
320
-
if l < n and arr[i] < arr[l]:
321
-
largest = l
322
-
323
-
if r < n and arr[largest] < arr[r]:
324
-
largest = r
325
-
326
-
# If root is not largest, swap with largest and continue heapifying
327
-
if largest != i:
328
-
arr[i],arr[largest] = arr[largest],arr[i]
329
-
heapify(arr, n, largest)
330
-
331
-
def heapSort(arr):
332
-
n = len(arr)
333
-
334
-
# Build max heap
335
-
for i in range(n, 0, -1):
336
-
heapify(arr, n, i)
337
-
338
-
339
-
for i in range(n-1, 0, -1):
340
-
# swap
341
-
arr[i], arr[0] = arr[0], arr[i]
342
-
343
-
#heapify root element
344
-
heapify(arr, i, 0)
345
-
346
-
arr = [ 12, 11, 13, 5, 6, 7]
347
-
heapSort(arr)
348
-
n = len(arr)
349
-
350
-
print ("Sorted array is")
351
-
for i in range(n):
352
-
print ("%d" %arr[i])
353
188
189
+
#The above algorithm for Heap Sort i.e. the Heap Sorting Algorithm is proposed by Argho Chakraborty!!!
0 commit comments