Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: DSA in Kotlin
Stage 5: Specialization

Part 17 / Topic 6 of 12

Sorting, by hand and by library

Four classic sorts, by hand, then the one line you actually write in real code. Knowing the by-hand versions is what makes "Dual-Pivot Quicksort" and "a stable sort" in the next paragraph mean something instead of being two more unfamiliar names.

Sort Big-O (time) Space Stable? Idea
Bubble O(n²), O(n) best case O(1) yes repeatedly swap adjacent out-of-order pairs
Insertion O(n²), O(n) best case O(1) yes grow a sorted prefix one element at a time
Merge O(n log n) always O(n) yes split in half, sort each half, merge
Quick O(n log n) average, O(n²) worst O(log n) no pick a pivot, partition around it, recurse
fun quickSort(fares: IntArray, low: Int = 0, high: Int = fares.size - 1) {
    if (low >= high) return
    val pivot = fares[high]
    var boundary = low
    for (i in low until high) {
        if (fares[i] < pivot) {
            val tmp = fares[i]; fares[i] = fares[boundary]; fares[boundary] = tmp
            boundary++
        }
    }
    val tmp = fares[boundary]; fares[boundary] = fares[high]; fares[high] = tmp
    quickSort(fares, low, boundary - 1)
    quickSort(fares, boundary + 1, high)
}
$ kotlinc SortingDemo.kt -include-runtime -d sorting.jar
$ java -jar sorting.jar
original:      [820, 140, 560, 90, 310, 70, 450]
bubble sort:   [70, 90, 140, 310, 450, 560, 820]
insertion sort:[70, 90, 140, 310, 450, 560, 820]
merge sort:    [70, 90, 140, 310, 450, 560, 820]
quick sort:    [70, 90, 140, 310, 450, 560, 820]

-- sortedArray(): what you actually write in real Kotlin code --
sortedArray(): [70, 90, 140, 310, 450, 560, 820]
all four by-hand sorts match sortedArray(): true

-- sorting objects: delivery riders by rating, highest first --
  Asha - 4.8
  Kiran - 4.5
  Ravi - 4.2

-- what sortedArray()/sortedBy() actually run, per the Kotlin stdlib docs --
primitive arrays (IntArray, etc.): java.util.Arrays.sort -- a Dual-Pivot Quicksort variant, not stable, in place
object collections (List<T>, etc.): a stable sort (TimSort under the hood on the JVM)
stability matters for objects: equal-rated riders must keep their original order

original.sortedArray() and riders.sortedByDescending { it.rating } genuinely go through different algorithms depending on what's being sorted — Kotlin's IntArray.sortedArray() delegates to java.util.Arrays.sort(int[]) (in-place Dual-Pivot Quicksort, not stable, no stability needed because identical Ints are indistinguishable anyway), while sorting a List<T> of objects goes through a stable sort, because stability — equal elements keeping their original relative order — matters the moment the elements carry other data (two riders rated 4.5 should not swap places just because you re-sorted).

🔍 Going deeper: sortedBy { }/sortedWith(comparator) read better than writing out Comparator<Rider> { a, b -> a.rating.compareTo(b.rating) } by hand, and compareBy { }.thenBy { } chains multiple sort keys the same way — reach for the by-hand sorts in this section only to understand what's happening, not as your daily driver.

Full program: examples/sorting/SortingDemo.kt.

Done here? The practice set for Part 17 — including the solutions — is on the part page.

Search the guide