题目标题

手写一个快速排序算法

参考解析
  1. def partition(arr,low,high):
  2. i = ( low-1 ) # 最小元素索引
  3. pivot = arr[high]
  4. for j in range(low , high):
  5. # 当前元素小于或等于 pivot
  6. if arr[j] <= pivot:
  7. i = i+1
  8. arr[i],arr[j] = arr[j],arr[i]
  9. arr[i+1],arr[high] = arr[high],arr[i+1]
  10. return ( i+1 )
  11. # arr[] --> 排序数组
  12. # low --> 起始索引
  13. # high --> 结束索引
  14. # 快速排序函数
  15. def quickSort(arr,low,high):
  16. if low < high:
  17. pi = partition(arr,low,high)
  18. quickSort(arr, low, pi-1)
  19. quickSort(arr, pi+1, high)
  20. arr = [10, 7, 8, 9, 1, 5]
  21. n = len(arr)
  22. quickSort(arr,0,n-1)
  23. print("排序后的数组:")
  24. for i in range(n):
  25. print("%d" %arr[i])