import java.util.*; // // Java implementation of quickselect algorithm. // See https://en.wikipedia.org/wiki/Quickselect // public class QuickSelect { private static final Random random = new Random(); public static T select(List list, int n, Comparator cmp) { return select(list, 0, list.size() - 1, n, cmp); } public static T select(List list, int left, int right, int n, Comparator cmp) { for (;;) { if (left == right) return list.get(left); int pivot = pivotIndex(left, right); pivot = partition(list, left, right, pivot, cmp); if (n == pivot) return list.get(n); else if (n < pivot) right = pivot - 1; else left = pivot + 1; } } private static int partition(List list, int left, int right, int pivot, Comparator cmp) { T pivotValue = list.get(pivot); swap(list, pivot, right); int store = left; for (int i = left; i < right; ++i) { if (cmp.compare(list.get(i), pivotValue) < 0) { swap(list, store, i); ++store; } } swap(list, right, store); return store; } private static void swap(List list, int i, int j) { T value = list.get(i); list.set(i, list.get(j)); list.set(j, value); } private static int pivotIndex(int left, int right) { return left + random.nextInt(right - left + 1); } }