55** 快速排序算法模板:**
66
77``` 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 ) {
1010 return ;
1111 }
12- int i = left - 1 , j = high + 1 ;
12+ int i = left - 1 , j = right + 1 ;
1313 int x = nums[left];
1414 while (i < j) {
1515 while (nums[++ i] < x);
@@ -21,7 +21,7 @@ void quickSort(int[] nums, int left, int high) {
2121 }
2222 }
2323 quickSort(nums, left, j);
24- quickSort(nums, j + 1 , high );
24+ quickSort(nums, j + 1 , right );
2525}
2626```
2727
@@ -72,11 +72,11 @@ N = int(input())
7272nums = list (map (int , input ().split()))
7373
7474
75- def quick_sort (nums , left , high ):
76- if left >= high :
75+ def quick_sort (nums , left , right ):
76+ if left >= right :
7777 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 ]
8080 while i < j:
8181 while 1 :
8282 i += 1
@@ -89,7 +89,7 @@ def quick_sort(nums, left, high):
8989 if i < j:
9090 nums[i], nums[j] = nums[j], nums[i]
9191 quick_sort(nums, left, j)
92- quick_sort(nums, j + 1 , high )
92+ quick_sort(nums, j + 1 , right )
9393
9494
9595quick_sort(nums, 0 , N - 1 )
@@ -115,11 +115,11 @@ public class Main {
115115 }
116116 }
117117
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 ) {
120120 return ;
121121 }
122- int i = left - 1 , j = high + 1 ;
122+ int i = left - 1 , j = right + 1 ;
123123 int x = nums[left];
124124 while (i < j) {
125125 while (nums[++ i] < x);
@@ -131,7 +131,7 @@ public class Main {
131131 }
132132 }
133133 quickSort(nums, left, j);
134- quickSort(nums, j + 1 , high );
134+ quickSort(nums, j + 1 , right );
135135 }
136136}
137137```
@@ -150,14 +150,14 @@ let getInputArgs = line => {
150150 return line .split (' ' ).filter (s => s !== ' ' ).map (x => parseInt (x));
151151}
152152
153- function quickSort (nums , left , high ) {
154- if (left >= high ) {
153+ function quickSort (nums , left , right ) {
154+ if (left >= right ) {
155155 return ;
156156 }
157157
158158 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 ];
161161 while (i < j) {
162162 while (nums[++ i] < x);
163163 while (nums[-- j] > x);
@@ -168,7 +168,7 @@ function quickSort(nums, left, high) {
168168 }
169169 }
170170 quickSort (nums, left, j);
171- quickSort (nums, j + 1 , high );
171+ quickSort (nums, j + 1 , right );
172172}
173173
174174
@@ -192,12 +192,12 @@ package main
192192
193193import " fmt"
194194
195- func quickSort (nums []int , left , high int ) {
196- if left >= high {
195+ func quickSort (nums []int , left , right int ) {
196+ if left >= right {
197197 return
198198 }
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 ]
201201 for i < j {
202202 for {
203203 i++
@@ -216,7 +216,7 @@ func quickSort(nums []int, left, high int) {
216216 }
217217 }
218218 quickSort (nums, left, j)
219- quickSort (nums, j+1 , high )
219+ quickSort (nums, j+1 , right )
220220}
221221
222222func main () {
0 commit comments