-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeap.cpp
More file actions
153 lines (120 loc) · 3.52 KB
/
Heap.cpp
File metadata and controls
153 lines (120 loc) · 3.52 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <bits/stdc++.h>
using namespace std;
#define vii vector<int>
int i;
/*
priority_queue <int, vii > max_heap;
priority_queue <int, vii, greater<int> > min_heap;
void insert(int x){
if (min_heap.size() == max_heap.size()){
//Base Case:
if(! max_heap.size() ){
max_heap.push(x);
return;
}
if(x < max_heap.top()) max_heap.push(x);
else min_heap.push(x);
}
else if (min_heap.size() < max_heap.size()) {
if(x >= max_heap.size()) min_heap.push(x);
else {
min_heap.push(max_heap.top());
max_heap.pop();
max_heap.push(x);
}
}
else {
if(x >= min_heap.size()) max_heap.push(x);
else {
max_heap.push(min_heap.top());
min_heap.pop();
min_heap.push(x);
}
}
}
float findMedian()
{
if(min_heap.size() == max_heap.size()) return (float)(max_heap.top() + min_heap.top())/2 ;
if(min_heap.size() > max_heap.size()) return min_heap.top();
return max_heap.top();
}
*/
void heapify(vector<int> &arr, int n, int i){//heapify kis aaray me karn ahai uska size , kahan se karna HAI
int maxIdx = i; //maxidx ko intialize kiya i se ,hence start point se
int l = 2*i + 1; //left element of a parent node ,exists at 2i+1 th index
int r = 2*i + 2; //right exists at 2i+2 nd node
if(l<n && arr[l] > arr[maxIdx]){//ahar left child , upar wale se bada hai toh update kar denge
maxIdx = l;
}
if(r<n && arr[r] > arr[maxIdx]){//same goes for right too
maxIdx = r;
}
if(maxIdx != i){ //upar wali iterattions khatam hone ke baad agar maxidx i ke baharbar nahi hai ,toh swap karenge
swap(arr[i], arr[maxIdx]);
heapify(arr, n, maxIdx); //phir naye mile tree pe vapis heapify laga denge
}
}
void heapSort(vector<int> &arr){
int n = arr.size();
// for(int i=n/2-1; i>=0; i--){//i=n/2-1 , 1st node ke element ka index de dega
// heapify(arr, n, i);//waha pe heapify call kar dunga last element n se le kar 1st i tak ke liye , hence 1st node se
// }//aur n/2-1 -- hai toh uske further neeche
heapify(arr,n,0);
for(int i=n-1; i>0; i--){// sare vec ke indexes me se
swap(arr[0], arr[i]);//swap kar do arr[0] and last wale
heapify(arr, i, 0);//bache hue pe heapify call kar do
}
}
/*class Node{
public:
int key;
int m;
Node** next;
void decide(int mm){ // To decide the number of Children
m = mm;
next = new Node* [m];
for(i=0;i<m; i++) next[i] = NULL ;
}
Node(int val){
key = val;
for(i=0;i<m; i++) next[i] = NULL ;
}
Node( int val, int mm){
decide(mm);
key = val;
for(i=0;i<m; i++) next[i] = NULL ;
}
};
Node* hinsert(Node* head, int v){
int size = head->m;
Node* h = new Node(v,size);
if(!head){
return h;
}
for(i=0; i<size; i++){
if(head->next[i]);
}
}
*/
int main(){
int n;
// while(true){
cin>>n;
// insert(n);
// cout << findMedian();
// }
vector<int>arr(n);
for(int i=0; i<n; i++){
cin>>arr[i];
}
heapSort(arr);
for(int i=0; i<n; i++){
cout<<arr[i]<<" ";
}
// int n,k;
// cin>>k>>n;
// Node *head = new Node(k,n);
// cout<< head->key ;
// vector<int> arr(n);
return 0;
}