Przejdź do treści

Sortowanie szybkie

🔗 Opis problemu

Implementacja

fun quickSort(array: MutableList<Int>, left: Int, right: Int) {
    if (right <= left) {
        return
    }

    val pivot = array[(left + right) / 2]
    var i = left
    var j = right

    while (i <= j) {
        while (array[i] < pivot) {
            i += 1
        }

        while (array[j] > pivot) {
            j -= 1
        }

        if (i > j) {
            break
        } 

        val tmp = array[i]
        array[i] = array[j]
        array[j] = tmp

        i++
        j--
    }

    quickSort(array, left, j)
    quickSort(array, i, right)
}

fun main() {
    val array = mutableListOf(7, 3, 0, 1, 5, 2, 5, 19, 10, 5)

    quickSort(array, 0, array.count() - 1)

    println(array)
}

Sortowanie szybkie