Last active
April 15, 2019 14:13
-
-
Save alirezameskin/04303ddeab81eda565dcd4b02320b8a0 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 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