Created
February 21, 2021 02:00
-
-
Save kazu728/34f77228492b6a37cbe216bf8b440730 to your computer and use it in GitHub Desktop.
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
object MergeSort { | |
def sort(l1: List[Int], l2: List[Int]): List[Int] = (l1, l2) match { | |
case (List(), List()) => List() | |
case (x :: xs, List()) => l1 | |
case (List(), y :: ys) => l2 | |
case (x :: xs, y :: ys) => | |
if (x < y) x :: sort(xs, l2) else y :: sort(ys, l1) | |
} | |
} |
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
import org.scalatest.funspec.AnyFunSpec | |
import org.scalatest.diagrams.Diagrams | |
class MergeSortSpec extends AnyFunSpec with Diagrams { | |
describe("MergeSort") { | |
it("returns merged list") { | |
assert(MergeSort.sort(List(), List()) == List()) | |
assert(MergeSort.sort(List(), List(1, 2)) == List(1, 2)) | |
assert(MergeSort.sort(List(1, 2), List()) == List(1, 2)) | |
assert(MergeSort.sort(List(1, 3), List(2, 4)) == List(1, 2, 3, 4)) | |
assert(MergeSort.sort(List(2, 4), List(1, 3)) == List(1, 2, 3, 4)) | |
assert(MergeSort.sort(List(1, 4), List(1, 3)) == List(1, 1, 3, 4)) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment