-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriorityqueue.cpp
More file actions
54 lines (46 loc) · 1.05 KB
/
priorityqueue.cpp
File metadata and controls
54 lines (46 loc) · 1.05 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
#include<bits/stdc++.h>
using namespace std;
/***************************************ORIGINAL aUTHOR*******************************
*************************************************************************************
*************************************************************************************
*************astrainL3gi0N**********************************************************/
int n;
void swim(int arr[], int k){
while(k > 0){
if(arr[k] > arr[k/2]){
int temp = arr[k];arr[k] = arr[k/2]; arr[k/2] = temp;
}
else break;
k = k/2;
}
}
void sink(int arr[],int k = 1){
while(k <= n/2){
int j = 2*k;
j = arr[j] > arr[j+1]? j:j+1;
if(arr[k] < arr[j]){
int temp = arr[k];arr[k] = arr[j];arr[j] = temp;
}
else break;
k = j;
}
}
int delheap(int arr[]){
int val = arr[1];
arr[1] = arr[n--];
sink(arr);
return val;
}
int main()
{
cin>>n;
int arr[1000];
for(int i = 1; i <= n; ++i){
cin>>arr[i];
swim(arr,i);//insertion in heap
}
for(int i = 1; i <= n; ++i)
cout<<arr[i]<<' ';
cout<<'\n'<<delheap(arr)<<'\n';
return 0;
}