Skip to content

Instantly share code, notes, and snippets.

@taintech
Created March 10, 2018 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taintech/bbf238a1bc94fef2b191ee3d4f6c1ff6 to your computer and use it in GitHub Desktop.
Save taintech/bbf238a1bc94fef2b191ee3d4f6c1ff6 to your computer and use it in GitHub Desktop.
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