tisdag 15 januari 2013

inplace quicksort in python



https://gist.github.com/4537928


def quicksort(l):
work = [(0,len(l)-1)]
while work:
(left,right) = work.pop()
if right < left: 
continue
i_piv = left
piv = l[i_piv]
l0,r0 = left, right
while left < right:
while l[left] <= piv and left < right: left += 1
while l[right] > piv: right -= 1
if left < right: l[left], l[right] = l[right], l[left]
l[i_piv], l[right] = l[right], piv
work.append((l0,right-1))
work.append((right+1,r0))



Inga kommentarer:

Skicka en kommentar