Algorithms Part 1-question 2-quicksort-quick Sorting Algorithm

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.