An interactive visualization of the Quick Sort algorithm using the Lomuto partition scheme. Watch how a pivot element partitions the array into smaller and larger halves, then recursively sorts each partition.
Divide and Conquer LeetCode 912 ↗ Mediumnums, sort the array in ascending order and return it.fun quickSort(arr: IntArray, low: Int, high: Int) { if (low < high) { val pi = partition(arr, low, high) quickSort(arr, low, pi - 1) quickSort(arr, pi + 1, high) } }fun partition(arr: IntArray, low: Int, high: Int): Int { val pivot = arr[high] var i = low - 1 for (j in low until high) { if (arr[j] <= pivot) { i++ arr[i] = arr[j].also { arr[j] = arr[i] } } } arr[i+1] = arr[high].also { arr[high] = arr[i+1] }; return i + 1 }
Quick Sort works by choosing a pivot element and partitioning the array around it. After partitioning, the pivot is in its final sorted position — all elements to its left are smaller or equal, and all elements to its right are larger. This divide-and-conquer approach recursively sorts both halves, resulting in a fully sorted array without needing to merge.
pivot = arr[high])i = low - 1 as the boundary between elements ≤ pivot and elements > pivotj from low to high - 1i and swap arr[i] with arr[j], expanding the "less than or equal" regionarr[i + 1] with arr[high] to place the pivot in its correct positionlow to pi - 1) and the right partition (pi + 1 to high)
On average, each partition divides the array roughly in half, giving O(log n) levels of recursion
with O(n) work per level. The worst case occurs when the pivot is always the smallest or largest element
(e.g., already sorted input with last-element pivot), leading to O(n²) time. The space complexity
of O(log n) comes from the recursion stack depth.