Quick Sort Algorithm review-bubble sort bubble sort and quick sort (python implementation)

Source: Internet
Author: User

The process of bubbling sorting is to first compare the keywords of the first record with the keywords of the second record, and, in reverse order, Exchange two records and then compare the keywords for the second record and the third record. And so on, until the n-1 record and the nth record's keywords have been compared. The above process is called the first bubbling sort, then the second trip to the previous n-1 the same operation, ...

The quick sort is an improvement to the bubbling sort, which divides the records into two separate parts through a sort of sequencing, in which some of the recorded keywords are smaller than the other keywords, and the two parts of the records can be sorted recursively in order to achieve the order of the whole sequence.

Single trip partition () Function procedure see the following diagram:

Implemented in Python:

#Coding=utf-8ImportTimea=[49,38,65,97,76,13,27,49]b=[-1,49,38,65,97,76,13,27,49]#where b[0]=-1 This position is a staging unit for storing the List[low in the algorithm below]#Bubble Sort-------------------------------------------------------defBubblesort (list): forIinchReversed (range (Len (a))): forJinchRange (0,len (a)-1):            if(List[j] > list[j+1]): Temp=List[j] List[j]=list[j+1] List[j+1]=Temp J+=1I-=1#Quick Sort-------------------------------------------------------defPartition (List,low,high): PivotKey=list[low]#Pivotlist[0]=List[low] whileLow#alternately scan from both ends of the table to the middle         while(Low andlist[high]>=pivotkey): high-=1List[low]=list[high]#move smaller than pivot record to the low end         while(Low andlist[low]<=PivotKey): Low+=1List[high]=list[low]#moves larger than the pivot record to the high endLIST[LOW]=LIST[0]#pivot record in place    returnLow#return to pivot positiondefQsort (list,low,high):iflow<High:pivotloc=partition (List,low,high);#Split list into splitQsort (list,low,pivotloc-1)#recursive ordering of low sub-tablesQsort (List,pivotloc+1,high)#recursive ordering of GaozidefQuickSort (list): Qsort (list,1,len (list)-1)#---------------------------------------------------------------start_time=time.time () Bubblesort (a) QuickSort (b) Use_time=time.time ()-start_timePrint 'Time used:'+Str (use_time)PrintaPrintB[1:]

Bibliography: "Data structure: C language Edition", Min, 聯繫, Tsinghua University Press

Quick Sort Algorithm review-bubble sort bubble sort and quick sort (python implementation)

Related Article

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.