-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuickSort.java
43 lines (33 loc) · 1.18 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
43
import java.util.*;
public class QuickSort {
private static int inPlaceSortWithPartition(List<Integer> list, int start, int end)
{
int size = end - start + 1;
Random random = new Random();
int randomIndex = random.nextInt(size); // returns any number between 0 and size - 1
int pivotLocation = start + randomIndex;
int pivotElement = list.get(pivotLocation);
Collections.swap(list, pivotLocation, end);
int k = start;
for (int i = start; i<=end; i++)
{
if(list.get(i) < pivotElement)
Collections.swap(list, k++, i);
}
Collections.swap(list, k, end);
return k;
}
private static void quickSort(List<Integer> list, int start, int end)
{
if(start >= end)
return;
int pivotLocation = inPlaceSortWithPartition(list, start, end);
quickSort(list, start, pivotLocation - 1);
quickSort(list, pivotLocation + 1, end);
}
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1, 0, 4);
QuickSort.quickSort(list, 0, list.size() - 1);
System.out.println(list);
}
}