递归实现块排:快速排序+随机快排
非递归实现块排具体思路如下图:
# -*- coding:utf-8 -*- def quickSort(list): stack=[0] stack.append(len(list)-1) #利用栈存储下标 while stack: j=stack.pop() i=stack.pop() mid=sort(i,j,list) if mid>i+1: stack.append(i) stack.append(mid-1) if mid<j-1: stack.append(mid+1) stack.append(j) # print stack #一次排序,返回下标 def sort(low,hight,list): if (low>hight): return j=hight i=low temp=list[low] while i!=j: while list[j]>=temp and i<j: j-=1 while list[i]<=temp and i<j: i+=1 if i<j: k=list[i] list[i]=list[j] list[j]=k list[low]=list[i] list[i]=temp print list return i list=[5,8,9,2,1,9,6,5] quickSort(list) print list
原文地址:https://www.cnblogs.com/ybf-yyj/p/8831213.html
时间: 2024-10-13 20:59:47