Created
October 12, 2013 19:09
-
-
Save mzimecki/6953706 to your computer and use it in GitHub Desktop.
[Scala] Insertion sort
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 InsertionSort { | |
def sort(list: List[Int]): List[Int] = { | |
def insert(x: Int, xs: List[Int]): List[Int] = xs match { | |
case List() => List(x) | |
case y :: ys => if (x <= y) x :: xs else y :: insert(x, ys) | |
} | |
list match { | |
case List() => List() | |
case y :: ys => insert(y, sort(ys)) | |
} | |
} | |
def main(args: Array[String]) { | |
val l = List(4, 6, 23, 6, 7) | |
val sortedList = sort(l) | |
for (i <- sortedList) | |
print(i + " ") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
very good