Bubble sort

An example of the implementation of the bubble sort algorithm.

Input data

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

Bubble sort

fun bubbleSort(data: Array<Int>) {
    var swapped = false
    for (i in 0 until data.size - 1) {
        for (j in 0 until data.size - 1 - i) {
            if (data[j] > data[j + 1]) data.swap(j, j + 1)
            swapped = true
        }
        if (!swapped) break
    }
}

Output data

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