Algorithms: design and analysis,
Part 1
Chapter 1 describes the partitioning algorithm, namely, DC. This chapter describes quick sorting. The difficulty of the job has been increased. After the problem sets were done twice, they accidentally scored only four points, and the programming job was done twice.
This job is to achieve fast sorting and change the selection method of the sentry element to compare the performance. The Sentinel can be the first and last elements, or the second largest number of the first, last, and middle elements.
The sorting method of list can be used when the last method is implemented.
The Code is as follows:
def quickSort(arrayall): if len(arrayall)<=1:return arrayall,0 #sel p #p=0 #p=len(arrayall)-1 p=(len(arrayall)-1)/2 a=[(arrayall[0],0),(arrayall[p],p),(arrayall[-1],len(arrayall)-1)] a.sort() tmp,p=a[1] if p!=0: arrayall[0],arrayall[p]=arrayall[p],arrayall[0] indi=1 for indj in range(1,len(arrayall)): if arrayall[indj]<arrayall[0]: arrayall[indi],arrayall[indj]=arrayall[indj],arrayall[indi] indi+=1 arrayall[0],arrayall[indi-1]=arrayall[indi-1],arrayall[0] arrayallp,mp=quickSort(arrayall[:indi-1]) arrayallq,mq=quickSort(arrayall[indi:]) return arrayallp+[arrayall[indi-1]]+arrayallq,mp+mq+len(arrayall)-1f=open('QuickSort.txt','r')listall=f.read()listall=[int(x) for x in listall.split()]f.close()sortedlist,m=quickSort(listall)print m
Of course, the experimental result is that the last method has superior performance.