-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathheap.cpp
182 lines (169 loc) · 4.72 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
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
#include<bits/stdc++.h>
using namespace std;
void max_heapify(int b[], int n, int i) {
int p=i;
int l=2*i+1;
int r=2*i+2;
if(l<n && b[l]>b[p]){
p=l;
}
if(r<n && b[r]>b[p]){
p=r;
}
if(p!=i){
int temp=b[i];
b[i]=b[p];
b[p]=temp;
max_heapify(b, n, p);
}
}
void min_heapify(int b[], int n, int i){
int p=i;
int l=2*i+1;
int r=2*i+2;
if(l<n && b[l]<b[p]){
p=l;
}
if(r<n && b[r]<b[p]){
p=r;
}
if(p!=i){
int temp=b[i];
b[i]=b[p];
b[p]=temp;
min_heapify(b, n, p);
}
}
void heapSort(int b[], int n){
int c;
cout<<"\nSort using \n1)Maxheap\n2)Minheap\n";
cin>>c;
switch(c){
case 1: for (int i=n/2-1;i>=0;i--)
max_heapify(b, n, i);
for (int i=n-1;i>=0;i--){
int temp=b[0];
b[0]=b[i];
b[i]=temp;
max_heapify(b, i, 0);
}
break;
case 2: for (int i=n/2-1;i>=0;i--)
min_heapify(b, n, i);
for (int i=n-1;i>=0;i--){
int temp=b[0];
b[0]=b[i];
b[i]=temp;
min_heapify(b, i, 0);
}
break;
default:cout<<"Enter valid case\n";
break;
}
}
void max_buildHeap(int b[], int n) {
for(int i=(n/2)-1;i>= 0;i--){
max_heapify(b, n, i);
}
}
void min_buildHeap(int b[], int n){
for(int i=(n/2)-1;i>= 0;i--){
min_heapify(b, n, i);
}
}
void printHeap(int b[], int n) {
cout<<"The array representation is:\n ";
for (int i=0;i<n;++i){
cout<<b[i]<<"\t";
}
cout<<endl;
}
void insert_maxheap(int arr[], int n, int value){
int i;
i=n;
while(i>0){
int parent=(int)(i/2);
if(arr[parent]<arr[i]){
int temp=arr[parent];
arr[parent]=arr[i];
arr[i]=temp; i=parent;
}
else
return ;
}
}
void insert_minheap(int arr[], int n, int value){
int i;
i=n;
while(i>0){
int parent=(int)(i/2);
if(arr[parent]>arr[i]){
int temp=arr[parent];
arr[parent]=arr[i];
arr[i]=temp; i=parent;
}
else
return ;
}
}
int main(){
int b[]={2,7,26,25,19,17,1,90,3,36};
int n=sizeof(b)/sizeof(b[0]);
int arr[10],i,c,x,a;
while(1){
cout<<"-------------------------------------\n";
cout<<"\n1)Create Maxheap\n2)Create Minheap\n3)Convert an array into Maxheap";
cout<<"\n4)Convert an array into Minheap\n5)Heapsort\n6)Exit\t";
cin>>a;
cout<<"\n\n";
switch(a){
case 1: i=0;
c=0;
cout<<"Enter Number of elements for maxheap: ";
cin>>x;
while(x--){
cin>>arr[i];
insert_maxheap(arr,c,arr[i]);
i++;
c++;
}
cout<<"Representation : ";
for(i=0;i<c;i++)
cout<<arr[i]<<"\t";
cout<<endl<<endl;
break;
case 2: i=0;
c=0;
cout<<"Enter Number of elements for minheap: ";
cin>>x;
while(x--){
cin>>arr[i];
insert_minheap(arr,c,arr[i]);
i++;
c++;
}
cout<<"Representation : ";
for(i=0;i<c;i++)
cout<<arr[i]<<"\t";
cout<<endl<<endl;
break;
case 3: max_buildHeap(b, n);
printHeap(b, n);
cout<<endl<<endl;
break;
case 4: min_buildHeap(b, n);
printHeap(b, n);
cout<<endl<<endl;
break;
case 5: heapSort(b, n);
printHeap(b, n);
cout<<endl<<endl;
break;
case 6: cout<<"Exiting...";
exit(0);
default:cout<<"Enter valid case\n";
break;
}
}
return 0;
}