-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheapSort.cpp
More file actions
42 lines (42 loc) · 761 Bytes
/
heapSort.cpp
File metadata and controls
42 lines (42 loc) · 761 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
34
35
36
37
38
39
40
41
42
#include<bits/stdc++.h>
using namespace std;
void heapify(int arr[],int n,int i){ //maxHeapify
int l=i*2+1;
int r=l+1;
int index=i,tmp;
if(r<n && arr[r]>arr[index]){
index=r;
}
if(l<n && arr[l]>arr[index]){
index=l;
}
if(index!=i) {
tmp=arr[index];
arr[index]=arr[i];
arr[i]=tmp;
heapify(arr,n,index);
}
}
void heapSort(int arr[],int n){
for(int i=n>>1;i--;){
heapify(arr,n,i);
}
int tmp;
for(int i=n-1;i>0;i--){
tmp=arr[i];
arr[i]=arr[0];
arr[0]=tmp;
heapify(arr,i,0);
}
}
int arr[1000006];
int main(){
int n;
memset(arr,0,sizeof arr);
//enter no of elements
cin>>n;
//enter elements
for(int i=n;i--;) cin>>arr[i];
heapSort(arr,n);
for(int i=0;i<n;i++) cout<<arr[i]<<" ";
}