Quick sort
September 29th, 2007 by Daniel Høyer Iversen
Quick sort in Python
def sort(A):
if len(A) <= 1:
return A
small = []
middle = []
big = []
temp = int((A[0][0]+A[len(A)-1][0])/2)
for x in A:
if x[0] < temp:
small.append(x)
elif x[0] == temp:
middle.append(x)
elif x[0] > temp:
big.append(x)
A = sort(small)
A.extend(middle)
A.extend(sort(big))
return A
http://en.wikipedia.org/wiki/Quicksort :
Quicksort is a well-known sorting algorithm developed by C. A. R. Hoare that, on average, makes O(nlogn) (big O notation) comparisons to sort n items. However, in the worst case, it makes Θ(n2) comparisons. Typically, quicksort is significantly faster in practice than other Θ(nlogn) algorithms, because its inner loop can be efficiently implemented on most architectures, and in most real-world data it is possible to make design choices which minimize the probability of requiring quadratic time.