This is an example of documentation generated by ZigDoc, an alternative to Zig's built-in Auto Doc feature. See also examples in other modes/formats. The project being documented here (as the example) is the Zig standard library. Private Function
fnpartialInsertionSort(a: usize, b: usize, context: anytype) bool {
@branchHint(.cold);
// maximum number of adjacent out-of-order pairs that will get shiftedconstmax_steps = 5;
// if the slice is shorter than this, don't shift any elementsconstshortest_shifting = 50;
vari = a + 1;
for (0..max_steps) |_| {
// find the next pair of adjacent out-of-order elements.while (i < band !context.lessThan(i, i - 1)) i += 1;
// are we done?if (i == b) returntrue;
// don't shift elements on short arrays, that has a performance cost.if (b - a < shortest_shifting) returnfalse;
// swap the found pair of elements. This puts them in correct order.context.swap(i, i - 1);
// shift the smaller element to the left.if (i - a >= 2) {
varj = i - 1;
while (j > a) : (j -= 1) {
if (!context.lessThan(j, j - 1)) break;
context.swap(j, j - 1);
}
}
// shift the greater element to the right.if (b - i >= 2) {
varj = i + 1;
while (j < b) : (j += 1) {
if (!context.lessThan(j, j - 1)) break;
context.swap(j, j - 1);
}
}
}
returnfalse;
}
partially sorts a slice by shifting several out-of-order elements around.
returns
trueif the slice is sorted at the end. This function isO(n)worst-case.