forked from kennyledet/Algorithm-Implementations
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request kennyledet#389 from aayushKumarJarvis/master
Scala Implementation of QuickSort
- Loading branch information
Showing
2 changed files
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
} | ||
|
||
} |