Skip to content

Instantly share code, notes, and snippets.

@markostam
Created November 11, 2016 19:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markostam/70086a5dcca84dbd869c9af1e2c9a169 to your computer and use it in GitHub Desktop.
Save markostam/70086a5dcca84dbd869c9af1e2c9a169 to your computer and use it in GitHub Desktop.
import random
import timeit
# quicksort python
def quicksort(a):
if len(a) < 2:
return a
else:
pivot = a[int(len(a)/2)]
return (quicksort(list(filter(lambda x: x < pivot, a))) +
list(filter(lambda x: x == pivot, a)) +
quicksort(list(filter(lambda x: x > pivot, a))))
# time testing
def test_quicksort(quicksort):
for power in range(1,7):
maxx = 10**power
a = [random.randint(0,maxx) for i in range(maxx)]
start_time = timeit.default_timer()
quicksort(a)
t = timeit.default_timer() - start_time
print('{:07.5f} for array size {}'.format(t,maxx))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment