Algorithm 3-Advanced sorting

Source: Internet
Author: User
Tags shuffle

Quick sort: Quick and fast line idea: Take an element P (the first element), so that the element p, the list is divided into two parts p, the left is smaller than P, the right side is larger than p; recursion completes the sort.
ImportRandom fromTimewrapImport*ImportCopyImportSyssys.setrecursionlimit (100000)defpartition (Li, left, right):#ri = Random.randint (left, right)    #Li[left], Li[ri] = Li[ri], Li[left]TMP =Li[left] whileLeft <Right : whileLeft < Right andLi[right] >=Tmp:right-= 1Li[left]=Li[right] whileLeft < Right andLi[left] <=Tmp:left+ = 1Li[right]=Li[left] Li[left]=tmpreturn Leftdef_quick_sort (Li, left, right):ifLeft < right:#There are at least two elementsMID =partition (Li, left, right) _quick_sort (Li, left, mid-1) _quick_sort (Li, Mid+1, right) @cal_timedefQuick_sort (LI):return_quick_sort (li, 0, Len (LI)-1) @cal_timedefSys_sort (LI): li.sort () Li= List (Range (10000)) Random.shuffle (LI)#Sys_sort (LI1)Quick_sort (LI)
Quick-Line sample code

Heap sorting builds the heap to get the top element, removes the top of the heap for the largest element, and puts the last element of the heap on top of the heap, where the heap can be re-ordered by a single adjustment. The top element of the heap is the second largest element. Repeat step 3 until the heap becomes empty.
 fromTimewrapImport*ImportRandomdef_sift (Li, Low, high):""":p Aram Li::p Aram Low: Location of the heap root node:p Aram High: The location of the heap with the most one node: return:"""I= Low#Father's positionj = 2 * i + 1#the location of the childTMP = Li[low]#former Governor     whileJ <=High :ifJ + 1 <= High andLi[j + 1] > Li[j]:#If the right child is present and the right child is largerJ + = 1ifTMP < LI[J]:#if the original governor is smaller than the childLi[i] = Li[j]#move the child up one leveli =J J= 2 * i + 1Else: Li[i]= tmp#The governor puts it in the corresponding position (cadre)             Break    Else: Li[i]= tmp#The governor is placed in the corresponding position (Villager/leaf node)defSift (Li, Low, high):""":p Aram Li::p Aram Low: Location of the heap root node:p Aram High: The location of the heap with the most one node: return:"""I= Low#Father's positionj = 2 * i + 1#the location of the childTMP = Li[low]#former Governor     whileJ <=High :ifJ + 1 <= High andLI[J+1] > Li[j]:#If the right child is present and the right child is largerJ + = 1ifTMP < LI[J]:#if the original governor is smaller than the childLi[i] = Li[j]#move the child up one leveli =J J= 2 * i + 1Else:             BreakLi[i]=Tmp@cal_timedefHeap_sort (LI): n=Len (LI)#1. Build a heap     forIinchRange (n//2-1,-1,-1): Sift (Li, I, n-1)    #2.     forJinchRange (n-1,-1,-1):#J indicates the position of the last element of the heapLi[0], li[j] =Li[j], li[0]#one element less than the size of the heap (j-1)Sift (li, 0, J-1) Li= List (Range (10000)) Random.shuffle (LI) heap_sort (LI)Print(LI)#li=[2,9,7,8,5,0,1,6,4,3]#sift (li, 0, Len (LI)-1)#Print (LI)
Sample Code

Merge sort Suppose now the list is organized in two paragraphs, how to combine it into an ordered list this operation is called a merge. Decomposition: The smaller the list forewarned, until it is divided into an element. Termination condition: An element is ordered. Merge: Merges two ordered lists, and the list is getting bigger.
ImportRandom fromTimewrapImport*ImportCopyImportSYSdefMerge (Li, Low, Mid, high): I=Low J= Mid + 1ltmp= []     whileI <= mid andJ <=High :ifLi[i] <Li[j]: Ltmp.append (li[i]) I+ = 1Else: Ltmp.append (Li[j]) J+ = 1 whileI <=mid:ltmp.append (li[i]) I+ = 1 whileJ <=high:ltmp.append (Li[j]) J+ = 1Li[low:high+1] =ltmpdef_merge_sort (Li, Low, high):ifLow < High:#minimum of two elementsMid = (low + high)//2_merge_sort (Li, Low, mid) _merge_sort (Li, Mid+1, High) merge (Li, Low, Mid, high)Print(li[low:high+1])defMerge_sort (LI):return_merge_sort (li, 0, Len (LI)-1) Li= List (Range (16)) Random.shuffle (LI)Print(LI) merge_sort (LI)Print(LI)
Sample Code

Algorithm 3-Advanced sorting

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.