From 31c7ce3b7119a1e42c95021a8b784bfdd02908b3 Mon Sep 17 00:00:00 2001 From: thebinarysoul Date: Sat, 25 Jun 2022 15:52:50 +0300 Subject: [PATCH] Quick Sort List --- DIRECTORY.md | 2 + src/main/scala/Sort/QuickSortList.scala | 41 +++++++++++++++++++++ src/test/scala/Sort/QuickSortListSpec.scala | 20 ++++++++++ 3 files changed, 63 insertions(+) create mode 100644 src/main/scala/Sort/QuickSortList.scala create mode 100644 src/test/scala/Sort/QuickSortListSpec.scala diff --git a/DIRECTORY.md b/DIRECTORY.md index b48aa26..ac87a1e 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -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) @@ -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) diff --git a/src/main/scala/Sort/QuickSortList.scala b/src/main/scala/Sort/QuickSortList.scala new file mode 100644 index 0000000..612eebc --- /dev/null +++ b/src/main/scala/Sort/QuickSortList.scala @@ -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) + } + } +} diff --git a/src/test/scala/Sort/QuickSortListSpec.scala b/src/test/scala/Sort/QuickSortListSpec.scala new file mode 100644 index 0000000..71dfe60 --- /dev/null +++ b/src/test/scala/Sort/QuickSortListSpec.scala @@ -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)) + } +}