myqsort/zssort.h
2025-04-05 02:15:39 +02:00

54 lines
1.6 KiB
C

#ifndef ZS_SORT_H
#define ZS_SORT_H
#include <stdint.h>
#include "qsort.h"
static inline void zssort(uint32_t array[], int low, int high) {
/* (*) Loop handles original "other half recursion"! */
while(low < high) {
int pi = partition(array, low, high);
/* If we recurse only the smaller part */
/* That ensures at most n/2 elements can */
/* be on any given level of the recursion */
/* tree: that is we ensure log2(N) memuse! */
if((pi - low) < (high - pi)) {
// Left smaller: recurse left of pivot
zssort(array, low, pi - 1);
// (*) Update partitioning loop for remaining part
low = pi + 1;
} else {
// Right smaller: recurse right of pivot
zssort(array, pi + 1, high);
// (*) Update partitioning loop for remaining part
high = pi - 1; /* high inclusive! */
}
}
}
static inline void zssort_rand(uint32_t array[], int low, int high, rpivotstate *state) {
while (low < high) {
int pi = pick_pivot(state, (high + 1) - low) + low;
pi = partition_with_pivot(array, pi, low, high);
/* If we recurse only the smaller part */
/* That ensures at most n/2 elements can */
/* be on any given level of the recursion */
/* tree: that is we ensure log2(N) memuse! */
if((pi - low) < (high - pi)) {
// Left smaller: recurse left of pivot
zssort_rand(array, low, pi - 1, state);
// (*) Update partitioning loop for remaining part
low = pi + 1;
} else {
// Right smaller: recurse right of pivot
zssort_rand(array, pi + 1, high, state);
// (*) Update partitioning loop for remaining part
high = pi - 1; /* high inclusive! */
}
}
}
#endif /* ZS_SORT_H */