Skip to content

Instantly share code, notes, and snippets.

@eivl
Created July 4, 2019 19:24
Show Gist options
  • Select an option

  • Save eivl/f3fcfd553bcd18934bd102b860b866ac to your computer and use it in GitHub Desktop.

Select an option

Save eivl/f3fcfd553bcd18934bd102b860b866ac to your computer and use it in GitHub Desktop.
import random
def quick_sort(list_of_numbers):
if len(list_of_numbers) <= 1:
return list_of_numbers
pivot = random.choice(list_of_numbers)
less_then = [n for n in list_of_numbers if n < pivot]
equal = [n for n in list_of_numbers if n == pivot]
greater_then = [n for n in list_of_numbers if n > pivot]
return quick_sort(less_then) + equal + quick_sort(greater_then)
l = [i for i in range(100_000)]
random.shuffle(l)
sorted_l = quick_sort(l)
print(sorted_l[:10])
@eivl
Copy link
Author

eivl commented Nov 14, 2020

import random

def quick_sort(list_of_numbers):
    if len(list_of_numbers) <= 1:
        return list_of_numbers
    pivot = random.choice(list_of_numbers)

    less_than = [n for n in list_of_numbers if n < pivot]
    equal = [n for n in list_of_numbers if n == pivot]
    greater_than = [n for n in list_of_numbers if n > pivot]
    return quick_sort(less_than) + equal + quick_sort(greater_than)

l = [i for i in range(100_000)]
random.shuffle(l)
sorted_l = quick_sort(l)
print(sorted_l[:10])```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment