June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -0,0 +1,30 @@
|
|||
public static <E extends Comparable<? super E>> List<E> quickSort(List<E> arr) {
|
||||
if (arr.isEmpty())
|
||||
return arr;
|
||||
else {
|
||||
E pivot = arr.get(0);
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
public static <E extends Comparable<E>> List<E> sort(List<E> col) {
|
||||
if (col == null || col.isEmpty())
|
||||
return Collections.emptyList();
|
||||
else {
|
||||
E pivot = col.get(0);
|
||||
Map<Integer, List<E>> grouped = col.stream()
|
||||
.collect(Collectors.groupingBy(pivot::compareTo));
|
||||
return Stream.of(sort(grouped.get(1)), grouped.get(0), sort(grouped.get(-1)))
|
||||
.flatMap(Collection::stream).collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
|
|
@ -1,31 +0,0 @@
|
|||
public static <E extends Comparable<? super E>> List<E> quickSort(List<E> arr) {
|
||||
if (!arr.isEmpty()) {
|
||||
E pivot = arr.get(0); //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;
|
||||
}
|
||||
return arr;
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue