-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheap2.cpp
More file actions
68 lines (66 loc) · 1.21 KB
/
heap2.cpp
File metadata and controls
68 lines (66 loc) · 1.21 KB
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
#include<iostream>
#include<vector>
using namespace std;
void maxhe(vector<int>&arr,int n,int i){
int largest=i;
int left=2*i+1;
int right=2*i+2;
if(left<n && arr[left]>arr[largest]){
largest=left;
}
else if(right<n && arr[right]>arr[largest]){
largest=right;
}
if(largest!=i){
swap(arr[i],arr[largest]);
maxhe(arr,n,largest);
}
}
void minhe(vector<int>&arr,int n,int i){
int smallest=i;
int left=2*i+1;
int right=2*i+2;
if(left<n && arr[left]<arr[smallest]){
smallest=left;
}
else if(right<n && arr[right]<arr[smallest]){
smallest=right;
}
if(smallest!=i){
swap(arr[i],arr[smallest]);
minhe(arr,n,smallest);
}
}
void heapsort(vector<int>&arr ,int n){
for(int i=n/2-1;i>=0;i--)
maxhe(arr,n,i);
for(int i=n-1;i>0;i--){
swap(arr[0],arr[i]);
maxhe(arr,i,0);
}
}
int main(){
int n;
cout<<"enter a number of students:";
cin>>n;
vector<int>marks(n);
cout<<"enter a marks:";
for(int i=0;i<n;i++){
cin>>marks[i];
}
vector<int>maxheap=marks;
vector<int>minheap=marks;
for(int i=n/2-1;i>=0;i--){
maxhe(maxheap,n,i);
}
for(int i=n/2-1;i>=0;i--){
minhe(minheap,n,i);
}
cout<<"maximum marks:"<<maxheap[0]<<endl;
cout<<"minimum marks:"<<minheap[0]<<endl;
heapsort(marks,n);
cout<<"sorted marks:";
for(int mark:marks)
cout<<mark<<" ";
return 0;
}