Last active
July 8, 2019 09:09
-
-
Save ababup1192/eb628a6148c34965c62a86f88b8c4074 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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