Python Quick Sort

Source: Internet
Author: User

Import Randomdef rand (n): For I in range (n): Yield Random.randint (0,1000) #创建一个随机数列表def createlist (n): lists=[ ] for I in Rand (n): Lists.append (i) return lists# dig pit Quick Sort def quick_sort (lists,l,r): if (l<r): #x是选 The pivot reference value is taken, and then the data is greater than the base value of the row to the right, less than the base value of the row to the left I,j,x=l,r,lists[l] while (i<j): #j从右边开始遍历, find j<x place, and with L Ists[i] interchange, while (I<j and lists[j]>=x): J-=1 if (i<j): Lists[i]                =LISTS[J] I+=1 #i从左边开始遍历, find j>x place and swap with LISTS[J] while (I<j and lists[i]<=x): I+=1 if (i<j): Lists[j]=lists[i] J-=1 #此时, I is greater than or equal to J, i.e. I left The edges are less than x, the right is greater than X, so now I fill in X, then recursively sort lists[i]=x quick_sort (lists,l,i-1) quick_sort (lists,i+1,r) Li Sts=createlist (+) Print Listsquick_sort (Lists,0,len (lists)-1) Print lists

Python Quick Sort

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.