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 33 34 35 36
| func sortArray(nums []int) []int { QuickSort(nums, 0, len(nums)-1)
return nums }
func QuickSort(nums []int, low int, high int){ if low < high { partition := RandomPartition(nums, low, high) QuickSort(nums, low, partition-1) QuickSort(nums, partition+1, high) } }
func RandomPartition(nums []int, low int, high int) int { randomIndex := low + rand.Intn(high-low+1) nums[randomIndex], nums[low] = nums[low], nums[randomIndex] return Partition(nums, low, high) }
func Partition(nums []int, low int, high int) int { pivot := nums[low] for low < high { for low < high && pivot <= nums[high] { high-- } nums[low] = nums[high] for low < high && nums[low] <= pivot { low++ } nums[high] = nums[low] } nums[low] = pivot
return low }
|