Skip to content

Instantly share code, notes, and snippets.

@alirezameskin
Last active April 15, 2019 13:42
Show Gist options
  • Save alirezameskin/498c0d4b88e3f57cb8244a734873dbad to your computer and use it in GitHub Desktop.
Save alirezameskin/498c0d4b88e3f57cb8244a734873dbad to your computer and use it in GitHub Desktop.
def getLargest[T <% Ordered[T]](data: List[T]): (T, List[T]) =
data match {
case head :: Nil => (head, Nil)
case head :: tail =>
val (large, remaining) = getLargest(tail)
if (large > head)
(large, head :: remaining)
else
(head, large :: remaining)
}
def bubbleSort[T <% Ordered[T]](data: List[T]): List[T] =
data match {
case Nil => Nil
case _ =>
val (greatest, tail) = getLargest(data)
bubbleSort(tail) ::: List(greatest)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment