-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuickSort.java
42 lines (33 loc) · 1.16 KB
/
QuickSort.java
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
import java.util.Arrays;
public class QuickSort {
private static Comparable[] array = {5,8,4,7,1,5,9,3,6};
public static void main(String[] args) {
QuickSort qsort = new QuickSort();
qsort.sort(array, 0, array.length-1);
System.out.println(Arrays.toString(array));
}
public void sort(Comparable[] arr, int low, int high) {
if (low < high) {
int i = low, j = high;
Comparable x = arr[(i + j) / 2];
do {
while (arr[i].compareTo(x) < 0) i++;
while (x.compareTo(arr[j]) < 0) j--;
if ( i <= j) {
Comparable tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
i++;
j--;
}
} while (i <= j);
//! Remove comments if you want to understand the code better, or watch the video in the Readme
//for(int k =0; k<array.length;k++){
// System.out.print(array[k] + ", ");
//}
//System.out.println();
sort(arr, low, j);
sort(arr, i, high);
}
}
}