__author__ = "yang xin" array=[] def quickSort(left,right): if left > right: return temp = array[left] i = left j = right while i < j: if array[j] >= temp and i<j: #if j-1>=left: j -= 1 if array[i] <= temp and i<j: # if i+1<right: i += 1 if i < j: tag = array[i] array[i] = array[j] array[j] = tag array[left] = array[i] array[i]=temp; quickSort(left,i-1) if i+1<right: quickSort(i+1,right) return n=input("请输入排序数字的个数:")m=int(n)for i in range(m): array.append(int(input()))quickSort(0,m-1)#特别注意第二个参数的问题,因为在range里面是做闭右开,防止list index out of range错误,在这控制他为左闭右闭print(array)
原文地址:https://www.cnblogs.com/yangdagaoge/p/9287043.html
时间: 2024-11-10 16:25:00