def partition(data, left, right):
pivot = data[left]
i = left
j = right
while i < j:
while i < j and data[j] >= pivot:
j -= 1
while i < j and data[i] <= pivot:
i += 1
if i < j:
data[i], data[j] = data[j], data[i]
data[left] = data[i]
data[i] = pivot
return i
def quick_sort1(data, left, right):
if left >= right:
return
i = partition(data, left, right)
quick_sort1(data, left, i-1)
quick_sort1(data, i+1, right)
def quick_sort2(data):
if len(data) in (0, 1):
return
s = [0, len(data)-1]
while s:
right = s.pop()
left = s.pop()
if left >= right:
continue
i = partition(data, left, right)
s.append(left)
s.append(i-1)
s.append(i+1)
s.append(right)
网友评论