Initial data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 72d218235f
commit f23f22d71c
199087 changed files with 3378941 additions and 0 deletions

View file

@ -0,0 +1,23 @@
import scala.language.implicitConversions
object MergeSort extends App {
def mergeSort(input: List[Int]): List[Int] = {
def merge(left: List[Int], right: List[Int]): LazyList[Int] = (left, right) match {
case (x :: xs, y :: ys) if x <= y => x #:: merge(xs, right)
case (x :: xs, y :: ys) => y #:: merge(left, ys)
case _ => if (left.isEmpty) right.to(LazyList) else left.to(LazyList)
}
def sort(input: List[Int], length: Int): List[Int] = input match {
case Nil | List(_) => input
case _ =>
val middle = length / 2
val (left, right) = input splitAt middle
merge(sort(left, middle), sort(right, middle + length % 2)).toList
}
sort(input, input.length)
}
}