Created
July 20, 2022 22:56
-
-
Save mbove77/dd69614ad169cf9c700ed25cbd8b152d to your computer and use it in GitHub Desktop.
Bubble sort implemented with loops 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
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