-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeap.cpp
79 lines (69 loc) · 1.46 KB
/
Heap.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include<iostream>
using namespace std;
#define capacity 100005
int minheap[capacity];
int size = 0;
void myswap(int *a,int *b){
int temp=*a;
*a = *b;
*b = temp;
}
void bubble_up(int i){
// replace current index with parent if current index is smaller than the parent
int p = (i-1)/2;
if(minheap[i]<minheap[p]){
myswap(&minheap[i],&minheap[p]);
bubble_up(p);
}
}
void insert(int key){
minheap[size++]=key;
int index = size-1;
bubble_up(index);
}
void minheapify(int pi){
// now compare it with other elements;
int l = 2*pi + 1;
int r = 2*pi + 2;
int smallest = pi;
if(l<size && minheap[l]<minheap[smallest])
smallest = l;
if(r<size && minheap[r]<minheap[smallest])
smallest = r;
if(smallest!=pi){
myswap(&minheap[smallest],&minheap[pi]);
minheapify(smallest);
}
}
int extract_min(){
int ans;
if(size>0){
ans = minheap[0];
minheap[0]=minheap[size-1];
size--;
minheapify(0);
}
return ans;
}
void heapsort(int arr[],int n){
//size = n
for(int i=0;i<n;i++){
insert(arr[i]);
}
for(int i=0;i<n;i++){
arr[i]= extract_min();
}
}
void print(int arr[],int n){
for(int i=0;i<n;i++){
cout<<arr[i]<<" ";
}
cout<<"\n";
}
int main(){
int arr[] = {1,0,-1,-3,-4,5,99,2,1,2};
int n = sizeof(arr)/sizeof(int);
heapsort(arr,n);
print(arr,n);
return 0;
}