Created
May 9, 2010 15:08
-
-
Save gom/395215 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
def sort(list:List[Int]):List[Int] = { | |
if (list.size == 1) { return list } | |
val mid = list.size / 2 | |
val left = sort(list.take(mid)) | |
val right = sort(list.drop(mid)) | |
merge(left, right) | |
} | |
def merge(left:List[Int], right:List[Int]):List[Int]= { | |
if (left.isEmpty || right.isEmpty) { | |
left:::right | |
} else if (left.head < right.head) { | |
List(left.head):::merge(left.tail, right) | |
} else { | |
List(right.head):::merge(left, right.tail) | |
} | |
} | |
val list = List(8,4,3,7,6,5,2,1) | |
println(sort(list)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment