Skip to content

Instantly share code, notes, and snippets.

@cgopalan
Created September 23, 2011 19:20
Show Gist options
  • Save cgopalan/1238212 to your computer and use it in GitHub Desktop.
Save cgopalan/1238212 to your computer and use it in GitHub Desktop.
List Insertion Sort
object ListSort {
def isort(xs: List[Int]): List[Int] =
if (xs.isEmpty) Nil
else insert(xs.head, isort(xs.tail))
def insert(head: Int, xs: List[Int]): List[Int] =
if (xs.isEmpty) List(head)
else if (head > xs.head) xs.head :: insert(head, xs.tail)
else head :: xs
def main(args: Array[String]) {
val l = List(6,4,8,12,1,7,22,10)
println("List is: " + l)
println("Sorted List is: " + isort(l))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment