forked from vatsal-08/DSA_code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheap.c
More file actions
219 lines (217 loc) · 4.57 KB
/
heap.c
File metadata and controls
219 lines (217 loc) · 4.57 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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include<stdio.h>
#include<stdlib.h>
int size = 0;
void swap(int *x, int *y)
{
int t = *y;
*y = *x;
*x = t;
}
void maxheapify(int arr[], int size, int i)
{
int large = i;
int l = 2 * i + 1;
int r = 2 * i + 2;
if (l < size && arr[l] > arr[large])
{large = l;}
if (r < size && arr[r] > arr[large])
{ large = r;}
if (large != i)
{
swap(&arr[i], &arr[large]);
maxheapify(arr, size, large);
}
}
void minheapify(int arr[], int size, int i)
{
int small = i;
int l = 2 * i + 1;
int r = 2 * i + 2;
if (l < size && arr[l] < arr[small])
{ small = l;}
if (r < size && arr[r] < arr[small])
{small = r;}
if (small != i)
{
swap(&arr[i], &arr[small]);
minheapify(arr, size, small);
}
}
void buildminheap(int arr[], int n)
{
int startidx = (n / 2) - 1;
for (int i = startidx; i >= 0; i--)
{
minheapify(arr, n, i);
}
}
void buildmaxheap(int arr[], int n)
{
int startidx = (n / 2) - 1;
for (int i = startidx; i >= 0; i--)
{
maxheapify(arr, n, i);
}
}
void insertminheap(int arr[], int new)
{
if (size == 0)
{
arr[0] = new;
size += 1;
}
else
{
arr[size] = new;
size += 1;
for (int i = size / 2 - 1; i >= 0; i--)
{
minheapify(arr, size, i);
}
}
}
void insertmaxheap(int arr[], int new)
{
if (size == 0)
{
arr[0] = new;
size += 1;
}
else
{
arr[size] = new;
size += 1;
for (int i = size / 2 - 1; i >= 0; i--)
{
maxheapify(arr, size, i);
}
}
}
void printHeap(int arr[], int n)
{
for (int i = 0; i < n; ++i)
{printf("%d\t",arr[i]);}
printf("\n");
}
int main()
{ int c=1,n;
while (c==1)
{
size=0;
int choice,d;
printf("\n----------MAIN MENU ----------\n");
printf("CHOICES ARE AS FOLLOW\n");
printf("1) press 1. Create a minheap from a sequence of integers. \n");
printf("2) press 2. Create a maxheap from a sequence of integers. \n");
printf("3) press 3. Convert any random arr into a minheap or maxheap. \n");
printf("4) press 4. Heapsort using minheaps and maxheaps. \n");
scanf("%d",&choice);
if(choice==1)
{
n=100;
int arr[n];
int t,d;
printf("how many element you want to enter--");
scanf("%d",&t);
for(int i=0;i<t;i++)
{
printf("enter the element--");
scanf("%d",&d);
insertminheap(arr, d);
}
printf("Array representation of Min Heap is:\n" );
printHeap(arr,size);
}
else if(choice==2)
{
n=100;
int arr[n];
int t,d;
printf("how many element you want to enter--");
scanf("%d",&t);
for(int i=0;i<t;i++)
{
printf("enter the element--");
scanf("%d",&d);
insertmaxheap(arr, d);
}
printf("Array representation of Max Heap is:\n" );
printHeap(arr,size);
}
else if(choice==3)
{
printf("enter the length of array --");
scanf("%d",&n);
int arr[n];
int t,i,d;
for(int i=0;i<n;i++)
{
printf("enter the element of array --");
scanf("%d",&arr[i]);
}
printf("prees 1 for min heap or 2 for max heap ---");
scanf("%d",&t);
if(t==1)
{
buildminheap(arr,n);
}
else
{
buildmaxheap(arr, n);
}
printf("Array representation of Heap is:\n" );
printHeap(arr,n);
}
else if(choice==4)
{
n=100;
int arr[n];
int w,d;
printf("prees 1 to sort with min heap or 2 with max heap----");
scanf("%d",&w);
if(w==1)
{
int arr[n];
int t,i,d;
printf("how many element you want to enter to sort--");
scanf("%d",&t);
for(int i=0;i<t;i++)
{
printf("enter the element--");
scanf("%d",&d);
insertminheap(arr, d);
}
for (int i = size - 1; i >= 0; i--)
{
swap(&arr[0], &arr[i]);
minheapify(arr, i, 0);
}
printf("sorted element using Heap sort with min heap is:\n" );
printHeap(arr,size);
}
else
{
int arr[n];
int t,i,d;
printf("how many element you want to enter sort--");
scanf("%d",&t);
for(int i=0;i<t;i++)
{
printf("enter the element--");
scanf("%d",&d);
insertmaxheap(arr, d);
}
for (int i = size - 1; i >= 0; i--)
{
swap(&arr[0], &arr[i]);
maxheapify(arr, i, 0);
}
printf("sorted element using Heap sort with max heap is:\n" );
printHeap(arr,size);
}
}
printf("\nto go to main menu prees 1 else press 0--");
scanf("%d",&c);
}
return 0;
}