Created
July 12, 2021 14:48
-
-
Save WangYihang/545b733ef56f77423098fae7d1d5fdb1 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import random | |
def quicksort(data): | |
''' | |
quicksort: sort a list via quicksort (recurrsive version) | |
args: | |
data: the list to be sort | |
return: | |
the sorted list (allocated) | |
''' | |
if len(data) in [0, 1]: return data | |
pivot, remain = data[0], data[1:] | |
left = [i for i in remain if i < pivot] | |
right = [i for i in remain if i >= pivot] | |
return quicksort(left) + [pivot] + quicksort(right) | |
def test(): | |
x = list(range(0x20)) | |
random.shuffle(x) | |
e = list(range(0x20)) | |
assert quicksort(x) == e | |
def main(): | |
test() | |
if __name__ == "__main__": | |
test() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment