-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_tuned.h
More file actions
43 lines (38 loc) · 1.03 KB
/
Copy pathquick_tuned.h
File metadata and controls
43 lines (38 loc) · 1.03 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
40
41
42
43
// Based on http://cphstl.dk/Paper/Quicksort/Report-2014-1/doc.pdf
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
void swap(int *a, int *b);
int median_of_three_of_median_of_three(int array[], int low, int high);
void insertionSort(int array[], int n);
int partition_quick_tuned(int array[], int low, int high) {
int pivot = median_of_three_of_median_of_three(array, low, high);
int p = low - 1;
int q = low;
while(q < high){
int x = array[q];
bool smaller = x < pivot;
p += smaller;
int delta = smaller * (q - p);
int s = p + delta;
int t = q - delta;
array[s] = array[p];
array[t] = x;
q++;
}
p++;
swap(&array[p], &array[high]);
return p;
}
void sort_quick_tuned(int array[], int low, int high) {
if (low < high) {
if (high - low > 16) {
int pi = partition_quick_tuned(array, low, high);
sort_quick_tuned(array, low, pi - 1);
sort_quick_tuned(array, pi + 1, high);
} else {
insertionSort(array + low, high - low + 1);
}
}
}