Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quick Sort List #68

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* [Quicksort](https://github.com/TheAlgorithms/Scala/blob/master/src/main/scala/Sort/QuickSort.scala)
* [Recursiveinsertionsort](https://github.com/TheAlgorithms/Scala/blob/master/src/main/scala/Sort/RecursiveInsertionSort.scala)
* [Selectionsort](https://github.com/TheAlgorithms/Scala/blob/master/src/main/scala/Sort/SelectionSort.scala)
* [Quicksortlist](https://github.com/TheAlgorithms/Scala/blob/master/src/main/scala/Sort/QuickSortList.scala)
* Test
* Dynamicprogramming
* [Coinchangespec](https://github.com/TheAlgorithms/Scala/blob/master/src/test/scala/DynamicProgramming/CoinChangeSpec.scala)
Expand Down Expand Up @@ -56,3 +57,4 @@
* [Quicksortspec](https://github.com/TheAlgorithms/Scala/blob/master/src/test/scala/Sort/QuickSortSpec.scala)
* [Recursiveinsertionsortspec](https://github.com/TheAlgorithms/Scala/blob/master/src/test/scala/Sort/RecursiveInsertionSortSpec.scala)
* [Selectionsortspec](https://github.com/TheAlgorithms/Scala/blob/master/src/test/scala/Sort/SelectionSortSpec.scala)
* [Quicksortlistspec](https://github.com/TheAlgorithms/Scala/blob/master/src/test/scala/Sort/QuickSortListSpec.scala)
41 changes: 41 additions & 0 deletions src/main/scala/Sort/QuickSortList.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package Sort

import scala.annotation.tailrec

/** An implementation of quicksort algorithm to sort of an unsorted integer list
*/
object QuickSortList {
/** @param list
* - a List of unsorted integers
* @return
* - a list of sorted integers
*/
def quickSortList(list: List[Int]): List[Int] = list match {
case Nil => list
case pivot :: other =>
sort(pivot, other, Nil, Nil) match {
case (left, right) => quickSortList(left) ::: pivot :: quickSortList(right)
}
}

/** @param piv
* - a pivot element
* @param rem
* - remaining elements of the list
* @param left
* - a list of integers less than piv
* @param right
* - a list of integers great than piv
* @return
* (l, r)
*/
@tailrec
private def sort(piv: Int, rem: List[Int], left: List[Int], right: List[Int]): (List[Int], List[Int]) = {
rem match {
case head :: tail =>
if (piv > head) sort(piv, tail, head :: left, right)
else sort(piv, tail, left, head :: right)
case Nil => (left, right)
}
}
}
20 changes: 20 additions & 0 deletions src/test/scala/Sort/QuickSortListSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package Sort

import org.scalatest.flatspec.AnyFlatSpec

class QuickSortListSpec extends AnyFlatSpec {
"A Quick Sort List" should "return a sorted version of a list passed to it" in {
val list = List(3, 2, 7, 1, 9, 0)
assert(QuickSortList.quickSortList(list) === List(0, 1, 2, 3, 7, 9))
}

"A Quick Sort List" should "return empty list when empty list is passed to it" in {
val list = List.empty[Int]
assert(QuickSortList.quickSortList(list) === Nil)
}

"A Quick Sort List" should "not modify list" in {
val list = List(0, 1, 2, 3, 7, 9)
assert(QuickSortList.quickSortList(list) === List(0, 1, 2, 3, 7, 9))
}
}