废话不多说,直接上代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | func sortArray(nums []int) []int { return quicksort(nums , 0 , len(nums) - 1) } func quicksort(nums []int , low int , high int) []int{ if low >= high{ return nums } pivot := nums[low] start := low end := high for low < high{ for low < high && nums[high] >= pivot{ high-- } if low < high{ nums[low] = nums[high] low++ } for low < high && nums[low] < pivot{ low++ } if low < high{ nums[high] = nums[low] high-- } } nums[low] = pivot quicksort(nums , start , low - 1) quicksort(nums , low + 1 , end) return nums } |