This commit is contained in:
Niklas Gollenstede
2025-10-31 22:37:36 +01:00
commit 174fe17e89
197 changed files with 79558 additions and 0 deletions

30
kernel/utils/sort.h Normal file
View 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;
}