Created
March 16, 2015 14:08
-
-
Save shigemk2/d9cfb41bce6e718269ca to your computer and use it in GitHub Desktop.
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
def bsort(list: List[Int]): List[Int] = { | |
def bswap(xs: List[Int]): List[Int] = { | |
xs match { | |
case xs if xs.length == 1 => List(xs.head) | |
case xs => { | |
lazy val ys = bswap(xs.tail) | |
if (xs.head > ys.head) ys.head :: xs.head :: ys.tail | |
else xs.head :: ys.head :: ys.tail | |
} | |
} | |
} | |
lazy val ys = bswap(list) | |
list match { | |
case list if list.isEmpty => List() | |
case list => ys.head :: bsort(ys.tail) | |
} | |
} | |
println(bsort(List(4,3,1,5,2))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment