Created
January 13, 2022 20:48
-
-
Save mertceyhan/e0b2fa3af925aa739fadc84d49d63c4f to your computer and use it in GitHub Desktop.
Bubble Sort in Kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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