diff --git a/Quick_Sort/Scala/aayushKumarJarvis/QuickSort.scala b/Quick_Sort/Scala/aayushKumarJarvis/QuickSort.scala new file mode 100644 index 00000000..22077a58 --- /dev/null +++ b/Quick_Sort/Scala/aayushKumarJarvis/QuickSort.scala @@ -0,0 +1,19 @@ +/* SCALA Implementation of Quick Sort. + + Though the corner cases are covered. But still if you find any additions to it, + please do add a test for it. + + Any improvements/tests in the code is highly appreciated. + */ + +class QuickSort { + + def quickSort(listToBeSorted: List[Int]): List[Int] = + if (listToBeSorted.isEmpty) { + listToBeSorted + } + else { + val (smaller, bigger) = listToBeSorted.tail partition (_ < listToBeSorted.head) + quickSort(smaller) ::: listToBeSorted.head :: quickSort(bigger) + } +} \ No newline at end of file diff --git a/Quick_Sort/Scala/aayushKumarJarvis/QuickSortTest.scala b/Quick_Sort/Scala/aayushKumarJarvis/QuickSortTest.scala new file mode 100644 index 00000000..25955694 --- /dev/null +++ b/Quick_Sort/Scala/aayushKumarJarvis/QuickSortTest.scala @@ -0,0 +1,14 @@ +import org.scalatest.{Matchers, FunSuite} + +class QuickSortTest extends FunSuite with Matchers { + + test("Quick Sort should sort") { + val objectForQuickSort = new QuickSort + + objectForQuickSort.quickSort(List(2,1,55,21,58)) should be(List(1,2,21,55,58)) + objectForQuickSort.quickSort(List(1,1,2,2,6,6,5,5)) should be(List(1,1,2,2,5,5,6,6)) + objectForQuickSort.quickSort(List(0,22,1,45)) should be(List(0,1,22,45)) + objectForQuickSort.quickSort(List(100,99,98,97,91)) should be(List(91,97,98,99,100)) + } + +} \ No newline at end of file