-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbinaryInsertionSort.c
More file actions
34 lines (26 loc) · 883 Bytes
/
binaryInsertionSort.c
File metadata and controls
34 lines (26 loc) · 883 Bytes
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
#include "insertion.h"
// O(n log n)
int binarySearch(long int *array, int num, int start, int end){
if(end <= start)
return ((num > *(array + start)) ? (start + 1) : start);
int mid = (start + end) / 2;
if(num == *(array + mid))
return (mid + 1);
if(num > *(array + mid))
return binarySearch(array, num, mid + 1, end);
return binarySearch(array, num, start, mid - 1);
}
void binaryInsertionSort(long int *array, int length){
long int aux;
int i, j, pos;
for(i = 1; i < length; i++){
j = (i - 1);
aux = *(array + i);
pos = binarySearch(array, aux, 0, j); // Find the position where the element should be inserted
while(pos <= j){ // Move all elements until reaches the position
*(array + j + 1) = *(array + j);
j--;
}
*(array + j + 1) = aux;
}
}