后台小程序开发的全方位指南
688
2022-10-26
python实现快速排序
'''Created on 2017-1-6@author: admin'''def quickSort(source,start,end): if start==end: return index=shuffle(source, start, end) if(index-1>start): quickSort(source, start,index-1) if index+1<=end: quickSort(source, index+1, end)def shuffle(source,start,end): leftIndex=start; rightIndex=end sepValue=source[start] directionRight=True while leftIndex!=rightIndex: if directionRight: if sepValue>source[rightIndex]: source[leftIndex]=source[rightIndex] leftIndex=leftIndex+1 directionRight=False else: rightIndex=rightIndex-1 else: if sepValue<=source[leftIndex]: source[rightIndex]=source[leftIndex] rightIndex=rightIndex-1 directionRight=True else: leftIndex+=1 source[leftIndex]=sepValue; return leftIndex if __name__ == '__main__': source=[7,3,2,9,4,8,5,6,1] quickSort(source,0,len(source)-1) for i in range(0,len(source)): print(source[i],end=',')
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~