forked from Soumyadeep-Sadhu/HacktoberFest2021-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheap_menudriven.c
203 lines (174 loc) · 4.82 KB
/
heap_menudriven.c
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
#include <stdio.h>
#include <stdlib.h>
#include<time.h>
int *randomNumberGen(int n) {
int *arr = (int *)malloc(sizeof(int) * n);
for (int i = 0; i < n; i++) {
arr[i] = rand()%100;
}
return arr;
}
void swap(int *a, int *b)
{
int tmp = *a;
*a = *b;
*b = tmp;
}
void heapify(int *arr, int n, int i)
{
int max = i;
int leftChild = 2 * i + 1;
int rightChild = 2 * i + 2;
if (leftChild < n && arr[leftChild] > arr[max])
max = leftChild;
if (rightChild < n && arr[rightChild] > arr[max])
max = rightChild;
if (max != i)
{
swap(&arr[i], &arr[max]);
heapify(arr, n, max);
}
}
void heapSort(int *arr, int n)
{
for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);
for (int i = n - 1; i >= 0; i--)
{
swap(&arr[0], &arr[i]);
heapify(arr, i, 0);
}
}
void sortDescending(int *arr,int n)
{
int i, key, j;
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] < key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
void printArray(int *arr,int n)
{
for(int i=0;i<n;i++)
printf("%d ",arr[i]);
printf("\n");
}
int largest(int *arr, int n)
{
int i;
int max = arr[0];
for (i = 1; i < n; i++)
if (arr[i] > max)
max = arr[i];
printf("Maximum element is: %d\n",max);
}
void delete(int *arr,int n)
{
int last = arr[n - 1];
arr[0] = last;
heapSort(arr,n);
}
void insert(int *arr,int n,int key)
{
arr[n-1] = key;
heapSort(arr, n);
}
void replace(int *arr,int n)
{
int k,in;
printf("Enter Element and Index of Replacement:\n");
scanf("%d%d",&k,&in);
arr[in]=k;
heapSort(arr,n);
}
int main()
{
int choice,n;
do
{
printf("0. Exit\n1. Insert Random Elements\n2. Display the Array\n3. Sort the Array in Ascending Order by using Max-Heap Sort Algorithm\n4. Sort the Array in Descending Order by using any sorting algorithm\n5. Time Complexity to sort ascending of random data\n6. Time Complexity to sort ascending of data already sorted in ascending order\n7. Time Complexity to sort ascending of data already sorted in descending order\n8. Time Complexity to sort ascending of data for all Cases.\n9. Maximum Element\n10. Replace value at a node with new value. \n11. Insert a new element.\n12. Delete an element\n");
scanf("%d",&choice);
switch(choice)
{
case 0:printf("Exiting !!!\n");
exit(1);
case 1:
printf("Enter N:");
scanf("%d", &n);
int *arr;
arr = randomNumberGen(n);
break;
case 2:
printArray(arr,n);
break;
case 3:
heapSort(arr, n);
break;
case 4:
sortDescending(arr,n);
break;
case 5:
printf("\n");
int *arr1;
arr1=randomNumberGen(10000);
clock_t start,end;
double total=0.0;
start=clock();
heapSort(arr1, 10000);
end=clock();
total+=(double)(end-start)/CLOCKS_PER_SEC;
printf("\nTime elpased is %f seconds\n", total);
printf("Time Complexity=O(nlog n)\n\n");
break;
case 6:
printf("\n");
clock_t start1,end1;
double total1=0.0;
start1=clock();
heapSort(arr1, 10000);
end1=clock();
total1+=(double)(end1-start1)/CLOCKS_PER_SEC;
printf("\nTime elpased is %f seconds\n", total1);
printf("Time Complexity=O(nlog n)\n");
break;
case 7:
sortDescending(arr1,10000);
clock_t start2,end2;
double total2=0.0;
start2=clock();
heapSort(arr1, 10000);
end2=clock();
total2+=(double)(end2-start2)/CLOCKS_PER_SEC;
printf("\nTime elpased is %f seconds\n", total2);
printf("Time Complexity=O(nlog n)\n");
break;
case 8:
printf("Already Sorted: O(nlog n)\n");
printf("Reverse Sorted: O(nlog n)\n");
printf("Random Sample: O(nlog n)\n");
break;
case 9:
largest(arr,n);
break;
case 10:
replace(arr,n);
break;
case 11:
printf("Enter the Element\n");
int key;
scanf("%d",&key);
n=n+1;
insert(arr,n,key);
case 12:
n=n-1;
delete(arr,n);
break;
}
}while (choice!=0);
return 0;
}