Created
September 23, 2011 19:20
-
-
Save cgopalan/1238212 to your computer and use it in GitHub Desktop.
List 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 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