Skip to content

Commit

Permalink
Quick Sort List
Browse files Browse the repository at this point in the history
  • Loading branch information
thebinarysoul committed Jun 25, 2022
1 parent a54b634 commit 31c7ce3
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
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))
}
}

0 comments on commit 31c7ce3

Please sign in to comment.