python basis - Quick Sort

1. Quick Sort

  Quick sort is an exchange sort .

  Quick sort proposed by CAR Hoare in 1962.

  The basic idea is: a trip by ordering the data to be sorted is divided into two separate parts: the division of the left side is smaller than its number on the right is bigger than its number .

  Then this way the two parts of the data are quickly sort, the entire sorting process can be recursively, in order to achieve the whole data into an ordered sequence.

  Detailed illustrations tend to be more explanatory power than piles of words

  The following figure illustrates the moment, but this dynamic map a bit complicated, I also see reeling

  

2. ado, directly on the code

  

 1 # 递归实现  快排
 2 def quickSort(arr):
 3     if len(arr) <= 1:
 4         return arr
 5     mid = arr[len(arr) // 2]
 6     left, right = [], []
 7     arr.remove(mid)
 8     for item in arr:
 9         if item >= mid:
10             right.append(item)
11         else:
12             left.append(item)
13     return quickSort(left) + [mid] + quickSort(right)
14 
15 l2 =list(random.choices([x for x in range(100)],k=20))
16 print('l2=',l2)
17 l2=quickSort(l2)
18 print('l2=',l2)

 

Guess you like

Origin www.cnblogs.com/jeffrey04118110/p/11784292.html