-
Notifications
You must be signed in to change notification settings - Fork 0
/
dual_pivot_sequential_adaptive.h
66 lines (57 loc) · 2.16 KB
/
dual_pivot_sequential_adaptive.h
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
#ifndef QUICKSORTMP_DUAL_PIVOT_SEQUENTIAL_ADAPTIVE_H
#define QUICKSORTMP_DUAL_PIVOT_SEQUENTIAL_ADAPTIVE_H
#include <stddef.h>
#include "partition.h"
#include "order.h"
#include "insertion_sort.h"
static
void sort_adaptive(
void* restrict array,
ptrdiff_t lower_index,
ptrdiff_t higher_index,
size_t size,
enum Order compare(const void*, const void*)
) {
if (lower_index < higher_index) {
size_t insertion_length = 0;
if ((insertion_length = higher_index - lower_index) < INSERTION_SORT_THRESHOLD) {
register unsigned char* arr_ptr = array;
insertion_sort(arr_ptr + size * lower_index, insertion_length + 1, size, compare);
} else {
ptrdiff_t left_pivot = 0;
ptrdiff_t right_pivot = 0;
partition(array, lower_index, higher_index, &left_pivot, &right_pivot, size, compare);
if ((insertion_length = left_pivot - lower_index) > INSERTION_SORT_THRESHOLD) {
sort_adaptive(array, lower_index, left_pivot - 1, size, compare);
} else {
register unsigned char* arr_ptr = array;
insertion_sort(arr_ptr + size * lower_index, insertion_length + 1, size, compare);
}
if ((insertion_length = right_pivot - left_pivot) > INSERTION_SORT_THRESHOLD) {
sort_adaptive(array, left_pivot + 1, right_pivot - 1, size, compare);
} else {
register unsigned char* arr_ptr = array;
insertion_sort(arr_ptr + size * left_pivot, insertion_length + 1, size, compare);
}
if ((insertion_length = higher_index - right_pivot) > INSERTION_SORT_THRESHOLD) {
sort_adaptive(array, right_pivot + 1, higher_index, size, compare);
} else {
register unsigned char* arr_ptr = array;
insertion_sort(arr_ptr + size * right_pivot, insertion_length + 1, size, compare);
}
}
}
}
void dual_pivot_quicksort_sequential_adaptive(
void* restrict array,
size_t length,
size_t size,
enum Order compare(const void*, const void*)
) {
if (length > INSERTION_SORT_THRESHOLD){
sort_adaptive(array, 0, length - 1, size, compare);
} else {
insertion_sort(array, length, size, compare);
}
}
#endif //QUICKSORTMP_DUAL_PIVOT_SEQUENTIAL_ADAPTIVE_H