Skip to content

Instantly share code, notes, and snippets.

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