-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHeap_Sort.c
61 lines (46 loc) · 1.29 KB
/
Heap_Sort.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
#include <stdio.h>
#include <stdlib.h>
void heapify(short arr[], int size, int root) {
int largest = root;
int left = 2 * root + 1;
int right = 2 * root + 2;
if (left < size && arr[left] > arr[largest])
largest = left;
if (right < size && arr[right] > arr[largest])
largest = right;
if (largest != root) {
short temp = arr[root];
arr[root] = arr[largest];
arr[largest] = temp;
heapify(arr, size, largest);
}
}
void heapSort(short arr[], int size) {
for (int i = size / 2 - 1; i >= 0; i--)
heapify(arr, size, i);
for (int i = size - 1; i > 0; i--) {
short temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
heapify(arr, i, 0);
}
}
int main() {
int size;
short *arr;
printf("Enter the size of the array: ");
scanf("%d", &size);
arr = (short*) malloc(size * sizeof(short));
printf("Enter %d elements:\n", size);
for (int i = 0; i < size; i++) {
printf("Element %d: ", i + 1);
scanf("%hd", &arr[i]);
}
heapSort(arr, size);
printf("\nSorted array: ");
for (int i = 0; i < size; i++)
printf("%hd ", arr[i]);
free(arr);
return 0;
}
//complexity : O(n logn)