-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathminHeapSort.c
More file actions
39 lines (30 loc) · 1.09 KB
/
minHeapSort.c
File metadata and controls
39 lines (30 loc) · 1.09 KB
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
#include "selection.h"
// Worst case performance O(n log n)
// Best case performance O(n log n)
// Average performance O(n log n)
void heapMin(long int *array, int n, int i){ // n is the size of heap
int smallest = i, left = 2 * i + 1, right = 2 * i + 2;
long int aux;
if(left < n && array[left] < *(array + smallest)) // Left child is smaller than node
smallest = left;
if(right < n && array[right] < *(array + smallest)) // Right child is smaller than node
smallest = right;
if(smallest != i){ // Smaller is not the actual node
aux = *(array + i);
*(array + i) = *(array + smallest);
*(array + smallest) = aux;
heapMin(array, n, smallest); //Acess the subtree
}
}
void minHeapSort(long int *array, int length){
int i;
long int aux;
for(i = length / 2 - 1; i >= 0; i--)
heapMin(array, length, i); // Creating the heap
for(i = length-1; i >= 0; i--){ // Remove each element from heap
aux = *(array + i);
*(array + i) = *array;
*array = aux;
heapMin(array, i, 0); // Reduced heap
}
}