-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_swap_array.h
More file actions
executable file
·37 lines (33 loc) · 996 Bytes
/
quick_swap_array.h
File metadata and controls
executable file
·37 lines (33 loc) · 996 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
35
36
37
#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_optimized_swap_array(int array[], int low, int high) {
int pivot = median_of_three_of_median_of_three(array, low, high);
int x[2];
int i = low;
for (int j = low; j < high; j++) {
int c = pivot > array[j];
x[0] = array[i];
x[1] = array[j];
array[j] = x[1 - c];
array[i] = x[c];
i += c;
}
swap(&array[i], &array[high]);
return (i);
}
void sort_quick_optimized_swap_array(int array[], int low, int high) {
if (low < high) {
if (high - low > 20) {
int pi = partition_quick_optimized_swap_array(array, low, high);
sort_quick_optimized_swap_array(array, low, pi - 1);
sort_quick_optimized_swap_array(array, pi + 1, high);
} else {
insertionSort(array + low, high - low + 1);
}
}
}