-
Notifications
You must be signed in to change notification settings - Fork 0
/
heapsort.c
75 lines (66 loc) · 1.59 KB
/
heapsort.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
#include <stdio.h>
#include <time.h> // Include the time.h header for time functions
void heapify(int arr[], int n)
{
int i, j, k, v, flag;
for (i = n / 2; i >= 1; i--)
{
k = i;
v = arr[k];
flag = 0;
while (!flag && 2 * k <= n)
{
j = 2 * k;
if (j < n)
{
if (arr[j] < arr[j + 1])
j++;
}
if (v >= arr[j])
flag = 1;
else
{
arr[k] = arr[j];
k = j;
}
}
arr[k] = v;
}
}
void heapSort(int arr[], int n)
{
int i, temp;
heapify(arr, n);
for (i = n; i >= 2; i--)
{
temp = arr[1];
arr[1] = arr[i];
arr[i] = temp;
heapify(arr, i - 1);
}
}
int main()
{
int arr[100], n, i;
clock_t start, end;
printf("Enter the number of elements (max 100): ");
scanf("%d", &n);
// Input validation for number of elements
if (n < 1 || n > 100) {
printf("Number of elements must be between 1 and 100.\n");
return 1; // Exit the program if invalid
}
printf("Enter the elements: ");
for (i = 1; i <= n; i++)
scanf("%d", &arr[i]);
start = clock();
heapSort(arr, n);
end = clock();
double total_time_taken = ((double)(end - start) * 1000) / CLOCKS_PER_SEC; // Time in milliseconds
printf("The sorted array is: ");
for (i = 1; i <= n; i++)
printf("%d ", arr[i]);
printf("\n");
printf("\nTime taken = %lf ms\n", total_time_taken);
return 0;
}