Adds selection, insertion and merge sorters
This commit is contained in:
parent
b01ce9327e
commit
e76b24940e
11
src/main/java/net/abhinavsarkar/algorist/Sorter.java
Normal file
11
src/main/java/net/abhinavsarkar/algorist/Sorter.java
Normal file
@ -0,0 +1,11 @@
|
||||
package net.abhinavsarkar.algorist;
|
||||
|
||||
public interface Sorter<T>
|
||||
{
|
||||
T[] sort(T[] input, Comparator<T> comparator);
|
||||
|
||||
@FunctionalInterface
|
||||
interface Comparator<T> {
|
||||
int compare(T o1, T o2);
|
||||
}
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package net.abhinavsarkar.algorist.sort;
|
||||
|
||||
import java.util.Arrays;
|
||||
import net.abhinavsarkar.algorist.Sorter;
|
||||
|
||||
public class InsertionSorter<T> implements Sorter<T>
|
||||
{
|
||||
@Override
|
||||
public T[] sort(T[] input, Comparator<T> comparator)
|
||||
{
|
||||
for (int i = 1; i < input.length; i++) {
|
||||
for (int j = i; j > 0; j--) {
|
||||
if (comparator.compare(input[j], input[j-1]) < 0) {
|
||||
swap(input, j, j - 1);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return input;
|
||||
}
|
||||
|
||||
private void swap(T[] input, int i, int j)
|
||||
{
|
||||
T temp = input[i];
|
||||
input[i] = input[j];
|
||||
input[j] = temp;
|
||||
}
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
Sorter<String> sorter = new InsertionSorter<>();
|
||||
String[] sorted =
|
||||
sorter.sort(new String[]{"abhinav", "sarkar", "barista", "jordan", "data"},
|
||||
(s, anotherString) -> anotherString.compareTo(s));
|
||||
System.out.println(Arrays.toString(sorted));
|
||||
}
|
||||
}
|
@ -0,0 +1,66 @@
|
||||
package net.abhinavsarkar.algorist.sort;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.Arrays;
|
||||
import net.abhinavsarkar.algorist.Sorter;
|
||||
|
||||
public class MergeSorter<T> implements Sorter<T>
|
||||
{
|
||||
@Override
|
||||
public T[] sort(T[] input, Comparator<T> comparator)
|
||||
{
|
||||
if (input.length == 0) {
|
||||
return input;
|
||||
}
|
||||
return this.sort(input, 0, input.length, comparator);
|
||||
}
|
||||
|
||||
private T[] sort(T[] input, int start, int end, Comparator<T> comparator)
|
||||
{
|
||||
if (start == end - 1) {
|
||||
T[] output = mkArray(input, 1);
|
||||
output[0] = input[start];
|
||||
return output;
|
||||
}
|
||||
|
||||
int half = start + (end - start)/2;
|
||||
T[] left = this.sort(input, start, half, comparator);
|
||||
T[] right = this.sort(input, half, end, comparator);
|
||||
return this.merge(left, right, comparator);
|
||||
}
|
||||
|
||||
private T[] merge(T[] left, T[] right, Comparator<T> comparator)
|
||||
{
|
||||
T[] output = mkArray(left, left.length + right.length);
|
||||
int i, j, k;
|
||||
i = j = k = 0;
|
||||
while (i < left.length || j < right.length) {
|
||||
if (i >= left.length) {
|
||||
System.arraycopy(right, j, output, j + left.length, right.length - j);
|
||||
break;
|
||||
}
|
||||
if (j >= right.length) {
|
||||
System.arraycopy(left, i, output, i + right.length, left.length - i);
|
||||
break;
|
||||
}
|
||||
output[k++] = comparator.compare(left[i], right[j]) <= 0 ? left[i++] : right[j++];
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private T[] mkArray(T[] input, int length)
|
||||
{
|
||||
return (T[]) Array.newInstance(input.getClass().getComponentType(), length);
|
||||
}
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
Sorter<String> sorter = new MergeSorter<>();
|
||||
String[] sorted =
|
||||
sorter.sort(new String[]{"abhinav", "sarkar", "barista", "jordan", "data"},
|
||||
(s, anotherString) -> anotherString.compareTo(s));
|
||||
System.out.println(Arrays.toString(sorted));
|
||||
}
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
package net.abhinavsarkar.algorist.sort;
|
||||
|
||||
import java.util.Arrays;
|
||||
import net.abhinavsarkar.algorist.Sorter;
|
||||
|
||||
public class SelectionSorter<T> implements Sorter<T>
|
||||
{
|
||||
@Override
|
||||
public T[] sort(T[] input, Comparator<T> comparator)
|
||||
{
|
||||
for (int i = 0; i < input.length - 1; i++) {
|
||||
int j = findMinimumIndex(input, i, comparator);
|
||||
swap(input, i, j);
|
||||
}
|
||||
return input;
|
||||
}
|
||||
|
||||
private int findMinimumIndex(T[] input, int idx, Comparator<T> comparator) {
|
||||
int minIdx = -1;
|
||||
T min = null;
|
||||
for (int j = idx; j < input.length; j++) {
|
||||
if (min == null || comparator.compare(input[j], min) < 0) {
|
||||
min = input[j];
|
||||
minIdx = j;
|
||||
}
|
||||
}
|
||||
return minIdx;
|
||||
}
|
||||
|
||||
private void swap(T[] input, int i, int j)
|
||||
{
|
||||
T temp = input[i];
|
||||
input[i] = input[j];
|
||||
input[j] = temp;
|
||||
}
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
Sorter<String> sorter = new SelectionSorter<>();
|
||||
String[] sorted =
|
||||
sorter.sort(new String[]{"abhinav", "sarkar", "barista", "jordan", "data"},
|
||||
(s, anotherString) -> anotherString.compareTo(s));
|
||||
System.out.println(Arrays.toString(sorted));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user