Handout
This commit is contained in:
30
kernel/utils/sort.h
Normal file
30
kernel/utils/sort.h
Normal file
@@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
|
||||
template <typename T>
|
||||
void arraysort(T *array, int size, int (*comp)(T *a, T *b)) {
|
||||
// this is shellsort
|
||||
static const int gaps[] = {3548, 1577, 701, 301, 132, 57, 23, 10, 4, 1};
|
||||
static const int gapsize = 10;
|
||||
|
||||
for (int g = 0; g < gapsize; ++g) {
|
||||
const int gap = gaps[g];
|
||||
for (int i = gap; i < size; i += 1) {
|
||||
T tmp = array[i];
|
||||
int j;
|
||||
for (j = i; (j >= gap) && (comp(array + j - gap, &tmp) > 0);
|
||||
j -= gap)
|
||||
array[j] = array[j - gap];
|
||||
array[j] = tmp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool arraysort_check(T *array, int size, int (*comp)(T *a, T *b)) {
|
||||
for (int i = 0; i < size - 1; ++i) {
|
||||
if (comp(array + i, array + i + 1) > 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user