-
Notifications
You must be signed in to change notification settings - Fork 0
/
HeapSort.cpp
71 lines (50 loc) · 1.41 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
#include<iostream>
using namespace std;
void maxHeapify(int arr[],int n,int i){
int curr_idx = i;
int leftIndex = 2*i + 1 ;
int rightIndex = 2*i + 2 ;
if(leftIndex < n && arr[leftIndex] > arr[curr_idx]){
curr_idx = leftIndex;
}
if(rightIndex < n && arr[rightIndex] > arr[curr_idx]){
curr_idx = rightIndex;
}
if(curr_idx!=i){
// curr_index got updated ,
// now the current index reached to correct place where the element at index i has to move
// Swap them
swap(arr[i],arr[curr_idx]);
// Recursively heapify the affected sub-tree
maxHeapify(arr,n,curr_idx);
}
}
void heapSort(int arr[],int size){
int n = size;
// Build a max heap from the array
for(int i=(n/2-1);i>=0;i--){
maxHeapify(arr,n,i);
}
while(n>1){
// Swap first and last element
swap(arr[0],arr[n-1]);
// now decrement the size of arr
n--;
// Now move the root node(first element to its correct place by heapifying it)
maxHeapify(arr,n,0);
}
}
void print(int arr[],int size){
for(int i=0;i<size;i++){
cout << arr[i] << " " ;
}cout << endl;
}
int main(){
int arr[] = { 12, 11, 13, 5, 6, 7 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function call
heapSort(arr, N);
cout << "Sorted array is \n";
print(arr, N);
return 0;
}