-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.java
More file actions
42 lines (36 loc) · 1.07 KB
/
QuickSort.java
File metadata and controls
42 lines (36 loc) · 1.07 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
package SortAlgorithms;
import java.util.Arrays;
public class QuickSort {
public static void main(String[] args) {
int[] intArray = {35, 45, -50, 12, 18, 5, 6, -10};
quickSort(intArray, 0, intArray.length);
System.out.println(Arrays.toString(intArray));
}
public static void quickSort(int[] input, int start, int end) {
if (end - start < 2) {
return;
}
int pivotIndex = partition(input, start, end);
//Left
quickSort(input, start, pivotIndex);
//Right
quickSort(input, pivotIndex + 1, end);
}
private static int partition(int[] input, int start, int end) {
int pivot = input[start];
int i = start;
int j = end;
while (i < j) {
while (i < j && input[--j] >= pivot) ;
if (i < j) {
input[i] = input[j];
}
while (i < j && input[++i] <= pivot) ;
if (i < j) {
input[j] = input[i];
}
}
input[j] = pivot;
return j;
}
}