Python Quicksort Demo

Source: Internet
Author: User

__author__ = ' student ' Quicksortstep 1, choose one pivot, such as Pivot=la[0]step 2, scan the data from right side, find Data less than pivot, then swap this with Pivotpivot=1 [4] 5 7 3 9 [J]then scan from left side, find data greater tha n Pivot, then swap the position J and I4 [] 7 3 9 5when i>=j then finish one loop. Then put the pivot in the I; All data is dived by pivot now. than pivot and right is greater than Pivot.think, step by step, do it and try Somethingstep 3. Then you have the parts to sort, the left part and the right part.recursive call the This method to sort ' import randomdef quicksort (la,l,r): If L>=r:return left=l;right=r Pivot=la[left] while left < Right:while left&lt La[right]>pivot:right-=1 if Left<right:la[left]=la[right] left +=1 while Left<right and la[left]<pivot:left+=1 if left<right:la[right]=la[ Left    La[left]=pivot quicksort (la,l,left-1) quicksort (LA,LEFT+1,R) def quicksort2 (LA): If Len (LA) <=1:retu RN La return quicksort2 ([LT for Lt in La[1:] if lt<la[0]]) + LA[0:1]+QUICKSORT2 ([ge for GE in la[1:] if ge>=la[0]] ) Import Syssys.setrecursionlimit (999) la=[]def generatenumbers (La,len): For x in range (len): La.extend ([Random.ra Ndint (1,50)]) generatenumbers (la,1000) Print Laquicksort (La,0,len (LA)-1) Print LA

Python Quicksort demo

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.