Skip to content

Instantly share code, notes, and snippets.

@mzimecki
Created October 12, 2013 19:09
Show Gist options
  • Save mzimecki/6953706 to your computer and use it in GitHub Desktop.
Save mzimecki/6953706 to your computer and use it in GitHub Desktop.
[Scala] Insertion sort
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 + " ")
}
}
@bearfighting
Copy link

very good

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment