Quick sort

An example of the implementation of the quick sort algorithm.

Input data

original data 	 1, 9, 3, 5, 14, 2, 0, 4, 10, 6

Quick sort

fun quickSort(data: Array<Int>, begin: Int = 0, end: Int = data.size - 1) {

    fun partition(data: Array<Int>, begin: Int, end: Int): Int {
        val pivot: Int = data[end]
        var b = begin
        var e = end
        while (b < e) {
            while (data[b] < pivot) { b++ }
            while (data[e] > pivot) { e-- }
            data.swap(b, e)
        }
        return b
    }

    if (begin < end) {
        val partitionIndex: Int = partition(data, begin, end)
        quickSort(data, begin, partitionIndex - 1)
        quickSort(data, partitionIndex + 1, end)
    }
}

Output data

insertionSort 	 0, 1, 2, 3, 4, 5, 6, 9, 10, 14