Skip to content

Instantly share code, notes, and snippets.

@shigemk2
Created March 16, 2015 14:08
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 shigemk2/d9cfb41bce6e718269ca to your computer and use it in GitHub Desktop.
Save shigemk2/d9cfb41bce6e718269ca to your computer and use it in GitHub Desktop.
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