Created
April 14, 2019 19:34
-
-
Save alirezameskin/e617a789945256759d240fe80c2b671d 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
def insertElement[T <% Ordered[T]](elm: T, sorted: List[T]): List[T] = | |
sorted match { | |
case Nil => elm :: sorted | |
case head :: tail if head < elm => head :: insertElement(elm, tail) | |
case _ => elm :: sorted | |
} | |
def insertionSort[T <% Ordered[T]](list: List[T]): List[T] = | |
list match { | |
case Nil => list | |
case head :: tail => | |
val sorted = insertionSort(tail) | |
insertElement(head, sorted) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment