Java tutorial
//package com.java2s; import java.util.Comparator; import java.util.List; public class Main { private static <T extends Comparable<T>> void eqBrute(List<T> list, int lo, int hi) { if ((hi - lo) == 1) { if (list.get(hi).compareTo(list.get(lo)) < 0) { T e = list.get(lo); list.set(lo, list.get(hi)); list.set(hi, e); } } else if ((hi - lo) == 2) { int pmin = list.get(lo).compareTo(list.get(lo + 1)) < 0 ? lo : lo + 1; pmin = list.get(pmin).compareTo(list.get(lo + 2)) < 0 ? pmin : lo + 2; if (pmin != lo) { T e = list.get(lo); list.set(lo, list.get(pmin)); list.set(pmin, e); } eqBrute(list, lo + 1, hi); } else if ((hi - lo) == 3) { int pmin = list.get(lo).compareTo(list.get(lo + 1)) < 0 ? lo : lo + 1; pmin = list.get(pmin).compareTo(list.get(lo + 2)) < 0 ? pmin : lo + 2; pmin = list.get(pmin).compareTo(list.get(lo + 3)) < 0 ? pmin : lo + 3; if (pmin != lo) { T e = list.get(lo); list.set(lo, list.get(pmin)); list.set(pmin, e); } int pmax = list.get(hi).compareTo(list.get(hi - 1)) > 0 ? hi : hi - 1; pmax = list.get(pmax).compareTo(list.get(hi - 2)) > 0 ? pmax : hi - 2; if (pmax != hi) { T e = list.get(hi); list.set(hi, list.get(pmax)); list.set(pmax, e); } eqBrute(list, lo + 1, hi - 1); } } private static <T> void eqBrute(List<T> list, int lo, int hi, Comparator<? super T> c) { if ((hi - lo) == 1) { if (c.compare(list.get(hi), list.get(lo)) < 0) { T e = list.get(lo); list.set(lo, list.get(hi)); list.set(hi, e); } } else if ((hi - lo) == 2) { int pmin = c.compare(list.get(lo), list.get(lo + 1)) < 0 ? lo : lo + 1; pmin = c.compare(list.get(pmin), list.get(lo + 2)) < 0 ? pmin : lo + 2; if (pmin != lo) { T e = list.get(lo); list.set(lo, list.get(pmin)); list.set(pmin, e); } eqBrute(list, lo + 1, hi, c); } else if ((hi - lo) == 3) { int pmin = c.compare(list.get(lo), list.get(lo + 1)) < 0 ? lo : lo + 1; pmin = c.compare(list.get(pmin), list.get(lo + 2)) < 0 ? pmin : lo + 2; pmin = c.compare(list.get(pmin), list.get(lo + 3)) < 0 ? pmin : lo + 3; if (pmin != lo) { T e = list.get(lo); list.set(lo, list.get(pmin)); list.set(pmin, e); } int pmax = c.compare(list.get(hi), list.get(hi - 1)) > 0 ? hi : hi - 1; pmax = c.compare(list.get(pmax), list.get(hi - 2)) > 0 ? pmax : hi - 2; if (pmax != hi) { T e = list.get(hi); list.set(hi, list.get(pmax)); list.set(pmax, e); } eqBrute(list, lo + 1, hi - 1, c); } } }