1
1
package com .examplehub .sorts ;
2
2
3
- import com .examplehub .utils .SortUtils ;
4
-
5
3
public class QuickSort implements Sort {
6
4
7
5
@ Override
@@ -11,18 +9,15 @@ public void sort(int[] numbers) {
11
9
12
10
private int partition (int [] number , int left , int right ) {
13
11
int pivot = number [left ];
14
- while (left < right ) {
15
- while (left < right && number [right ] >= pivot ) {
12
+ while (left != right ) {
13
+ while (left != right && number [right ] >= pivot ) {
16
14
right --;
17
15
}
18
- while (left < right && number [left ] <= pivot ) {
19
- left ++;
20
- }
21
- if (left < right ) {
22
- SortUtils .swap (number , left , right );
16
+ number [left ] = number [right ];
17
+ while (left != right && number [left ] <= pivot ) {
23
18
left ++;
24
- right --;
25
19
}
20
+ number [right ] = number [left ];
26
21
}
27
22
number [left ] = pivot ;
28
23
return left ;
@@ -54,18 +49,15 @@ public <T extends Comparable<T>> void sort(T[] array) {
54
49
55
50
private static <T extends Comparable <T >> int partition (T [] array , int left , int right ) {
56
51
T pivot = array [left ];
57
- while (left < right ) {
58
- while (left < right && array [right ].compareTo (pivot ) >= 0 ) {
52
+ while (left != right ) {
53
+ while (left != right && array [right ].compareTo (pivot ) >= 0 ) {
59
54
right --;
60
55
}
61
- while (left < right && array [left ].compareTo (pivot ) <= 0 ) {
56
+ array [left ] = array [right ];
57
+ while (left != right && array [left ].compareTo (pivot ) <= 0 ) {
62
58
left ++;
63
59
}
64
- if (left < right ) {
65
- SortUtils .swap (array , left , right );
66
- left ++;
67
- right --;
68
- }
60
+ array [right ] = array [left ];
69
61
}
70
62
array [left ] = pivot ;
71
63
return left ;
0 commit comments