function quicksort(q) var list less, pivotList, greater if length(q) ≤ 1 { return q } else { select a pivot value pivot from q for each x in q except the pivot element if x < pivot then add x to less if x ≥ pivot then add x to greater add pivot to pivotList return concatenate(quicksort(less), pivotList, quicksort(greater)) }
/** * 快速排序,递归版 */ publicstatic <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); } elseif (i.compareTo(pivot) > 0) { more.add(i); } else { pivotList.add(i); } }
// Recursively sort sublists less = quickSort(less); more = quickSort(more);