Adds quicksorter
This commit is contained in:
parent
e76b24940e
commit
690a506a56
@ -9,6 +9,12 @@ public class Algorist
|
|||||||
// Hash Table - open addressing and chaining
|
// Hash Table - open addressing and chaining
|
||||||
// Linked BST
|
// Linked BST
|
||||||
// Ranked BST
|
// Ranked BST
|
||||||
|
// Heapsort
|
||||||
|
// selection sort
|
||||||
|
// insertion sort
|
||||||
|
// merge sort
|
||||||
|
// quick sort
|
||||||
|
// binary search
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -22,6 +22,10 @@ public class InsertionSorter<T> implements Sorter<T>
|
|||||||
|
|
||||||
private void swap(T[] input, int i, int j)
|
private void swap(T[] input, int i, int j)
|
||||||
{
|
{
|
||||||
|
if (i == j) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
T temp = input[i];
|
T temp = input[i];
|
||||||
input[i] = input[j];
|
input[i] = input[j];
|
||||||
input[j] = temp;
|
input[j] = temp;
|
||||||
|
@ -0,0 +1,79 @@
|
|||||||
|
package net.abhinavsarkar.algorist.sort;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.concurrent.ThreadLocalRandom;
|
||||||
|
import net.abhinavsarkar.algorist.Sorter;
|
||||||
|
|
||||||
|
public class QuickSorter<T> implements Sorter<T>
|
||||||
|
{
|
||||||
|
private static final ThreadLocalRandom RANDOM = ThreadLocalRandom.current();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public T[] sort(T[] input, Comparator<T> comparator)
|
||||||
|
{
|
||||||
|
if (input.length <= 1) {
|
||||||
|
return input;
|
||||||
|
}
|
||||||
|
|
||||||
|
shuffle(input);
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
if (end - start <= 1) {
|
||||||
|
return input;
|
||||||
|
}
|
||||||
|
|
||||||
|
int pivot = partition(input, start, end, comparator);
|
||||||
|
sort(input, start, pivot, comparator);
|
||||||
|
sort(input, pivot, end, comparator);
|
||||||
|
return input;
|
||||||
|
}
|
||||||
|
|
||||||
|
private int partition(T[] input, int start, int end, Comparator<T> comparator)
|
||||||
|
{
|
||||||
|
int pivot = end - 1;
|
||||||
|
int firstHigh = start;
|
||||||
|
for (int i = start; i < pivot; i++)
|
||||||
|
{
|
||||||
|
if (comparator.compare(input[i], input[pivot]) < 0) {
|
||||||
|
swap(input, i, firstHigh);
|
||||||
|
firstHigh++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
swap(input, pivot, firstHigh);
|
||||||
|
|
||||||
|
return firstHigh;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void swap(T[] input, int i, int j)
|
||||||
|
{
|
||||||
|
if (i == j) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
T temp = input[i];
|
||||||
|
input[i] = input[j];
|
||||||
|
input[j] = temp;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args)
|
||||||
|
{
|
||||||
|
Sorter<String> sorter = new QuickSorter<>();
|
||||||
|
String[] sorted =
|
||||||
|
sorter.sort(new String[]{
|
||||||
|
"abhinav", "sarkar", "barista", "jordan", "data",
|
||||||
|
"cata", "meta", "dota", "best", "recursion"},
|
||||||
|
(s, anotherString) -> anotherString.compareTo(s));
|
||||||
|
System.out.println(Arrays.toString(sorted));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -29,6 +29,10 @@ public class SelectionSorter<T> implements Sorter<T>
|
|||||||
|
|
||||||
private void swap(T[] input, int i, int j)
|
private void swap(T[] input, int i, int j)
|
||||||
{
|
{
|
||||||
|
if (i == j) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
T temp = input[i];
|
T temp = input[i];
|
||||||
input[i] = input[j];
|
input[i] = input[j];
|
||||||
input[j] = temp;
|
input[j] = temp;
|
||||||
|
Loading…
Reference in New Issue
Block a user