Skip to content

Instantly share code, notes, and snippets.

@alirezameskin
Last active April 15, 2019 14:13
Show Gist options
  • Save alirezameskin/04303ddeab81eda565dcd4b02320b8a0 to your computer and use it in GitHub Desktop.
Save alirezameskin/04303ddeab81eda565dcd4b02320b8a0 to your computer and use it in GitHub Desktop.
def partition[T <% Ordered[T]](elm: T, list: List[T]): (List[T], List[T]) = {
doPartition(elm, list, Nil, Nil)
def doPartition[T <% Ordered[T]](elm: T, list: List[T], smallerItems: List[T], largerItems: List[T]): (List[T], List[T]) =
list match {
case Nil => (smallerItems, largerItems)
case head :: tail =>
if (head < elm)
doPartition(elm, tail, head :: smallerItems, largerItems)
else
doPartition(elm, tail, smallerItems, head :: largerItems)
}
}
def quickSort[T <% Ordered[T]](data: List[T]): List[T] =
data match {
case Nil => Nil
case head :: Nil => List(head)
case head :: tail =>
val (list1, list2) = partition(head, tail)
val smallerList = quickSort(list1)
val biggerList = quickSort(list2)
smallerList ::: (head :: biggerList)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment