Quicksort in Python

Source: Internet
Author: User

1 #Quick Sort2 defquicksort (Low, High, array):
Limit = Ten3 ifLow < 0orHigh >=Len (array):4 return5 6 if(high-low) <limit:7 Insertsort (Low,high,array)8 return9 TenMiddle = (Low+high)/2 One A ifArray[low] >Array[middle]: - Exchange (Low,middle,array) - the ifArray[middle]>Array[high]: - Exchange (Middle,high,array) - - ifArray[low]>Array[high]: + Exchange (Low,high,array) - +Pivot =Array[middle] A atExchange (middle,high-1, Array) - -Low_ind = low + 1 - -High_ind = High-2 - in whileLow_ind <=High_ind: - whileArray[low_ind] <Pivot: toLow_ind+=1 + - whileArray[high_ind] >Pivot: theHigh_ind-=1 * $ ifLow_ind <High_ind:Panax Notoginseng Exchange (Low_ind,high_ind,array) - theLow_ind+=1 +High_ind-=1 A Else: the Break + -Exchange (low_ind,high-1, Array) $ $Quicksort (low,low_ind-1, Array) -Quicksort (low_ind+1, High,array) - the #Exchange-Element - defExchange (INDEX1, Index2, array):Wuyitemp =Array[index1] theARRAY[INDEX1] =Array[index2] -ARRAY[INDEX2] =Temp Wu - #Insertion Sort About defInsertsort (low,high,array): $ ifLow < 0orHigh >=Len (array): - return - -outer = low + 1 A + while(outer<=High ): theInner = Low - while(inner<outer): $ ifarray[outer]<Array[inner]: the Array.insert (Inner,array[outer]) the delArray[outer+1] the Break theInner+=1 -Outer+=1 in the #Test thearray=[] About theQuicksort (0,len (array)-1, Array) the the PrintArray

The first essay, it is used to write a quick line, after all, all kinds of interviews will be asked.

I use the three-digit median method to take pivot, and then swap the insertion sort when the element to be sorted is less than limit (set to 10).

First post, encourage each other

Postscript:

When you're done, you'll see a blog post. Http://www.cnblogs.com/figure9/archive/2010/12/10/1902711.html study the shortest possible number of lines of code can be written out of the fast line.

Reproduced part of the code is as follows:

1 def Q_sort (L): 2     return if Else  for inch if  for inch if x>=l[0]])

This side does not consider pivot selection, always choose the first as pivot, so it is possible to degenerate into the worst case. But so little code is written or very powerful, and shortly before Python, two lines of code have been studied for a long time.

Quicksort in Python

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.