chooses a pivot in items[a..b].
swaps likely_sorted when items[a..b] seems to be already sorted.
fn chosePivot(a: usize, b: usize, pivot: *usize, context: anytype) Hint
fn chosePivot(a: usize, b: usize, pivot: *usize, context: anytype) Hint {
// minimum length for using the Tukey's ninther method
const shortest_ninther = 50;
// max_swaps is the maximum number of swaps allowed in this function
const max_swaps = 4 * 3;
const len = b - a;
const i = a + len / 4 * 1;
const j = a + len / 4 * 2;
const k = a + len / 4 * 3;
var swaps: usize = 0;
if (len >= 8) {
if (len >= shortest_ninther) {
// find medians in the neighborhoods of `i`, `j` and `k`
sort3(i - 1, i, i + 1, &swaps, context);
sort3(j - 1, j, j + 1, &swaps, context);
sort3(k - 1, k, k + 1, &swaps, context);
}
// find the median among `i`, `j` and `k` and stores it in `j`
sort3(i, j, k, &swaps, context);
}
pivot.* = j;
return switch (swaps) {
0 => .increasing,
max_swaps => .decreasing,
else => .unknown,
};
}