all tasks
This commit is contained in:
parent
b83f433714
commit
68f8f3e56b
14735 changed files with 178959 additions and 0 deletions
|
|
@ -0,0 +1,28 @@
|
|||
public static <E extends Comparable<? super E>> List<E> quickSort(List<E> arr) {
|
||||
if (arr.size() <= 1)
|
||||
return arr;
|
||||
E pivot = arr.getFirst(); //This pivot can change to get faster results
|
||||
|
||||
List<E> less = new LinkedList<E>();
|
||||
List<E> pivotList = new LinkedList<E>();
|
||||
List<E> more = new LinkedList<E>();
|
||||
|
||||
// Partition
|
||||
for (E i: arr) {
|
||||
if (i.compareTo(pivot) < 0)
|
||||
less.add(i);
|
||||
else if (i.compareTo(pivot) > 0)
|
||||
more.add(i);
|
||||
else
|
||||
pivotList.add(i);
|
||||
}
|
||||
|
||||
// Recursively sort sublists
|
||||
less = quickSort(less);
|
||||
more = quickSort(more);
|
||||
|
||||
// Concatenate results
|
||||
less.addAll(pivotList);
|
||||
less.addAll(more);
|
||||
return less;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue