pyhton 手撕快排
1 import numpy as np 2 3 4 def quicksort(arr, low, high): # [a,b] 5 if low < high: 6 m = partition(arr, low, high) 7 quicksort(arr, low, m - 1) 8 quicksort(arr, m + 1, high) 9 10 11 def partition(arr, low, high): # [a,b] 12 first_num = arr[low] 13 i = low 14 j = high 15 while i < j: 16 while i < j and arr[j] >= first_num: j -= 1 17 arr[i] = arr[j] 18 while i < j and arr[i] <= first_num: i += 1 19 arr[j] = arr[i] 20 arr[i] = first_num 21 return i 22 23 24 arr = np.random.randint(999, size=100) 25 quicksort(arr, 0, len(arr) - 1) 26 print(arr)