Skip to content

Instantly share code, notes, and snippets.

@juanplopes
Created July 23, 2015 13:15
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 juanplopes/704d20dbd47cc7042e60 to your computer and use it in GitHub Desktop.
Save juanplopes/704d20dbd47cc7042e60 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
from __future__ import print_function
import timeit, random, sys
if sys.version_info >= (3, ): xrange=range
def partition(data, begin, end):
value = data[end-1]
pivot = begin
for i in xrange(begin, end-1):
if data[i] < value:
data[pivot], data[i] = data[i], data[pivot]
pivot += 1
data[pivot], data[end-1] = data[end-1], data[pivot]
return pivot
def qsort(data, begin, end):
while begin+1 < end:
pivot = partition(data, begin, end)
if pivot-begin < end-pivot:
qsort(data, begin, pivot)
begin = pivot+1
else:
qsort(data, pivot+1, end)
end = pivot
V = random.sample(xrange(1,99999999),1000000)
print(timeit.timeit(lambda: qsort(V, 0, len(V)), number=1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment