快排 python3
Class Solution:
def QuickSort(self, array, low, high):
if low >= high:
return
i = low, j = high, index = array[i] #取数组最左侧数值作为基准
while(i<j):
while((i<j) and (array[j] >= index)): #从右向左寻找第一个小于基准的数值
j -= 1
if i<j:
array[i] = array[j] #将该值填到左侧,i值右移【右侧该值位置空出】
i +=1
while((i<j) and (array[i] < index): #继续从左向右寻找第一个大于基准的数值
i += 1
if i<j:
array[j] = array[i] #将该值填到右侧空位,j值左移【左侧该值位置空出】
j -=1
array[i] = index #将基准值填入中间空位
QuickSort(array,low,i-1) #分治、迭代实现
QuickSort(array,i+1,high)