|
46 | 46 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
47 | 47 |
|
48 | 48 | ```python
|
49 |
| - |
| 49 | +class Solution: |
| 50 | + def sortArray(self, nums: List[int]) -> List[int]: |
| 51 | + def quick_sort(nums, left, right): |
| 52 | + if left >= right: |
| 53 | + return |
| 54 | + i, j = left - 1, right + 1 |
| 55 | + x = nums[(left + right) >> 1] |
| 56 | + while i < j: |
| 57 | + while 1: |
| 58 | + i += 1 |
| 59 | + if nums[i] >= x: |
| 60 | + break |
| 61 | + while 1: |
| 62 | + j -= 1 |
| 63 | + if nums[j] <= x: |
| 64 | + break |
| 65 | + if i < j: |
| 66 | + nums[i], nums[j] = nums[j], nums[i] |
| 67 | + quick_sort(nums, left, j) |
| 68 | + quick_sort(nums, j + 1, right) |
| 69 | + |
| 70 | + quick_sort(nums, 0, len(nums) - 1) |
| 71 | + return nums |
50 | 72 | ```
|
51 | 73 |
|
52 | 74 | ### **Java**
|
53 | 75 |
|
54 | 76 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
55 | 77 |
|
56 | 78 | ```java
|
| 79 | +class Solution { |
| 80 | + public int[] sortArray(int[] nums) { |
| 81 | + quickSort(nums, 0, nums.length - 1); |
| 82 | + return nums; |
| 83 | + } |
| 84 | + |
| 85 | + private void quickSort(int[] nums, int left, int right) { |
| 86 | + if (left >= right) { |
| 87 | + return; |
| 88 | + } |
| 89 | + int i = left - 1, j = right + 1; |
| 90 | + int x = nums[(left + right) >> 1]; |
| 91 | + while (i < j) { |
| 92 | + while (nums[++i] < x); |
| 93 | + while (nums[--j] > x); |
| 94 | + if (i < j) { |
| 95 | + int t = nums[i]; |
| 96 | + nums[i] = nums[j]; |
| 97 | + nums[j] = t; |
| 98 | + } |
| 99 | + } |
| 100 | + quickSort(nums, left, j); |
| 101 | + quickSort(nums, j + 1, right); |
| 102 | + } |
| 103 | +} |
| 104 | +``` |
| 105 | + |
| 106 | +### **C++** |
| 107 | + |
| 108 | +```cpp |
| 109 | +class Solution { |
| 110 | +public: |
| 111 | + vector<int> sortArray(vector<int>& nums) { |
| 112 | + quick_sort(nums, 0, nums.size() - 1); |
| 113 | + return nums; |
| 114 | + } |
| 115 | + |
| 116 | + void quick_sort(vector<int>& nums, int left, int right) { |
| 117 | + if (left >= right) return; |
| 118 | + int i = left - 1, j = right + 1; |
| 119 | + int x = nums[left + right >> 1]; |
| 120 | + while (i < j) |
| 121 | + { |
| 122 | + while (nums[++i] < x); |
| 123 | + while (nums[--j] > x); |
| 124 | + if (i < j) swap(nums[i], nums[j]); |
| 125 | + } |
| 126 | + quick_sort(nums, left, j); |
| 127 | + quick_sort(nums, j + 1, right); |
| 128 | + } |
| 129 | +}; |
| 130 | +``` |
57 | 131 |
|
| 132 | +### **Go** |
| 133 | +
|
| 134 | +```go |
| 135 | +func sortArray(nums []int) []int { |
| 136 | + quickSort(nums, 0, len(nums)-1) |
| 137 | + return nums |
| 138 | +} |
| 139 | +
|
| 140 | +func quickSort(nums []int, left, right int) { |
| 141 | + if left >= right { |
| 142 | + return |
| 143 | + } |
| 144 | + i, j := left-1, right+1 |
| 145 | + x := nums[(left+right)>>1] |
| 146 | + for i < j { |
| 147 | + for { |
| 148 | + i++ |
| 149 | + if nums[i] >= x { |
| 150 | + break |
| 151 | + } |
| 152 | + } |
| 153 | + for { |
| 154 | + j-- |
| 155 | + if nums[j] <= x { |
| 156 | + break |
| 157 | + } |
| 158 | + } |
| 159 | + if i < j { |
| 160 | + nums[i], nums[j] = nums[j], nums[i] |
| 161 | + } |
| 162 | + } |
| 163 | + quickSort(nums, left, j) |
| 164 | + quickSort(nums, j+1, right) |
| 165 | +} |
58 | 166 | ```
|
59 | 167 |
|
60 | 168 | ### **...**
|
|
0 commit comments