Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Scala bubble sort implementation
object sortings {
def swap(ar: Array[Int], i: Int, j: Int): Array[Int] = {
val temp = ar(i)
ar(i) = ar(j)
ar(j) = temp
ar
}
def bubbleSort(ar: Array[Int]): Array[Int] = {
for {
i <- ar.indices
j <- ar.length - 1 until i by -1
} if (ar(j) < ar(j - 1)) swap(ar, j, j - 1)
ar
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment