forked from MostafaTwfiq/C-DataStructures-And-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HeapSort.c
232 lines (174 loc) · 5.97 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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
#include "../Headers/HeapSort.h"
#include "../Headers/SwapFunction.h"
#include "../../../System/Utils.h"
#include "../../../Unit Test/CuTest/CuTest.h"
// Formulas:
// child 1: parentIndex * 2 + 1
// child 2: parentIndex * 2 + 2
// parentIndex = (childIndex - 1) / 2
// Ex array:
// 5, 3, 4, 10, 6
// Step 1: build a valid heap
// **we don't need to allocate a new array, we just can use the original array to convert it to a valid heap**
// First method:
// add 5
// 5
// add 3
// 5, 3
// add 4
// 5, 3, 4
// add 10
// 5, 3, 4, 10
// 10, 5, 4, 3
// add 6
// 10, 5, 4, 3, 6
// 10, 6, 4, 3, 5
// Second method (Optimized):
// Check every element node with it's children, and swap the parent with it's biggest or smallest child (depends on the type of the heap, min or max).
// The complexity of this approach should be O(n).
// The heap in 2D:
// 10
// 6 4
// 3 5
// Step 2: delete the tree root and balance the heap. Repeat until your tree turns empty.
// 10, 6, 4, 3, 5
// 5, 6, 4, 3, 10
// 6, 5, 4, 3, 10
// 3, 5, 4, 6, 10
// 5, 3, 4, 6, 10
// 4, 3, 5, 6, 10
// 3, 4, 5, 6, 10
/** This function will take a child index,
* then it will return the parent index.
*
* @param childIndex the child index
* @return it will return the parent index
*/
int getParentIndex(int childIndex) {
return (childIndex - 1) / 2;
}
/** This function will take a parent index,
* then it will return the first child index.
*
* @param parentIndex the parent index
* @return it will return the first child index
*/
int getFirstChildIndex(int parentIndex) {
return parentIndex * 2 + 1;
}
/** This function will take a parent index,
* then it will return the second child index.
*
* @param parentIndex the parent index
* @return the second child index
*/
int getSecondChildIndex(int parentIndex) {
return parentIndex * 2 + 2;
}
/** This function will swap up the element with the given index,
* until it's in the right place.
*
* @param arr the array pointer
* @param index the index of the element
* @param elemSize the size of the array elements in bytes
* @param cmp the comparator function, that will be called to compare the elements
*/
void heapUp(char *arr, int index, int elemSize, int (*cmp)(const void *, const void *)) {
if (index <= 0)
return;
int parentIndex = getParentIndex(index);
if (cmp(arr + index * elemSize, arr + parentIndex * elemSize) > 0) {
swap(arr + index * elemSize, arr + parentIndex * elemSize, elemSize);
heapUp(arr, parentIndex, elemSize, cmp);
}
}
/** This function will swap down the element with the given index,
* until it's in the right place.
*
* @param arr the array pointer
* @param length the length of the array
* @param index the index of the element
* @param elemSize the array elements size in bytes
* @param cmp the the comparator function, that will be called to compare the elements
*/
void heapDown(char *arr, int length, int index, int elemSize, int (*cmp)(const void *, const void *)) {
int fChildIndex = getFirstChildIndex(index), sChildIndex = getSecondChildIndex(index);
int target = index;
if (fChildIndex < length && cmp(arr + target * elemSize, arr + fChildIndex * elemSize) < 0)
target = fChildIndex;
if (sChildIndex < length && cmp(arr + target * elemSize, arr + sChildIndex * elemSize) < 0)
target = sChildIndex;
if (target != index) {
swap(arr + index * elemSize, arr + target * elemSize, elemSize);
heapDown(arr, length, target, elemSize, cmp);
}
}
/** This function takes an array, and it will convert the array to a valid heap.
*
* Note: This function should only be called by the heap sort functions.
*
* Note: the complexity of this algorithm is O(n).
*
* @param arr the array pointer
* @param length the length of the array (number of elements)
* @param elemSize the size of the array elements
* @param cmp the compare function, that will compare the array elements
*/
void buildHeap(char *arr, int length, int elemSize, int (*cmp)(const void *, const void *)) {
for (int i = length - 1; i >= 0; i--)
heapDown(arr, length, i, elemSize, cmp);
}
/** This function will take an array then it sort it with the heap sort algorithm.
*
* Time Complexity: worst: O( n log(n) ) , best: O ( n log(n) ).
*
* Space Complexity: O ( 1 ).
*
* @param arr the array pointer
* @param length the length of the array
* @param elemSize the size of the array elements in bytes
* @param cmp the comparator function pointer
*/
void heapSort(void *arr, int length, int elemSize, int (*cmp)(const void *, const void *)) {
if (arr == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = NULL_POINTER;
return;
#else
fprintf(stderr, NULL_POINTER_MESSAGE, "passed array", "heap sort");
exit(NULL_POINTER);
#endif
} else if (cmp == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = INVALID_ARG;
return;
#else
fprintf(stderr, INVALID_ARG_MESSAGE, "comparator function pointer", "heap sort");
exit(INVALID_ARG);
#endif
} else if (length < 0) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = INVALID_ARG;
return;
#else
fprintf(stderr, INVALID_ARG_MESSAGE, "array length", "heap sort");
exit(INVALID_ARG);
#endif
} else if (elemSize <= 0) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = INVALID_ARG;
return;
#else
fprintf(stderr, INVALID_ARG_MESSAGE, "element size", "heap sort");
exit(INVALID_ARG);
#endif
}
char *oneBytePointer = (char *) arr;
// to build a valid heap.
buildHeap(oneBytePointer, length, elemSize, cmp);
// to sort the heap (delete the tree root and balance the heap the number of times the length of the array).
while (length-- > 0) {
swap(oneBytePointer, oneBytePointer + length * elemSize, elemSize);
heapDown(oneBytePointer, length, 0, elemSize, cmp);
}
}