-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreeWayQuickSort.java
More file actions
42 lines (35 loc) · 1.01 KB
/
ThreeWayQuickSort.java
File metadata and controls
42 lines (35 loc) · 1.01 KB
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
37
38
39
40
41
42
public class ThreeWayQuickSort {
public static void sort(int[] arr, int left, int right) {
if (right <= left)
return;
int i = left;
int lt = left;
int gt = right;
int pivot = arr[left];
while (i <= gt) {
if (arr[i] < pivot) {
int temp = arr[lt];
arr[lt] = arr[i];
arr[i] = temp;
i++;
lt++;
} else if (arr[i] > pivot) {
int temp = arr[i];
arr[i] = arr[gt];
arr[gt] = temp;
gt--;
} else
i++;
}
sort(arr, left, lt-1);
sort(arr, gt+1, right);
}
public static void main(String[] args) {
int[] iarr = Utils.generateArray(100, 1000);
System.out.println("Initial array:");
Utils.printArray(iarr);
sort(iarr, 0, iarr.length-1);
System.out.println("Sorted array:");
Utils.printArray(iarr);
}
}