Skip to content

Instantly share code, notes, and snippets.

@dschobel
Created November 5, 2012 04:22
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 dschobel/4015298 to your computer and use it in GitHub Desktop.
Save dschobel/4015298 to your computer and use it in GitHub Desktop.
insertionsort in scala
val r = new scala.util.Random()
val data = List.fill(10)(r.nextInt(100))
def isort(xs: 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 :: y :: ys else y :: insert(x, ys)
}
xs match {
case List() => xs
case y :: ys => insert(y, isort(ys))
}
}
isort(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment