对于一个想找工作的人来说,快排必须能手写出来。
快排原理无需解释,直接给出python代码:
def quicksort(num,low,high):
if low<high:
location=partition(num,low,high)
quicksort(num,low,location-1)
quicksort(num,location+1,high)
def partition(num,low,high):
pivot=num[low]
while(low<high):
while(low<high and num[high]>pivot):
high-=1
while (low < high and num[low] < pivot):
low += 1
temp=num[low]
num[low]=num[high]
num[high]=temp
num[low]=pivot
return low
pai=[5,12,79,2,4,8,6]
quicksort(pai,0,len(pai)-1)
print(pai)