快速排序--python

 1 import random
 2 
 3 
 4 def quick_sort(nums, start, end):
 5     if start >= end:
 6         return
 7     low = start
 8     mid = nums[low]
 9     high = end
10     while low < high:
11         while low < high and mid <= nums[high]:
12             high -= 1
13         nums[low] = nums[high]
14         while low < high and mid > nums[low]:
15             low += 1
16         nums[high] = nums[low]
17     nums[low] = mid
18     quick_sort(nums, start, low - 1)
19     quick_sort(nums, low + 1, end)
20 
21 
22 def test_arr(count, limit):
23     arr = []
24     for i in range(count):
25         arr.append(random.randint(1, limit))
26     return arr
27 
28 
29 if __name__ == '__main__':
30     arr = test_arr(10, 100)
31     print('快速排序前:')
32     print(arr)
33     quick_sort(arr, 0, len(arr) - 1)
34     print('快速排序后:')
35     print(arr)
原文地址:https://www.cnblogs.com/yixiu868/p/11730593.html