Skip to content

Instantly share code, notes, and snippets.

@mertceyhan
Created January 13, 2022 20:48
Show Gist options
  • Save mertceyhan/e0b2fa3af925aa739fadc84d49d63c4f to your computer and use it in GitHub Desktop.
Save mertceyhan/e0b2fa3af925aa739fadc84d49d63c4f to your computer and use it in GitHub Desktop.
Bubble Sort in Kotlin
object BubbleSort {
fun sort(array: Array<Int>) {
for (i in array.indices) {
for (j in 0 until array.size - i - 1) {
if (array[j] > array[j + 1]) {
swap(array, firstIndex = j, secondIndex = j + 1)
}
}
}
}
private fun swap(array: Array<Int>, firstIndex: Int, secondIndex: Int) {
val temp: Int = array[firstIndex]
array[firstIndex] = array[secondIndex]
array[secondIndex] = temp
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment