I started to love quick sort. The reason being…it’s quick! Quick to code, quick to sort. Easy to understand. So.. here it is in 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
public List<Integer> qsort(List<Integer> list){
//base case
if (list.size() < 2){
return list;
}
//choose pivot
int pivot = list.get(0);
//find low, pivots and high elems
List<Integer> low = new ArrayList<Integer>();
List<Integer> high = new ArrayList<Integer>();
List<Integer> pivots = new ArrayList<Integer>();
for (int i : list){
if (i < pivot){
low.add(i);
}else if (i == pivot){
pivots.add(i);
}
else{
high.add(i);
}
}
//qsort on low and high recursively
low = qsort(low);
high = qsort(high);
//get here eventually after all recursive calls
low.addAll(pivots);
low.addAll(high);
return low;
}