2014-02-26 147 views
0

我是新來的斯卡拉和有這部分代碼的麻煩。我在第18/20行遇到堆棧溢出錯誤,但我不知道爲什麼。斯卡拉堆棧溢出錯誤

def quicksort(list : List[Int]) : List[Int] = list match { 
    case Nil => List() 
    case x::Nil => List(x) 
    case x::xs => { 
    val lesserList = partitionLesser(list.tail, list(0)) 
    val greaterList = partitionGreater(list.tail, list(0)) 
    quicksort(lesserList ::: List(x) ::: greaterList) 
    } 
} 

def partitionLesser(list : List[Int], pivot : Int) : List[Int] = list match{ 
    case Nil => List() 
    case x::Nil => List(x) 
    case x::xs => { 
    if(x <= pivot) { x :: partitionLesser(list.tail, pivot) } 
    else { partitionLesser(list.tail, pivot) } 
    } 
} 

def partitionGreater(list : List[Int], pivot : Int) : List[Int] = list match { 
    case Nil => List() 
    case x::Nil => List(x) 
    case x::xs => { 
    if(x > pivot) { x :: partitionGreater(list.tail, pivot) } 
    else { partitionLesser(list.tail, pivot)} 
    } 
} 

回答

2
def quicksort(list : List[Int]) : List[Int] = list match { 
    case Nil => List() 
    case x::Nil => List(x) 
    case x::xs => { 
    val lesserList = partitionLesser(list.tail, list(0)) 
    val greaterList = partitionGreater(list.tail, list(0)) 
    // quicksort(lesserList ::: List(x) ::: greaterList) 
    quicksort(lesserList) ::: List(x) ::: quicksort(greaterList) 
    } 
}