Java tutorial
//package com.java2s; import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.Set; public class Main { public static <T> T qnth(List<T> sample, int n, Comparator<T> comp) { T pivot = sample.get(0); List<T> below = new ArrayList<T>(), above = new ArrayList<T>(); for (T s : sample) { int flag = comp.compare(s, pivot); if (flag < 0) { above.add(s); } else if (flag > 0) { below.add(s); } } int i = below.size(), j = sample.size() - above.size(); if (n < i) return qnth(below, n, comp); else if (n >= j) return qnth(above, n - j, comp); else return pivot; } public static <T> T get(Set<T> set, T obj) { for (T o : set) { if (o.equals(obj)) { return o; } } return null; } }