Quicksort

Notation

Current Partition: 1, 2, 3

Pivot Index: X

Loop Index: X

Swap Index: X

Recent Swap: X

Reference Code


/**
* @param start inclusive start index
* @param end exclusive end index
*/
function quickSort(list: number[], start: number, end: number) {
    if (start >= end - 1) return;

    const pivotIndex = partition(list, start, end)
    quickSort(list, start, pivotIndex)
    quickSort(list, pivotIndex + 1, end)
}

function partition(list: number[], start: number, end: number): number {
    let swapIndex = start
    for (let i = start; i < end - 1; i++) {
        if (list[i] < list[end - 1]) {
            swap(list, i, swapIndex)
            swapIndex++
        }
    }
    swap(list, swapIndex, end - 1)
    return swapIndex
}