This repository has been archived by the owner on Oct 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
Copy pathHeapsort_PriQ.cpp
167 lines (149 loc) · 3.11 KB
/
Heapsort_PriQ.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
@uthor : Ankit Singh
//This is a cpp file that contains max priority queue & heapsort programs both.
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define size 10 //This is the size of heap created.Increase, if required
int heapsize=0;
using namespace std;
void Max_heapify(int A[],int i)
{
int left=(2*i)+1,right=(2*i)+2,largest,temp;
if(left<heapsize && A[left]>A[i])
largest=left;
else
largest=i;
if(right<heapsize && A[right]>A[largest])
largest=right;
if(largest!=i)
{
temp=A[i];
A[i]=A[largest];
A[largest]=temp;
Max_heapify(A,largest);
}
}
void Build_maxheap(int A[])
{
heapsize=size;
int i=(size-2)/2;
while(i>=0)
{
Max_heapify(A,i);
i--;
}
}
int heapmax(int A[])
{
return A[0];
}
int extractmax(int A[])
{
if(heapsize<=0)
{
printf("Heap Underflow\n");
//return ;
}
else
{
int max=A[0];
A[0]=A[heapsize-1];
heapsize--;
Max_heapify(A,0);
return max;
}
}
void heapinsert(int A[],int key)
{
int temp;
heapsize++;
int i=heapsize-1;
A[i]=key;
while(A[(i-1)/2]<A[i] && i>0)
{
temp=A[(i-1)/2];
A[(i-1)/2]=A[i];
A[i]=temp;
i=(i-1)/2 ;
}
}
void display(int A[])
{
if(heapsize==0)
cout<<"Priority Queue Empty\n";
else
{
cout<<"Priority Queue:\n";
for(int i=0;i<heapsize;i++)
{
cout<<A[i] <<" ";
}
cout<<endl;
}
}
void heapsort(int A[])
{
if(size<1)
cout<<"Array underflow"<<endl;
else
{
int temp;
Build_maxheap(A);
for(int i=size-1;i>0;i--)
{
temp=A[0];
A[0]=A[i];
A[i]=temp;
heapsize--;
Max_heapify(A,0);
}
}
}
/*
int main()
{
//Uncomment this main() & comment the later one to run the heapsort program.
srand(time(NULL));
int A[size];
for(int i=0;i<size;i++)
A[i]=rand()%100; //use cin operator if u want to enter your own values instead of random numbers
cout<<"Array:\n";
for(int i=0;i<size;i++)
cout<<A[i]<<" ";
cout<<endl;
heapsort(A);
cout<<"Sorted Array:\n";
for(int i=0;i<size;i++)
cout<<A[i]<<" ";
cout<<endl;
}*/
int main()
{
int ch,x,Q[size];
while(1)
{
cout<<"--------------------\n";
cout<<"Menu\n1.Enqueue an element\n2.Extract maximum element\n3.Display Queue\nANY OTHER NUMBER TO EXIT\n";
cout<<"--------------------\n";
cin>>ch;
if(ch>3 || ch<=0)
break;
switch(ch)
{
case 1:
cout<<"Enter the priority value of the element\n";
cin>>x;
heapinsert(Q,x);
break;
case 2:
x=extractmax(Q);
cout<<"Max Priority element="<<x<<endl;
break;
case 3:
display(Q);
break;
default:;
}
}
}