-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathpriorityQueue_1.cpp
184 lines (151 loc) · 5.04 KB
/
priorityQueue_1.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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <iostream>
#include <vector>
using namespace std;
//priority queue is made out of complete binary tree but stores the elements in form of array
//or vector
class PriorityQueue
{
vector<int> pq;
public:
//constructor
PriorityQueue(){
}
bool isEmpty(){
return pq.size()==0;
}
int getSize(){
return pq.size();
}
int getMin(){
if(isEmpty())
return 0;
return pq[0];
}
//insert
void insert(int element){
pq.push_back(element); //sbse pehle element ko insert kr lenge vector me
int childIndex = pq.size()-1; //jis index pr push hua hai wo index calculate kr liye
while(childIndex>0){
int parentIndex = (childIndex-1)/2; //parent index calculte kr liye
//coz parent index pr jo element hai usi se compare krna hota hai ki
//element shi jagah hai ya swapping krna prega
if(pq[childIndex]<pq[parentIndex]){ //agar childindex wala element chhota hai
//means galat jagah hai hme use parent index wale element se swap krna hoga
int temp = pq[childIndex];
pq[childIndex]=pq[parentIndex];
pq[parentIndex]=temp;
}else{ //agar child element chhota nhi hai means swap krne ki jaroorat nhi
break;
}
childIndex=parentIndex;
}
}
//remove
int removeMin(){
if(isEmpty()){
return 0;
}
int ans=pq[0];
pq[0]=pq[pq.size()-1];
pq.pop_back();
//now maintaining the complete binary tree
int parentIndex = 0;
int leftChildIndex = 2*parentIndex+1;
int rightChildIndex = 2*parentIndex+2;
while(leftChildIndex<pq.size()){
int minIndex = parentIndex;
if(pq[minIndex]>pq[leftChildIndex]){
minIndex=leftChildIndex;
}
if(rightChildIndex<pq.size()&&pq[rightChildIndex]<pq[minIndex]){
minIndex=rightChildIndex;
}
if(minIndex==parentIndex){
break;
}
int temp = pq[minIndex];
pq[minIndex]=pq[parentIndex];
pq[parentIndex]=temp;
parentIndex=minIndex;
leftChildIndex = 2*parentIndex+1;
rightChildIndex = 2*parentIndex+2;
}
return ans;
}
};
//heap sort function
void inplaceHeapSort(int pq[],int n){
//sbse pehele input array se heap bana lenge
for(int i=1;i<n;i++){
int childIndex = i;
while(childIndex>0){
int parentIndex = (childIndex-1)/2; //parent index calculte kr liye
//coz parent index pr jo element hai usi se compare krna hota hai ki
//element shi jagah hai ya swapping krna prega
if(pq[childIndex]<pq[parentIndex]){ //agar childindex wala element chhota hai
//means galat jagah hai hme use parent index wale element se swap krna hoga
int temp = pq[childIndex];
pq[childIndex]=pq[parentIndex];
pq[parentIndex]=temp;
}else{ //agar child element chhota nhi hai means swap krne ki jaroorat nhi
break;
}
childIndex=parentIndex;
}
}
//now ek ek element ko heap se remove krenge to sorted order me bahar aayege
int size = n;
while(size>1){
int temp=pq[0];
pq[0]=pq[size-1];
pq[size-1]=temp;
size--;
int parentIndex = 0;
int leftChildIndex = 2*parentIndex+1;
int rightChildIndex = 2*parentIndex+2;
while(leftChildIndex < size){
int minIndex = parentIndex;
if(pq[minIndex] > pq[leftChildIndex]){
minIndex=leftChildIndex;
}
if(rightChildIndex < size && pq[rightChildIndex] < pq[minIndex]){
minIndex = rightChildIndex;
}
if(minIndex == parentIndex){
break;
}
int temp = pq[minIndex];
pq[minIndex] = pq[parentIndex];
pq[parentIndex] = temp;
parentIndex = minIndex;
leftChildIndex = 2 * parentIndex + 1;
rightChildIndex = 2 * parentIndex + 2;
}
}
}
int main()
{
cout<<"-------------Program started-------------"<<endl;
PriorityQueue p;
p.insert(10);
p.insert(30);
p.insert(50);
p.insert(80);
p.insert(40);
p.insert(20);
p.insert(60);
p.insert(70);
cout<<p.getSize()<<endl;
cout<<p.getMin()<<endl;
while(!p.isEmpty()){
cout<<"Element Removed: "<<p.removeMin()<<endl;
}
//inplace heap sort
int input[]={1,5,2,8,0};
inplaceHeapSort(input,5);
for(int i=0;i<5;i++){
cout<<input[i]<<" ";
}
cout<<endl;
return 0;
}