5
5
** 快速排序算法模板:**
6
6
7
7
``` java
8
- void quickSort(int [] nums, int left, int high ) {
9
- if (left >= high ) {
8
+ void quickSort(int [] nums, int left, int right ) {
9
+ if (left >= right ) {
10
10
return ;
11
11
}
12
- int i = left - 1 , j = high + 1 ;
12
+ int i = left - 1 , j = right + 1 ;
13
13
int x = nums[left];
14
14
while (i < j) {
15
15
while (nums[++ i] < x);
@@ -21,7 +21,7 @@ void quickSort(int[] nums, int left, int high) {
21
21
}
22
22
}
23
23
quickSort(nums, left, j);
24
- quickSort(nums, j + 1 , high );
24
+ quickSort(nums, j + 1 , right );
25
25
}
26
26
```
27
27
@@ -72,11 +72,11 @@ N = int(input())
72
72
nums = list (map (int , input ().split()))
73
73
74
74
75
- def quick_sort (nums , left , high ):
76
- if left >= high :
75
+ def quick_sort (nums , left , right ):
76
+ if left >= right :
77
77
return
78
- i, j = left - 1 , high + 1
79
- x = nums[(left + high ) >> 1 ]
78
+ i, j = left - 1 , right + 1
79
+ x = nums[(left + right ) >> 1 ]
80
80
while i < j:
81
81
while 1 :
82
82
i += 1
@@ -89,7 +89,7 @@ def quick_sort(nums, left, high):
89
89
if i < j:
90
90
nums[i], nums[j] = nums[j], nums[i]
91
91
quick_sort(nums, left, j)
92
- quick_sort(nums, j + 1 , high )
92
+ quick_sort(nums, j + 1 , right )
93
93
94
94
95
95
quick_sort(nums, 0 , N - 1 )
@@ -115,11 +115,11 @@ public class Main {
115
115
}
116
116
}
117
117
118
- public static void quickSort (int [] nums , int left , int high ) {
119
- if (left >= high ) {
118
+ public static void quickSort (int [] nums , int left , int right ) {
119
+ if (left >= right ) {
120
120
return ;
121
121
}
122
- int i = left - 1 , j = high + 1 ;
122
+ int i = left - 1 , j = right + 1 ;
123
123
int x = nums[left];
124
124
while (i < j) {
125
125
while (nums[++ i] < x);
@@ -131,7 +131,7 @@ public class Main {
131
131
}
132
132
}
133
133
quickSort(nums, left, j);
134
- quickSort(nums, j + 1 , high );
134
+ quickSort(nums, j + 1 , right );
135
135
}
136
136
}
137
137
```
@@ -150,14 +150,14 @@ let getInputArgs = line => {
150
150
return line .split (' ' ).filter (s => s !== ' ' ).map (x => parseInt (x));
151
151
}
152
152
153
- function quickSort (nums , left , high ) {
154
- if (left >= high ) {
153
+ function quickSort (nums , left , right ) {
154
+ if (left >= right ) {
155
155
return ;
156
156
}
157
157
158
158
let i = left - 1 ;
159
- let j = high + 1 ;
160
- let x = nums[(left + high ) >> 1 ];
159
+ let j = right + 1 ;
160
+ let x = nums[(left + right ) >> 1 ];
161
161
while (i < j) {
162
162
while (nums[++ i] < x);
163
163
while (nums[-- j] > x);
@@ -168,7 +168,7 @@ function quickSort(nums, left, high) {
168
168
}
169
169
}
170
170
quickSort (nums, left, j);
171
- quickSort (nums, j + 1 , high );
171
+ quickSort (nums, j + 1 , right );
172
172
}
173
173
174
174
@@ -192,12 +192,12 @@ package main
192
192
193
193
import " fmt"
194
194
195
- func quickSort (nums []int , left , high int ) {
196
- if left >= high {
195
+ func quickSort (nums []int , left , right int ) {
196
+ if left >= right {
197
197
return
198
198
}
199
- i , j := left-1 , high +1
200
- x := nums[(left+high )>>1 ]
199
+ i , j := left-1 , right +1
200
+ x := nums[(left+right )>>1 ]
201
201
for i < j {
202
202
for {
203
203
i++
@@ -216,7 +216,7 @@ func quickSort(nums []int, left, high int) {
216
216
}
217
217
}
218
218
quickSort (nums, left, j)
219
- quickSort (nums, j+1 , high )
219
+ quickSort (nums, j+1 , right )
220
220
}
221
221
222
222
func main () {
0 commit comments