-
Notifications
You must be signed in to change notification settings - Fork 0
/
heap_sort.c
105 lines (92 loc) · 2.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
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define INPUT_SIZE 1000000
//macros: get rid of the cost of jumping and returning by repeating instructions at each call
#define left(i) 2*i
#define right(i) 2*i+1
struct Heap{
int heap_size;
int* array;
};
void max_heapify(struct Heap, int);
struct Heap build_max_heap(int[], int);
void heap_sort(int[], int);
void swap(int[], int, int);
int main(void){
int arr[INPUT_SIZE];
int i;
for(i = 0; i < INPUT_SIZE; i++){
arr[i] = random();
}
clock_t start_time = clock();
heap_sort(arr, INPUT_SIZE);
clock_t end_time = clock();
double execution_time = (double)(end_time - start_time) / CLOCKS_PER_SEC;
/*for(i = 0; i < INPUT_SIZE; i++){
printf("%i\n", arr[i]);
}*/
printf("\nExecution Time = %f\n", execution_time);
return 0;
}
/*
** heap is an object of heap structure
** index is the index of a node
** given a node that has a left and right child each represents a max-heap and their parent needs to be put in its right position so the whole subtree represents a max-heap
*/
void max_heapify(struct Heap heap, int index){
int r = right(index);
int l = left(index);
int largest;
if(l <= heap.heap_size && heap.array[l] > heap.array[index]){
largest = l;
}
else{
largest = index;
}
if(r <= heap.heap_size && heap.array[r] > heap.array[largest]){
largest = r;
}
if(index != largest){
swap(heap.array, index, largest);
max_heapify(heap, largest);
}
}
/*
** array is an array to be converted into a max-heap
** n is the length of the array
** return a max-heap
*/
struct Heap build_max_heap(int array[], int n){
struct Heap heap;
heap.heap_size = n;
heap.array = array;
int i;
for(i = heap.heap_size / 2 - 1; i >= 0; i--){
max_heapify(heap, i);
}
return heap;
}
/*
** array is an array to be ordered using a heap_sort
** n is the length of the array
*/
void heap_sort(int array[], int n){
struct Heap heap = build_max_heap(array, n);
int i;
for(i = n - 1; i > 0; i--){
swap(heap.array, 1, i);
heap.heap_size --;
max_heapify(heap, 1);
}
}
/*
** A is the array that holds items to be swapped
** i is the index of the item to be swapped
** j is the index of the other item to be swapped
*/
void swap(int A[], int i, int j){
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}