Adds quick select and quick top algos
parent
66973cf46d
commit
70e2911852
|
@ -4,6 +4,30 @@ public interface Sorter<T>
|
||||||
{
|
{
|
||||||
T[] sort(T[] input, Comparator<T> comparator);
|
T[] sort(T[] input, Comparator<T> comparator);
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@FunctionalInterface
|
@FunctionalInterface
|
||||||
interface Comparator<T> {
|
interface Comparator<T> {
|
||||||
int compare(T o1, T o2);
|
int compare(T o1, T o2);
|
||||||
|
|
|
@ -6,7 +6,6 @@ import net.abhinavsarkar.algorist.Sorter;
|
||||||
|
|
||||||
public class QuickSorter<T> implements Sorter<T>
|
public class QuickSorter<T> implements Sorter<T>
|
||||||
{
|
{
|
||||||
private static final ThreadLocalRandom RANDOM = ThreadLocalRandom.current();
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public T[] sort(T[] input, Comparator<T> comparator)
|
public T[] sort(T[] input, Comparator<T> comparator)
|
||||||
|
@ -19,14 +18,6 @@ public class QuickSorter<T> implements Sorter<T>
|
||||||
return sort(input, 0, input.length, comparator);
|
return sort(input, 0, input.length, comparator);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void shuffle(T[] input)
|
|
||||||
{
|
|
||||||
for (int i = input.length - 1; i > 0; i--) {
|
|
||||||
int j = RANDOM.nextInt(i + 1);
|
|
||||||
swap(input, i, j);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private T[] sort(T[] input, int start, int end, Comparator<T> comparator)
|
private T[] sort(T[] input, int start, int end, Comparator<T> comparator)
|
||||||
{
|
{
|
||||||
if (end - start <= 1) {
|
if (end - start <= 1) {
|
||||||
|
@ -39,6 +30,76 @@ public class QuickSorter<T> implements Sorter<T>
|
||||||
return input;
|
return input;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public T[] top(T[] input, int rank, Comparator<T> comparator)
|
||||||
|
{
|
||||||
|
checkRank(input, rank);
|
||||||
|
|
||||||
|
shuffle(input);
|
||||||
|
return top(input, rank - 1, 0, input.length, comparator);
|
||||||
|
}
|
||||||
|
|
||||||
|
private T[] top(T[] input, int rank, int start, int end, Comparator<T> comparator)
|
||||||
|
{
|
||||||
|
if (end - start <= 1) {
|
||||||
|
return input;
|
||||||
|
}
|
||||||
|
|
||||||
|
int pivot = partition(input, start, end, comparator);
|
||||||
|
sort(input, start, pivot, comparator);
|
||||||
|
if (pivot <= rank)
|
||||||
|
{
|
||||||
|
top(input, rank, pivot, end, comparator);
|
||||||
|
}
|
||||||
|
return input;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public T select(T[] input, int rank, Comparator<T> comparator)
|
||||||
|
{
|
||||||
|
checkRank(input, rank);
|
||||||
|
|
||||||
|
shuffle(input);
|
||||||
|
return select(input, rank - 1, 0, input.length, comparator);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void checkRank(T[] input, int rank)
|
||||||
|
{
|
||||||
|
if (rank < 1) {
|
||||||
|
throw new IllegalArgumentException("rank is less than one");
|
||||||
|
}
|
||||||
|
if (rank > input.length) {
|
||||||
|
throw new IllegalArgumentException("rank is larger than input length");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private T select(T[] input, int rank, int start, int end, Comparator<T> comparator)
|
||||||
|
{
|
||||||
|
if (end - start == 1) {
|
||||||
|
if (rank == start) {
|
||||||
|
return input[start];
|
||||||
|
}
|
||||||
|
throw new IllegalStateException("Impossible");
|
||||||
|
}
|
||||||
|
|
||||||
|
int pivot = partition(input, start, end, comparator);
|
||||||
|
if (pivot == rank) {
|
||||||
|
return input[pivot];
|
||||||
|
} else if (pivot > rank) {
|
||||||
|
return select(input, rank, start, pivot, comparator);
|
||||||
|
} else {
|
||||||
|
return select(input, rank, pivot, end, comparator);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void shuffle(T[] input)
|
||||||
|
{
|
||||||
|
for (int i = input.length - 1; i > 0; i--) {
|
||||||
|
int j = ThreadLocalRandom.current().nextInt(i + 1);
|
||||||
|
swap(input, i, j);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private int partition(T[] input, int start, int end, Comparator<T> comparator)
|
private int partition(T[] input, int start, int end, Comparator<T> comparator)
|
||||||
{
|
{
|
||||||
int pivot = end - 1;
|
int pivot = end - 1;
|
||||||
|
@ -68,12 +129,13 @@ public class QuickSorter<T> implements Sorter<T>
|
||||||
public static void main(String[] args)
|
public static void main(String[] args)
|
||||||
{
|
{
|
||||||
Sorter<String> sorter = new QuickSorter<>();
|
Sorter<String> sorter = new QuickSorter<>();
|
||||||
String[] sorted =
|
String[] input = {"abhinav", "sarkar", "barista", "jordan", "data", "cata",
|
||||||
sorter.sort(new String[]{
|
"meta", "dota", "best", "recursion"};
|
||||||
"abhinav", "sarkar", "barista", "jordan", "data",
|
Comparator<String> comparator = (s1, s2) -> s2.compareTo(s1);
|
||||||
"cata", "meta", "dota", "best", "recursion"},
|
|
||||||
(s, anotherString) -> anotherString.compareTo(s));
|
System.out.println(Arrays.toString(sorter.sort(input, comparator)));
|
||||||
System.out.println(Arrays.toString(sorted));
|
System.out.println(sorter.select(input, 1, comparator));
|
||||||
|
System.out.println(Arrays.toString(sorter.top(input, 3, comparator)));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue