相对于算法实现(一),下面这个实现过程更容易理解:
#!/usr/bin/python
class sort:
def quicksort2(self,seq,left,right):
i = left;
j = right;
middle = seq[left];
while i<=j:
while seq[i]< middle and i<right:
i += 1;
while seq[j]> middle and j>left:
j -= 1;
if i <= j:
temp = seq[i];
seq[i] = seq[j];
seq[j] = temp;
i += 1;
j += 1;
if left<j:
self.quicksort2(seq,left,j);
if right>i:
self.quicksort2(seq,i,right);
a = sort();
seq = [5,6,67,0,-1,-43,12,79];
a.quicksort2(seq,0,7);
print (seq);
运行结果如图: