Skip to content

Instantly share code, notes, and snippets.

@mbove77
Created July 20, 2022 22:56
Show Gist options
  • Save mbove77/dd69614ad169cf9c700ed25cbd8b152d to your computer and use it in GitHub Desktop.
Save mbove77/dd69614ad169cf9c700ed25cbd8b152d to your computer and use it in GitHub Desktop.
Bubble sort implemented with loops in kotlin
fun bubbleSortLoop(inputArray: Array<Int>): Array<Int> {
var iterations = 1
var isSorted: Boolean
for (i in 0 until inputArray.size -1) {
isSorted = true
// This print is for see the interactions in sort algorithm
println("Iteration number: ${iterations++}")
for (j in 0 until inputArray.size -i -1) {
if (inputArray[j] > inputArray[j+1]) {
swapNumberInArray(inputArray, j)
isSorted = false
}
}
if (isSorted) {
return inputArray
}
}
return inputArray
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment