您的位置:首页 > 编程语言 > Python开发

python实现快速排序

2017-01-06 13:51 417 查看
'''
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=',')
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: