-
-
Notifications
You must be signed in to change notification settings - Fork 390
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a54b634
commit 31c7ce3
Showing
3 changed files
with
63 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
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,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) | ||
} | ||
} | ||
} |
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,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)) | ||
} | ||
} |