Skip to content

Instantly share code, notes, and snippets.

@nsfyn55
Created September 20, 2012 18:09
Show Gist options
  • Save nsfyn55/3757441 to your computer and use it in GitHub Desktop.
Save nsfyn55/3757441 to your computer and use it in GitHub Desktop.
Bubble Sort
object App{
def main(args: Array[String]){
println(sort(List(6,1,56,3,5,4,9)))
}
def sort(xs:List[Int]):List[Int] = {
def innerSort(xs: List[Int], count:Int): List[Int] = count match {
case 0 => xs
case _ => innerSort(pass(xs), count-1)
}
innerSort(xs, xs.length)
}
def pass(xs:List[Int]):List[Int] = xs match {
case Nil => Nil
case x::Nil => x::Nil
case l::r::xs if(l>r) => r::pass(l::xs)
case l::r::xs => l::pass(r::xs)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment