Skip to content

Commit ee6ab67

Browse files
committed
Quick Sort List
1 parent a54b634 commit ee6ab67

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

Diff for: DIRECTORY.md

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
* [Quicksort](https://github.com/TheAlgorithms/Scala/blob/master/src/main/scala/Sort/QuickSort.scala)
3030
* [Recursiveinsertionsort](https://github.com/TheAlgorithms/Scala/blob/master/src/main/scala/Sort/RecursiveInsertionSort.scala)
3131
* [Selectionsort](https://github.com/TheAlgorithms/Scala/blob/master/src/main/scala/Sort/SelectionSort.scala)
32+
* [Quicksortlist](https://github.com/TheAlgorithms/Scala/blob/master/src/main/scala/Sort/QuickSortList.scala)
3233
* Test
3334
* Dynamicprogramming
3435
* [Coinchangespec](https://github.com/TheAlgorithms/Scala/blob/master/src/test/scala/DynamicProgramming/CoinChangeSpec.scala)
@@ -56,3 +57,4 @@
5657
* [Quicksortspec](https://github.com/TheAlgorithms/Scala/blob/master/src/test/scala/Sort/QuickSortSpec.scala)
5758
* [Recursiveinsertionsortspec](https://github.com/TheAlgorithms/Scala/blob/master/src/test/scala/Sort/RecursiveInsertionSortSpec.scala)
5859
* [Selectionsortspec](https://github.com/TheAlgorithms/Scala/blob/master/src/test/scala/Sort/SelectionSortSpec.scala)
60+
* [Quicksortlistspec](https://github.com/TheAlgorithms/Scala/blob/master/src/test/scala/Sort/QuickSortListSpec.scala)

Diff for: src/main/scala/Sort/QuickSortList.scala

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package Sort
2+
3+
import scala.annotation.tailrec
4+
5+
/** An implementation of quicksort algorithm to sort of an unsorted integer list
6+
*/
7+
object QuickSortList {
8+
/** @param list
9+
* - a List of unsorted integers
10+
* @return
11+
* - a list of sorted integers
12+
*/
13+
def quickSortList(list: List[Int]): List[Int] = list match {
14+
case Nil => list
15+
case pivot :: other =>
16+
sort(pivot, other, Nil, Nil) match {
17+
case (left, right) => quickSortList(left) ::: pivot :: quickSortList(right)
18+
}
19+
}
20+
21+
/** @param piv
22+
* - a pivot element
23+
* @param rem
24+
* - remaining elements of the list
25+
* @param left
26+
* - a list of integers less than piv
27+
* @param right
28+
* - a list of integer more then piv
29+
* @return
30+
* (l, r)
31+
*/
32+
@tailrec
33+
private def sort(piv: Int, rem: List[Int], left: List[Int], right: List[Int]): (List[Int], List[Int]) = {
34+
rem match {
35+
case head :: tail =>
36+
if (piv > head) sort(piv, tail, head :: left, right)
37+
else sort(piv, tail, left, head :: right)
38+
case Nil => (left, right)
39+
}
40+
}
41+
}

Diff for: src/test/scala/Sort/QuickSortListSpec.scala

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package Sort
2+
3+
import org.scalatest.flatspec.AnyFlatSpec
4+
5+
class QuickSortListSpec extends AnyFlatSpec {
6+
"A Quick Sort List" should "return a sorted version of a list passed to it" in {
7+
val list = List(3, 2, 7, 1, 9, 0)
8+
assert(QuickSortList.quickSortList(list) === List(0, 1, 2, 3, 7, 9))
9+
}
10+
11+
"A Quick Sort List" should "return empty list when empty list is passed to it" in {
12+
val list = List.empty[Int]
13+
assert(QuickSortList.quickSortList(list) === Nil)
14+
}
15+
16+
"A Quick Sort List" should "not modify list" in {
17+
val list = List(0, 1, 2, 3, 7, 9)
18+
assert(QuickSortList.quickSortList(list) === List(0, 1, 2, 3, 7, 9))
19+
}
20+
}

0 commit comments

Comments
 (0)