Kotlin Program to Implement Quick Sort Algorithm
quickSort function that picks a pivot, partitions the list, and recursively sorts sublists; for example: fun quickSort(arr: MutableList, low: Int, high: Int) { if (low < high) { val pi = partition(arr, low, high); quickSort(arr, low, pi - 1); quickSort(arr, pi + 1, high) } } .Examples
How to Think About It
Algorithm
Code
fun partition(arr: MutableList<Int>, 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
}
fun quickSort(arr: MutableList<Int>, low: Int, high: Int) {
if (low < high) {
val pi = partition(arr, low, high)
quickSort(arr, low, pi - 1)
quickSort(arr, pi + 1, high)
}
}
fun main() {
val list = mutableListOf(10, 7, 8, 9, 1, 5)
quickSort(list, 0, list.size - 1)
println(list)
}Dry Run
Let's trace sorting [10, 7, 8, 9, 1, 5] through the quick sort code.
Initial call
quickSort called with low=0, high=5, array=[10, 7, 8, 9, 1, 5]
Partitioning
Pivot=5, rearranged array after partition: [1, 5, 8, 9, 10, 7], pivot index=1
Left recursive call
quickSort called with low=0, high=0 (single element), returns immediately
Right recursive call
quickSort called with low=2, high=5, array=[8, 9, 10, 7]
Partitioning right sublist
Pivot=7, rearranged array: [1, 5, 7, 9, 10, 8], pivot index=2
Further recursive calls
Sort left sublist low=2, high=1 (empty), right sublist low=3, high=5
Final sorting
After sorting right sublist, final array: [1, 5, 7, 8, 9, 10]
| Step | Array State | Pivot | Pivot Index |
|---|---|---|---|
| 1 | [10, 7, 8, 9, 1, 5] | 5 | 1 |
| 2 | [1, 5, 8, 9, 10, 7] | 7 | 2 |
| 3 | [1, 5, 7, 9, 10, 8] | 8 | 5 |
| 4 | [1, 5, 7, 8, 9, 10] | - | - |
Why This Works
Step 1: Choosing a pivot
The pivot divides the list into smaller and larger parts using partition.
Step 2: Partitioning the list
Elements less than or equal to pivot move left; others move right, so pivot is in correct place.
Step 3: Recursive sorting
Quick sort repeats on left and right parts until all parts are sorted, resulting in a sorted list.
Alternative Approaches
fun lomutoPartition(arr: MutableList<Int>, low: Int, high: Int): Int {
val pivot = arr[high]
var i = low
for (j in low until high) {
if (arr[j] < pivot) {
arr[i] = arr[j].also { arr[j] = arr[i] }
i++
}
}
arr[i] = arr[high].also { arr[high] = arr[i] }
return i
}
fun quickSortLomuto(arr: MutableList<Int>, low: Int, high: Int) {
if (low < high) {
val pi = lomutoPartition(arr, low, high)
quickSortLomuto(arr, low, pi - 1)
quickSortLomuto(arr, pi + 1, high)
}
}fun quickSortBuiltIn(arr: MutableList<Int>) {
arr.sort()
}Complexity: O(n log n) average, O(n^2) worst case time, O(log n) due to recursion stack space
Time Complexity
Quick sort divides the list and sorts sublists recursively, averaging O(n log n) because each partition splits the list roughly in half.
Space Complexity
It uses O(log n) space for recursion calls; the sorting is done in-place without extra arrays.
Which Approach is Fastest?
Quick sort is generally faster than simple sorts like bubble sort; built-in sorts may use hybrid algorithms optimized for real data.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Quick Sort (Hoare) | O(n log n) avg | O(log n) | General-purpose in-place sorting |
| Quick Sort (Lomuto) | O(n log n) avg | O(log n) | Simpler code, less efficient on some inputs |
| Built-in sort | O(n log n) avg | O(n) | Best performance, no learning quick sort logic |