0

I'm new to Scala and having trouble with this portion of code. I'm getting a stack overflow error on line 18 / 20, but I am not sure why.

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)}
  }
}

1 Answer 1

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)
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.