-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHeapSort.cpp
More file actions
33 lines (31 loc) · 862 Bytes
/
HeapSort.cpp
File metadata and controls
33 lines (31 loc) · 862 Bytes
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
#include<bits/stdc++.h>
using namespace std;
template<typename T>
void maxHeapify(T heap[] , int size,int perant = 0) {
int leftChild = 2*perant+1,rightChild = 2*perant+2,current;
while(leftChild<size){
if(heap[leftChild]<heap[rightChild] and rightChild < size){
current=rightChild;
}else{
current= leftChild;
}
if(heap[perant]<heap[current]){
swap(heap[perant],heap[current]);
perant=current;
leftChild=2*perant+1;
rightChild=2*perant+2;
}else{
break;
}
}
}
template<typename T>
void heapSort(T arr[],int size){
for(int perant = (size/2)-1; perant >=0 ; perant--){
maxHeapify(arr,size,perant);
}
for (int i = size-1; i >=0 ; --i) {
swap(arr[0],arr[i]);
maxHeapify(arr,i);
}
}