Skip to content

Instantly share code, notes, and snippets.

@ababup1192
Last active July 8, 2019 09:09
Show Gist options
  • Save ababup1192/eb628a6148c34965c62a86f88b8c4074 to your computer and use it in GitHub Desktop.
Save ababup1192/eb628a6148c34965c62a86f88b8c4074 to your computer and use it in GitHub Desktop.
def insertionSort(list: List[Int]): List[Int] = {
list match {
case Nil => Nil
case List(x) => List(x)
case x :: xs => {
insert(x, insertionSort(xs))
}
}
}
def insert(x: Int, lst: List[Int]): List[Int] = {
lst match {
case Nil => List(x)
case y :: ys =>
if (x < y) x :: y :: ys
else y :: insert(x, ys)
}
}
println(insertionSort(List(5, 3, 1, 2, 4)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment