python快速排序算法程序
-- coding: utf-8 --
"""
Created on Thu Oct 15 17:36:26 2020
@author: 叶叶ye
"""
def swap(lyst,i,j):
temp=lyst[i]
lyst[i]=lyst[j]
lyst[j]=temp
def quicksort(lyst):
quicksortHelper(lyst,0,len(lyst)-1)
def quicksortHelper(lyst,left,right):
if left<right:
pivotLocation=partition(lyst,left,right)
quicksortHelper(lyst,left,pivotLocation-1)
quicksortHelper(lyst,pivotLocation+1,right)
def partition(lyst,left,right):
middle=(left+right)//2
pivot=lyst[middle]
lyst[middle]=lyst[right]
lyst[right]=pivot
boundary=left
for index in range(left,right):
if lyst[index]<pivot:
swap(lyst,index,boundary)
boundary+=1
swap(lyst,right,boundary)
return boundary
def insertionSort(lyst):
i=1
while i<len(lyst):
itemToInsert=lyst[i]
j=i-1
while j>=0:
if itemToInsert<lyst[j]:
lyst[j+1]=lyst[j]
j-=1
else:
break
lyst[j+1]=itemToInsert
i+=1
import random
def main(size=100,sort=quicksort):
lyst=[]
for count in range(size):
lyst.append(random.randint(1,size+1))
print(lyst)
sort(lyst)
print(lyst)
if __name__=="__main__":
main()