Skip to content

Instantly share code, notes, and snippets.

@alirezameskin
Created April 14, 2019 19:34
Show Gist options
  • Save alirezameskin/e617a789945256759d240fe80c2b671d to your computer and use it in GitHub Desktop.
Save alirezameskin/e617a789945256759d240fe80c2b671d to your computer and use it in GitHub Desktop.
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