2019-07-07 17:54:43 +05:30
|
|
|
package net.abhinavsarkar.algorist;
|
|
|
|
|
|
|
|
public interface Sorter<T>
|
|
|
|
{
|
|
|
|
T[] sort(T[] input, Comparator<T> comparator);
|
|
|
|
|
2019-07-08 20:55:20 +05:30
|
|
|
default T select(T[] input, int rank, Comparator<T> comparator) {
|
|
|
|
if (rank < 1) {
|
|
|
|
throw new IllegalArgumentException("rank is less than one");
|
|
|
|
}
|
|
|
|
if (rank > input.length) {
|
|
|
|
throw new IllegalArgumentException("rank is larger than input length");
|
|
|
|
}
|
|
|
|
|
|
|
|
T[] sorted = sort(input, comparator);
|
|
|
|
return sorted[rank - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
default T[] top(T[] input, int rank, Comparator<T> comparator) {
|
|
|
|
if (rank < 1) {
|
|
|
|
throw new IllegalArgumentException("rank is less than one");
|
|
|
|
}
|
|
|
|
if (rank > input.length) {
|
|
|
|
throw new IllegalArgumentException("rank is larger than input length");
|
|
|
|
}
|
|
|
|
|
|
|
|
return sort(input, comparator);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-07 17:54:43 +05:30
|
|
|
@FunctionalInterface
|
|
|
|
interface Comparator<T> {
|
|
|
|
int compare(T o1, T o2);
|
|
|
|
}
|
|
|
|
}
|