'scala bubbleSort endless recursion
I am trying to get this implementation to work, tho it ends in an infinite loop. What am I missing?
def bubbleSort(l: List[Int]): List[Int] = if(isSorted(l)) l else bubbleSort(bubble(l))
def bubble(l: List[Int]): List[Int] = l match {
case Nil => List()
case x::Nil => List(x)
case x::y::ys => if (x<y) x::bubble(y::ys) else y::bubble(x::ys)
//case x::xs => if (x<xs.head) bubble(xs) else bubble ()
}
def isSorted(l: List[Int]): Boolean = l match {
case Nil => true
case x::Nil => true
case x::y::ys => if(y>x) false else isSorted(y::ys)
}
val l = List(12,35,15,2,1,2,17,51,34)
println(bubble(l))
println(bubbleSort(l))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
