Created
March 10, 2018 14:10
-
-
Save taintech/bbf238a1bc94fef2b191ee3d4f6c1ff6 to your computer and use it in GitHub Desktop.
Scala bubble sort implementation
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 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