Skip to content

Instantly share code, notes, and snippets.

@WangYihang
Created July 12, 2021 14:48
Show Gist options
  • Save WangYihang/545b733ef56f77423098fae7d1d5fdb1 to your computer and use it in GitHub Desktop.
Save WangYihang/545b733ef56f77423098fae7d1d5fdb1 to your computer and use it in GitHub Desktop.
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