Created
April 14, 2019 19:32
-
-
Save alirezameskin/aabe89dc6c51e642906c690cfa84aa18 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 selectionSort[T <% Ordered[T]](data: List[T]): List[T] = | |
data match { | |
case Nil => Nil | |
case head :: Nil => List(head) | |
case head :: tail => | |
val smallest = tail.min | |
val smallIndex = tail.indexOf(smallest) | |
if (head <= smallest) | |
head :: selectionSort(tail) | |
else { | |
val (part1, part2) = tail.splitAt(smallIndex) | |
part2.head :: selectionSort(part1 ::: head :: part2.tail) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment