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