-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLab9P1.cpp
52 lines (42 loc) · 888 Bytes
/
Lab9P1.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
#include<bits/stdc++.h>
using namespace std;
void maxheapify(int arr[],int i,int n){
int parent=i;
int left=(2*i)+1;
int right= (2*i)+2;
if(left<n && arr[left]>arr[parent]){
parent= left;
}
//else{parent=i;}
if(right<n && arr[right]>arr[parent]){
parent= right;
}
if(parent!=i){
int temp;
temp= arr[parent];
arr[parent]= arr[i];
arr[i]= temp;
maxheapify(arr,parent,n);
}
}
void buildheap(int arr[],int n){
int start= (n/2)-1;
int i;
for(i=start;i>=0;i--){
maxheapify(arr,i,n);
}
}
int main(){
int a[100];
cout<<"Number of Inputs:"<<endl;
int n;
cin>>n;
int i;
for(i=0;i<n;i++){
cin>>a[i];
}
buildheap(a,n);
for(i=0;i<n;i++){
cout<<a[i]<<" " ;
}
}