Skip to content

Instantly share code, notes, and snippets.

@EdisonChendi
Last active April 19, 2021 20:50
Show Gist options
  • Save EdisonChendi/828c5470b08f277a8c45de2efe8f86b1 to your computer and use it in GitHub Desktop.
Save EdisonChendi/828c5470b08f277a8c45de2efe8f86b1 to your computer and use it in GitHub Desktop.
toy quick sort in python, just one line!
# -*- coding: UTF-8 -*-
quicksort = lambda l: quicksort([i for i in l[1:] if i < l[0]]) + [l[0]] + quicksort([j for j in l[1:] if j >= l[0]]) if l else []
## but readability counts
# def quicksort(l):
# if not l: return []
# pivot = l.pop()
# less = [i for i in l if i < pivot]
# greater = [j for j in l if j >= pivot]
# return quicksort(less) + [pivot] + quicksort(greater)
def run():
from random import randint
l = [randint(0, 50) for i in range(10)]
print(l)
print(quicksort(l))
if __name__ == "__main__":
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment