forked from Hithomelabs/Princeton1
Compare commits
6 Commits
6a752c9dc2
...
dd1ec6e2cd
Author | SHA1 | Date | |
---|---|---|---|
dd1ec6e2cd | |||
6a43e7d183 | |||
bbc040dcd4 | |||
265aa3ca85 | |||
c9432c8c2b | |||
d84cc16d24 |
@ -16,6 +16,7 @@ dependencies {
|
||||
implementation project(':module5')
|
||||
implementation project(':module6')
|
||||
implementation project(':module7')
|
||||
implementation project(':module8')
|
||||
implementation project(':common')
|
||||
testImplementation project(':common')
|
||||
}
|
||||
|
@ -0,0 +1,57 @@
|
||||
package com.hithomelabs.clients.Benchmarks;
|
||||
|
||||
import com.hithomelabs.princeton1.common.Apple;
|
||||
import com.hithomelabs.princeton1.module5.MeasurableSort;
|
||||
import com.hithomelabs.princeton1.module5.SortingMetaData;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
public class BenchMark {
|
||||
|
||||
private int size;
|
||||
private List<MeasurableSort<Apple>> algorithms;
|
||||
private Apple[] sorted;
|
||||
private Apple[] reverseSorted;
|
||||
private Apple[] random;
|
||||
|
||||
BenchMark(int size, List<MeasurableSort<Apple>> algorithms) {
|
||||
this.size = size;
|
||||
this.algorithms = algorithms;
|
||||
sorted = new Apple[this.size];
|
||||
reverseSorted = new Apple[this.size];
|
||||
random = new Apple[this.size];
|
||||
|
||||
for (int i = 0; i < size; i++) {
|
||||
sorted[i] = new Apple(i);
|
||||
reverseSorted[i] = new Apple(size - i);
|
||||
random[i] = new Apple(new Random().nextInt(1000));
|
||||
}
|
||||
}
|
||||
|
||||
public void run() {
|
||||
for (MeasurableSort<Apple> algo: algorithms){
|
||||
System.out.println("Input size: "+size);
|
||||
System.out.println("Running "+algo.getClass());
|
||||
System.out.println("Sorting already sorted list");
|
||||
SortingMetaData metaData = new SortingMetaData();
|
||||
algo.sort(sorted.clone(),Apple.COMPARE_BY_SIZE, metaData);
|
||||
System.out.println(metaData);
|
||||
System.out.println("Sorting reverse sorted list");
|
||||
metaData = new SortingMetaData();
|
||||
algo.sort(reverseSorted.clone(),Apple.COMPARE_BY_SIZE, metaData);
|
||||
System.out.println(metaData);
|
||||
System.out.println("Sorting randomly ordered list");
|
||||
metaData = new SortingMetaData();
|
||||
algo.sort(random.clone(),Apple.COMPARE_BY_SIZE, metaData);
|
||||
System.out.println(metaData);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -0,0 +1,37 @@
|
||||
package com.hithomelabs.clients.Benchmarks;
|
||||
|
||||
import com.hithomelabs.princeton1.common.Apple;
|
||||
import com.hithomelabs.princeton1.module5.Insertion;
|
||||
import com.hithomelabs.princeton1.module5.MeasurableSort;
|
||||
import com.hithomelabs.princeton1.module5.Selection;
|
||||
import com.hithomelabs.princeton1.module5.Shell;
|
||||
import com.hithomelabs.princeton1.module6.Merge;
|
||||
import com.hithomelabs.princeton1.module7.Quick;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class BenchMarkingClient {
|
||||
|
||||
public static void main(String[] args) {
|
||||
List<MeasurableSort<Apple>> algorithms = new ArrayList<MeasurableSort<Apple>>();
|
||||
|
||||
algorithms.add(new Selection<Apple>());
|
||||
algorithms.add(new Insertion<Apple>());
|
||||
algorithms.add(new Shell<Apple>());
|
||||
algorithms.add(new Merge<Apple>());
|
||||
algorithms.add(new Quick<Apple>());
|
||||
|
||||
BenchMark b1 = new BenchMark(32, algorithms);
|
||||
BenchMark b2 = new BenchMark(512, algorithms);
|
||||
BenchMark b3 = new BenchMark(1024, algorithms);
|
||||
BenchMark b4 = new BenchMark(4096,algorithms);
|
||||
b1.run();
|
||||
b2.run();
|
||||
b3.run();
|
||||
b4.run();
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package com.hithomelabs.clients.module7;
|
||||
|
||||
import com.hithomelabs.princeton1.common.Apple;
|
||||
import com.hithomelabs.princeton1.module7.Quick;
|
||||
import com.hithomelabs.princeton1.module7.ThreeWayQuick;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class ThreeWayQuickSortClient {
|
||||
|
||||
public static void main(String[] args) {
|
||||
int size = 100;
|
||||
Apple[] apples = new Apple[size];
|
||||
ThreeWayQuick<Apple> quick = new ThreeWayQuick<>();
|
||||
|
||||
for (int i = 0; i < apples.length; i++) {
|
||||
apples[i] = new Apple(new Random().nextInt(10));
|
||||
}
|
||||
quick.sort(apples);
|
||||
for (int i = 0; i < apples.length; i++)
|
||||
System.out.println(apples[i]);
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package com.hithomelabs.clients.module8;
|
||||
|
||||
import com.hithomelabs.princeton1.common.Apple;
|
||||
import com.hithomelabs.princeton1.module8.HeapSort;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class HeapSortClient {
|
||||
|
||||
public static void main(String[] args) {
|
||||
int size = 10;
|
||||
Apple[] apples = new Apple[size];
|
||||
HeapSort<Apple> heap = new HeapSort<Apple>();
|
||||
|
||||
for (int i = 0; i < apples.length; i++) {
|
||||
apples[i] = new Apple(new Random().nextInt(10));
|
||||
}
|
||||
heap.sort(apples);
|
||||
for (int i = 0; i < apples.length; i++)
|
||||
System.out.println(apples[i]);
|
||||
}
|
||||
|
||||
}
|
@ -1,9 +1,18 @@
|
||||
package com.hithomelabs.princeton1.common;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.Objects;
|
||||
|
||||
public class Apple implements Comparable<Apple> {
|
||||
private int size;
|
||||
public static Comparator<Apple> COMPARE_BY_SIZE = new Comparator<Apple>() {
|
||||
@Override
|
||||
public int compare(Apple a1, Apple a2) {
|
||||
if(a1.size < a2.size) return -1;
|
||||
else if (a1.size == (a2.size)) return 0;
|
||||
else return 1;
|
||||
}
|
||||
};
|
||||
|
||||
public Apple(int size) {
|
||||
this.size = size;
|
||||
|
@ -1,17 +0,0 @@
|
||||
package com.hithomelabs.princeton1.module5;
|
||||
|
||||
public abstract class AbstractCustomSorts<E> {
|
||||
|
||||
public abstract void sort(E[] arr);
|
||||
|
||||
public void exch(E[] arr, int j, int i) {
|
||||
E temp = arr[i];
|
||||
arr[i] = arr[j];
|
||||
arr[j] = temp;
|
||||
}
|
||||
|
||||
public boolean less(Comparable<E> e1, E e2) {
|
||||
return e1.compareTo(e2) < 0;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package com.hithomelabs.princeton1.module5;
|
||||
|
||||
// * * Provides a set of helper functions less and exch for Comparable data types
|
||||
public interface ComparableHelper {
|
||||
|
||||
static <T> void exch(T[] arr, int j, int i) {
|
||||
T temp = arr[i];
|
||||
arr[i] = arr[j];
|
||||
arr[j] = temp;
|
||||
}
|
||||
|
||||
static <T> boolean less(T e1, T e2) {
|
||||
return ((Comparable<T>)e1).compareTo(e2) < 0;
|
||||
}
|
||||
|
||||
static <T> boolean equals(T e1, T e2) { return ((Comparable<T>)e1).compareTo(e2) == 0;}
|
||||
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package com.hithomelabs.princeton1.module5;
|
||||
|
||||
public interface ComparableSort<E> {
|
||||
|
||||
void sort(E[] arr);
|
||||
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package com.hithomelabs.princeton1.module5;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
public interface ComparatorHelper extends ComparableHelper {
|
||||
|
||||
static <T> boolean less(T v, T w, Comparator<T> cmp) {
|
||||
if (cmp == null) return ComparableHelper.less(v, w);
|
||||
else
|
||||
return cmp.compare(v, w) < 0;
|
||||
}
|
||||
|
||||
static <T> boolean equals(T v, T w, Comparator<T> cmp) {
|
||||
if (cmp == null) return ComparableHelper.equals(v, w);
|
||||
else
|
||||
return cmp.compare(v, w) == 0;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package com.hithomelabs.princeton1.module5;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
public interface ComparatorSort<E> extends ComparableSort<E> {
|
||||
|
||||
public void sort(E[] arr, Comparator<E> cmp);
|
||||
|
||||
}
|
@ -1,23 +1,41 @@
|
||||
package com.hithomelabs.princeton1.module5;
|
||||
|
||||
public class Insertion<E> extends AbstractCustomSorts<E> {
|
||||
import java.util.Comparator;
|
||||
|
||||
public void sort(E[] arr){
|
||||
public class Insertion<E> implements MeasurableSort<E>, MeasurableHelper{
|
||||
|
||||
public void sort(E[] arr) {
|
||||
coreSortLogic(arr, null, null);
|
||||
|
||||
}
|
||||
|
||||
private void coreSortLogic(E[] arr, Comparator<E> cmp, SortingMetaData metaData) {
|
||||
if (arr == null) return;
|
||||
else{
|
||||
else {
|
||||
int N = arr.length;
|
||||
// * * swap arr[i] with each element greater to it's left
|
||||
for (int i = 1; i < N; i++){
|
||||
for (int i = 1; i < N; i++) {
|
||||
int j = i;
|
||||
while(j >= 1 && less((Comparable<E>)arr[j], arr[j-1])){
|
||||
exch(arr, j, j-1);
|
||||
j = j-1;
|
||||
while (j >= 1 && MeasurableHelper.less(arr[j], arr[j - 1], cmp, metaData)) {
|
||||
MeasurableHelper.exch(arr, j, j - 1, metaData);
|
||||
j = j - 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void sort(E[] arr, Comparator<E> cmp, SortingMetaData metaData) {
|
||||
if (metaData != null)
|
||||
metaData.startTime();
|
||||
coreSortLogic(arr, cmp, metaData);
|
||||
if (metaData != null)
|
||||
metaData.endTime();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sort(E[] arr, Comparator<E> cmp) {
|
||||
coreSortLogic(arr, cmp, null);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,25 @@
|
||||
package com.hithomelabs.princeton1.module5;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
public interface MeasurableHelper extends ComparatorHelper{
|
||||
|
||||
static <T> boolean less(T v, T w, Comparator<T> cmp, SortingMetaData metaData) {
|
||||
if (metaData != null)
|
||||
metaData.incrementCompares();
|
||||
return ComparatorHelper.less(v, w, cmp);
|
||||
}
|
||||
|
||||
static <T> boolean equals(T v, T w, Comparator<T> cmp, SortingMetaData metaData) {
|
||||
if (metaData != null)
|
||||
metaData.incrementCompares();
|
||||
return ComparatorHelper.equals(v, w, cmp);
|
||||
}
|
||||
|
||||
static <T> void exch(T[] arr, int j, int i, SortingMetaData metaData) {
|
||||
if (metaData != null)
|
||||
metaData.incrementExchanges();
|
||||
ComparableHelper.exch(arr, j, i);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package com.hithomelabs.princeton1.module5;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
public interface MeasurableSort<E> extends ComparatorSort<E> {
|
||||
|
||||
public void sort(E[] arr, Comparator<E> cmp, SortingMetaData metaData);
|
||||
|
||||
}
|
@ -1,23 +1,42 @@
|
||||
package com.hithomelabs.princeton1.module5;
|
||||
|
||||
|
||||
public class Selection<E> extends AbstractCustomSorts<E>{
|
||||
import java.util.Comparator;
|
||||
|
||||
public class Selection<E> implements MeasurableSort<E>, MeasurableHelper {
|
||||
|
||||
/*
|
||||
* * Selection sort "selects" the smallest element and swaps it with arr[0] of the array
|
||||
* * Then proceeds to do the same swapping arr[i] with arr[i:arr.length-1]
|
||||
*/
|
||||
public void sort(E[] arr){
|
||||
coreSortLogic(arr, null, null);
|
||||
}
|
||||
|
||||
private void coreSortLogic(E[] arr, Comparator<E> cmp, SortingMetaData metaData){
|
||||
if (arr == null) return;
|
||||
Comparable<E>[] arr1 = (Comparable<E>[]) arr;
|
||||
for(int i = 0; i < arr1.length - 1; i++){
|
||||
int minIndex = i;
|
||||
for(int j = i+1; j < arr.length; j ++){
|
||||
if (less((Comparable<E>) arr[j], arr[minIndex])) minIndex = j;
|
||||
if (MeasurableHelper.less(arr[j], arr[minIndex], cmp, metaData)) minIndex = j;
|
||||
}
|
||||
exch(arr, i, minIndex);
|
||||
MeasurableHelper.exch(arr, i, minIndex, metaData);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sort(E[] arr, Comparator<E> cmp, SortingMetaData metaData) {
|
||||
if (metaData != null)
|
||||
metaData.startTime();
|
||||
coreSortLogic(arr, cmp, metaData);
|
||||
if (metaData != null)
|
||||
metaData.endTime();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sort(E[] arr, Comparator<E> cmp) {
|
||||
coreSortLogic(arr, cmp, null);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,13 +1,9 @@
|
||||
package com.hithomelabs.princeton1.module5;
|
||||
|
||||
public class Shell<E> extends AbstractCustomSorts<E> {
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
|
||||
// * * sample metadata class to compare no. of sorts and compares, shell sort vs insertion sort
|
||||
private class MetaData{
|
||||
|
||||
int compares;
|
||||
int swaps;
|
||||
}
|
||||
public class Shell<E> implements MeasurableSort<E>, MeasurableHelper {
|
||||
|
||||
/*
|
||||
* * We will be performing h sort
|
||||
@ -24,7 +20,10 @@ public class Shell<E> extends AbstractCustomSorts<E> {
|
||||
*/
|
||||
@Override
|
||||
public void sort(E[] arr) {
|
||||
MetaData metaData = new MetaData();
|
||||
coreSortLogic(arr,null,null);
|
||||
}
|
||||
|
||||
private void coreSortLogic(E[] arr, Comparator<E> cmp, SortingMetaData metaData){
|
||||
int N = arr.length;
|
||||
int h = 1;
|
||||
// * * Calculates the largest value of h greater than n
|
||||
@ -32,22 +31,18 @@ public class Shell<E> extends AbstractCustomSorts<E> {
|
||||
h = 3 * h + 1;
|
||||
}
|
||||
while (h >= 1) {
|
||||
h = hsort(arr, h, metaData);
|
||||
h = hsort(arr, h, cmp, metaData);
|
||||
h = h / 3;
|
||||
}
|
||||
System.out.println("Array sorted (shell sort) with " + metaData.compares + " compares and " + metaData.swaps + " swaps");
|
||||
}
|
||||
|
||||
private int hsort(E[] arr, int h, MetaData metadata) {
|
||||
private int hsort(E[] arr, int h, Comparator<E> cmp, SortingMetaData metaData) {
|
||||
int N = arr.length;
|
||||
for(int i = h; i < N; i++){
|
||||
int j = i;
|
||||
++metadata.compares;
|
||||
while(j >= h && less((Comparable<E>) arr[j], arr[j-h])){
|
||||
++metadata.swaps;
|
||||
exch(arr, j, j-h);
|
||||
while(j >= h && MeasurableHelper.less(arr[j], arr[j-h], cmp, metaData)){
|
||||
MeasurableHelper.exch(arr, j, j-h, metaData);
|
||||
j = j - h;
|
||||
++metadata.compares;
|
||||
}
|
||||
}
|
||||
return h;
|
||||
@ -57,10 +52,21 @@ public class Shell<E> extends AbstractCustomSorts<E> {
|
||||
* Will just be comparing the number of saps taken across both implementations
|
||||
*/
|
||||
public void insertionSort(E[] arr){
|
||||
MetaData metaData = new MetaData();
|
||||
int h = 1;
|
||||
h = hsort(arr, h, metaData);
|
||||
System.out.println("Array sorted (insertion sort) with " + metaData.compares + " compares and " + metaData.swaps + " swaps");
|
||||
h = hsort(arr, h, null, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sort(E[] arr, Comparator<E> cmp, SortingMetaData metaData) {
|
||||
if (metaData != null)
|
||||
metaData.startTime();
|
||||
coreSortLogic(arr, cmp, metaData);
|
||||
if (metaData != null)
|
||||
metaData.endTime();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sort(E[] arr, Comparator<E> cmp) {
|
||||
coreSortLogic(arr, cmp, null);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,34 @@
|
||||
package com.hithomelabs.princeton1.module5;
|
||||
|
||||
public class SortingMetaData {
|
||||
|
||||
private double tick;
|
||||
private double tok;
|
||||
private int exchanges;
|
||||
private int compares;
|
||||
|
||||
public double timeElapsed(){
|
||||
return tok - tick;
|
||||
}
|
||||
|
||||
public void startTime(){
|
||||
tick = System.nanoTime();
|
||||
}
|
||||
|
||||
public void endTime(){
|
||||
tok = System.nanoTime();
|
||||
}
|
||||
|
||||
public void incrementCompares(){
|
||||
++compares;
|
||||
}
|
||||
|
||||
public void incrementExchanges(){
|
||||
++exchanges;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Time elapsed: " + this.timeElapsed() + " nano-seconds \nCompares: "+this.compares+ "\nSwaps: "+this.exchanges;
|
||||
}
|
||||
}
|
@ -11,7 +11,7 @@ import java.util.Random;
|
||||
class SortTest {
|
||||
|
||||
private ArrayList<Apple> apples;
|
||||
private AbstractCustomSorts<Apple> sortingAlgorithm;
|
||||
private ComparableSort<Apple> sortingAlgorithm;
|
||||
private Random random;
|
||||
|
||||
@BeforeEach
|
||||
@ -21,7 +21,7 @@ class SortTest {
|
||||
random = new Random();
|
||||
}
|
||||
|
||||
private void testSort(AbstractCustomSorts<Apple> sortingAlgorithm) {
|
||||
private void testSort(ComparableSort<Apple> sortingAlgorithm) {
|
||||
for (int i = 0; i < 100; i++)
|
||||
apples.add(new Apple(random.nextInt(100)));
|
||||
|
||||
|
@ -1,46 +1,74 @@
|
||||
package com.hithomelabs.princeton1.module6;
|
||||
|
||||
import com.hithomelabs.princeton1.module5.AbstractCustomSorts;
|
||||
import com.hithomelabs.princeton1.module5.ComparableSort;
|
||||
import com.hithomelabs.princeton1.module5.MeasurableHelper;
|
||||
import com.hithomelabs.princeton1.module5.MeasurableSort;
|
||||
import com.hithomelabs.princeton1.module5.SortingMetaData;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
|
||||
public class Merge<E> extends AbstractCustomSorts<E> {
|
||||
public class Merge<E> implements MeasurableSort<E>, MeasurableHelper {
|
||||
@Override
|
||||
public void sort(E[] arr) {
|
||||
coreSortLogic(arr, null, null);
|
||||
}
|
||||
|
||||
private void coreSortLogic(E[] arr, Comparator<E> cmp, SortingMetaData metaData) {
|
||||
int N = arr.length;
|
||||
// * * aux is a helper array required for merge
|
||||
E[] aux = Arrays.copyOf(arr, N);
|
||||
mergesort(arr, aux, 0, N-1);
|
||||
|
||||
mergesort(arr, aux, 0, N - 1, cmp, metaData);
|
||||
}
|
||||
|
||||
private void mergesort(E[] arr, E[] aux, int lo, int hi) {
|
||||
private void mergesort(E[] arr, E[] aux, int lo, int hi, Comparator<E> cmp, SortingMetaData metaData) {
|
||||
|
||||
if (hi <= lo) return;
|
||||
int mid = lo + (hi - lo)/2;
|
||||
mergesort(arr, aux, lo, mid);
|
||||
mergesort(arr, aux, mid+1, hi);
|
||||
merge(arr, aux, lo, mid, hi);
|
||||
int mid = lo + (hi - lo) / 2;
|
||||
mergesort(arr, aux, lo, mid, cmp, metaData);
|
||||
mergesort(arr, aux, mid + 1, hi, cmp, metaData);
|
||||
merge(arr, aux, lo, mid, hi, cmp, metaData);
|
||||
|
||||
}
|
||||
|
||||
private void merge(E[] arr, E[] aux, int lo, int mid, int hi) {
|
||||
private void merge(E[] arr, E[] aux, int lo, int mid, int hi, Comparator<E> cmp, SortingMetaData metaData) {
|
||||
|
||||
// * * creating backup of original array
|
||||
for (int i = lo; i <= hi; i++)
|
||||
aux[i] = arr[i];
|
||||
|
||||
int i = lo;
|
||||
int j = mid+1;
|
||||
for (int k = lo; k <= hi; k++){
|
||||
int j = mid + 1;
|
||||
for (int k = lo; k <= hi; k++) {
|
||||
// * If i has already reached mid, no need to compare we insert at pointer k
|
||||
if(i > mid) arr[k] = aux[j++];
|
||||
else if(j > hi) arr[k] = aux[i++];
|
||||
else if(less((Comparable<E>) aux[i], aux[j])) arr[k] = aux[i++];
|
||||
if (i > mid) {
|
||||
arr[k] = aux[j++];
|
||||
if (metaData != null)
|
||||
metaData.incrementCompares();
|
||||
} else if (j > hi) {
|
||||
arr[k] = aux[i++];
|
||||
if (metaData != null)
|
||||
metaData.incrementCompares();
|
||||
} else if (MeasurableHelper.less( aux[i], aux[j], cmp, metaData)) {
|
||||
arr[k] = aux[i++];
|
||||
}
|
||||
else arr[k] = aux[j++];
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sort(E[] arr, Comparator<E> cmp, SortingMetaData metaData) {
|
||||
if (metaData != null)
|
||||
metaData.startTime();
|
||||
coreSortLogic(arr, cmp, metaData);
|
||||
if (metaData != null)
|
||||
metaData.endTime();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sort(E[] arr, Comparator<E> cmp) {
|
||||
coreSortLogic(arr, cmp, null);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
package com.hithomelabs.princeton1.module6;
|
||||
|
||||
import com.hithomelabs.princeton1.module5.AbstractCustomSorts;
|
||||
import com.hithomelabs.princeton1.module5.ComparableSort;
|
||||
import com.hithomelabs.princeton1.common.Apple;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
@ -14,7 +14,7 @@ import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class MergeTest {
|
||||
|
||||
private AbstractCustomSorts<Apple> sortingAlgorithm;
|
||||
private ComparableSort<Apple> sortingAlgorithm;
|
||||
private ArrayList<Apple> apples;
|
||||
private Random random;
|
||||
|
||||
|
@ -1,13 +1,17 @@
|
||||
package com.hithomelabs.princeton1.module7;
|
||||
|
||||
import com.hithomelabs.princeton1.module5.AbstractCustomSorts;
|
||||
import com.hithomelabs.princeton1.module5.MeasurableHelper;
|
||||
import com.hithomelabs.princeton1.module5.MeasurableSort;
|
||||
import com.hithomelabs.princeton1.module5.SortingMetaData;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
|
||||
public class Quick<E> extends AbstractCustomSorts<E> {
|
||||
public class Quick<E> implements MeasurableSort<E>, MeasurableHelper {
|
||||
@Override
|
||||
public void sort(E[] arr) {
|
||||
int N = arr.length;
|
||||
quickSort(arr, 0, N - 1);
|
||||
quickSort(arr, 0, N - 1, null, null);
|
||||
}
|
||||
|
||||
public void altSort(E[] arr) {
|
||||
@ -20,35 +24,51 @@ public class Quick<E> extends AbstractCustomSorts<E> {
|
||||
int i = lo + 1;
|
||||
int j = i;
|
||||
while(j <= hi){
|
||||
if(less((Comparable<E>) arr[j], arr[lo])){
|
||||
exch(arr, i, j);
|
||||
if(MeasurableHelper.less((Comparable<E>) arr[j], arr[lo], null, null)){
|
||||
MeasurableHelper.exch(arr, i, j, null);
|
||||
i++;
|
||||
}
|
||||
j++;
|
||||
}
|
||||
exch(arr, i-1, lo);
|
||||
MeasurableHelper.exch(arr, i-1, lo,null);
|
||||
altQuickSort(arr, lo, i-2);
|
||||
altQuickSort(arr, i, hi);
|
||||
}
|
||||
|
||||
private void quickSort(E[] arr, int lo, int hi) {
|
||||
private void quickSort(E[] arr, int lo, int hi, Comparator<E> cmp, SortingMetaData metaData) {
|
||||
|
||||
if (lo >= hi) return;
|
||||
int i = lo;
|
||||
int j = hi+1;
|
||||
while(true){
|
||||
while(less((Comparable<E>) arr[++i], arr[lo])){
|
||||
while(MeasurableHelper.less( arr[++i], arr[lo], cmp, metaData)){
|
||||
if(i == hi) break;
|
||||
}
|
||||
while(!less((Comparable<E>) arr[--j], arr[lo])){
|
||||
while(!MeasurableHelper.less(arr[--j], arr[lo], cmp, metaData)){
|
||||
if (j == lo ) break;
|
||||
}
|
||||
if(j<=i) break;
|
||||
exch(arr, i , j);
|
||||
MeasurableHelper.exch(arr, i , j, metaData);
|
||||
}
|
||||
exch(arr, j, lo);
|
||||
quickSort(arr, lo, j-1);
|
||||
quickSort(arr, j+1, hi);
|
||||
MeasurableHelper.exch(arr, j, lo, metaData);
|
||||
quickSort(arr, lo, j-1, cmp, metaData);
|
||||
quickSort(arr, j+1, hi, cmp, metaData);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sort(E[] arr, Comparator<E> cmp) {
|
||||
int N = arr.length;
|
||||
quickSort(arr, 0, N - 1, cmp, null);
|
||||
}
|
||||
|
||||
public void sort(E[] arr, Comparator<E> cmp, SortingMetaData metaData) {
|
||||
if(metaData != null){
|
||||
metaData.startTime();
|
||||
}
|
||||
int N = arr.length;
|
||||
quickSort(arr, 0, N - 1, cmp, metaData);
|
||||
if (metaData != null)
|
||||
metaData.endTime();
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,53 @@
|
||||
package com.hithomelabs.princeton1.module7;
|
||||
|
||||
import com.hithomelabs.princeton1.module5.MeasurableHelper;
|
||||
import com.hithomelabs.princeton1.module5.MeasurableSort;
|
||||
import com.hithomelabs.princeton1.module5.SortingMetaData;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
public class ThreeWayQuick<E> implements MeasurableSort<E>, MeasurableHelper {
|
||||
@Override
|
||||
public void sort(E[] arr) {
|
||||
coreSortingLogic(arr, null, null);
|
||||
}
|
||||
|
||||
private void coreSortingLogic(E[] arr, Comparator<E> cmp, SortingMetaData metaData) {
|
||||
int N = arr.length;
|
||||
threeWaySort(arr, 0, N - 1, cmp, metaData);
|
||||
}
|
||||
|
||||
private void threeWaySort(E[] arr, int lo, int hi, Comparator<E> cmp, SortingMetaData metaData) {
|
||||
|
||||
if (hi <= lo)
|
||||
return;
|
||||
int k = hi;
|
||||
int i = lo + 1;
|
||||
int j = lo;
|
||||
|
||||
while (true) {
|
||||
if (MeasurableHelper.less(arr[i], arr[j], cmp, metaData)) MeasurableHelper.exch(arr, i++, j++, metaData);
|
||||
else if (MeasurableHelper.equals(arr[i],arr[j], cmp, metaData)) i++;
|
||||
else MeasurableHelper.exch(arr, i, k--, metaData);
|
||||
if (k < i) break;
|
||||
}
|
||||
threeWaySort(arr, lo, j - 1, cmp, metaData);
|
||||
threeWaySort(arr, k + 1, hi, cmp, metaData);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sort(E[] arr, Comparator<E> cmp, SortingMetaData metaData) {
|
||||
if(metaData != null){
|
||||
metaData.startTime();
|
||||
}
|
||||
coreSortingLogic(arr, cmp, metaData);
|
||||
if (metaData != null)
|
||||
metaData.endTime();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sort(E[] arr, Comparator<E> cmp) {
|
||||
coreSortingLogic(arr, cmp, null);
|
||||
}
|
||||
|
||||
}
|
@ -34,7 +34,7 @@ class QuickTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("testing Quick sort default implementation")
|
||||
@DisplayName("testing Quick sort alternate implementation")
|
||||
public void testAltSort(){
|
||||
|
||||
for(int i = 0; i < 100; i++)
|
||||
@ -46,6 +46,18 @@ class QuickTest {
|
||||
Assertions.assertArrayEquals(sorted, arr);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("testing Comparator sort")
|
||||
public void testComparatorSort(){
|
||||
for(int i = 0; i < 100; i++)
|
||||
apples.add(new Apple(random.nextInt(1000)));
|
||||
Apple[] arr = apples.toArray(new Apple[apples.size()]);
|
||||
quick.sort(arr, Apple.COMPARE_BY_SIZE);
|
||||
apples.sort(Apple.COMPARE_BY_SIZE);
|
||||
Apple[] sorted = apples.toArray(new Apple[apples.size()]);
|
||||
Assertions.assertArrayEquals(sorted, arr);
|
||||
}
|
||||
|
||||
|
||||
@AfterEach
|
||||
void tearDown() {
|
||||
|
@ -0,0 +1,54 @@
|
||||
package com.hithomelabs.princeton1.module7;
|
||||
|
||||
import com.hithomelabs.princeton1.common.Apple;
|
||||
import org.junit.jupiter.api.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Random;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
class ThreeWayQuickTest {
|
||||
|
||||
private ThreeWayQuick<Apple> quick;
|
||||
private ArrayList<Apple> apples;
|
||||
private Random random;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
quick = new ThreeWayQuick<Apple>();
|
||||
apples = new ArrayList<Apple>();
|
||||
random = new Random();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("testing Quick sort default implementation")
|
||||
public void testSort(){
|
||||
for(int i = 0; i < 100; i++)
|
||||
apples.add(new Apple(random.nextInt(1000)));
|
||||
Apple[] arr = apples.toArray(new Apple[apples.size()]);
|
||||
quick.sort(arr);
|
||||
apples.sort(null);
|
||||
Apple[] sorted = apples.toArray(new Apple[apples.size()]);
|
||||
Assertions.assertArrayEquals(sorted, arr);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("testing Comparator sort")
|
||||
public void testComparatorSort(){
|
||||
for(int i = 0; i < 100; i++)
|
||||
apples.add(new Apple(random.nextInt(1000)));
|
||||
Apple[] arr = apples.toArray(new Apple[apples.size()]);
|
||||
quick.sort(arr, Apple.COMPARE_BY_SIZE);
|
||||
apples.sort(Apple.COMPARE_BY_SIZE);
|
||||
Apple[] sorted = apples.toArray(new Apple[apples.size()]);
|
||||
Assertions.assertArrayEquals(sorted, arr);
|
||||
}
|
||||
|
||||
|
||||
@AfterEach
|
||||
void tearDown() {
|
||||
quick = null;
|
||||
apples = null;
|
||||
random = null;
|
||||
}
|
||||
}
|
21
module8/build.gradle
Normal file
21
module8/build.gradle
Normal file
@ -0,0 +1,21 @@
|
||||
plugins {
|
||||
id 'java'
|
||||
}
|
||||
|
||||
group = 'com.hithomelabs.princeton1.module8'
|
||||
version = 'unspecified'
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
testImplementation platform('org.junit:junit-bom:5.10.0')
|
||||
testImplementation 'org.junit.jupiter:junit-jupiter'
|
||||
implementation project(':module5')
|
||||
testImplementation project(':common')
|
||||
}
|
||||
|
||||
test {
|
||||
useJUnitPlatform()
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
package com.hithomelabs.princeton1.module8;
|
||||
|
||||
/*
|
||||
* * Designing Heap as a wrapper class with static methods that perform sink and swim operations on underlying comparable[] array
|
||||
* * Heap does not have any instance variables, instance variables shall be housed by, the implementation using heaps, like priority queues and heap sort
|
||||
*/
|
||||
|
||||
|
||||
import com.hithomelabs.princeton1.module5.MeasurableHelper;
|
||||
import com.hithomelabs.princeton1.module5.SortingMetaData;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
public class Heap implements MeasurableHelper {
|
||||
|
||||
/*
|
||||
* * Sink Node T with to it's appropriate place in a Heap of size N, if it's children exist and are greater
|
||||
*/
|
||||
public static <T> void sink(T[] arr, int root, int N, Comparator<T> cmp, SortingMetaData metaData){
|
||||
// * Check if at least one child exists
|
||||
while(2 * root <= N){
|
||||
int j = 2 * root;
|
||||
// * Check if the right child exists and is larger than the left child, if yes swap right and left child
|
||||
if(j+1 <= N){
|
||||
if (MeasurableHelper.less(arr[j], arr[j+1], cmp, metaData)) j++;
|
||||
}
|
||||
if (!MeasurableHelper.less(arr[root], arr[j], cmp, metaData)) break;
|
||||
MeasurableHelper.exch(arr, root, j, metaData);
|
||||
// * The root node has now sunken low, call sink recursively with new node to check if it sinks further
|
||||
root = j;
|
||||
}
|
||||
}
|
||||
|
||||
// * * Swim if element is not root, and parent is lesser than node
|
||||
public static <T extends Comparable<T>> void swim(T[] arr, int node, int N, Comparator<T> cmp, SortingMetaData metaData){
|
||||
while(node > 1){
|
||||
if(! MeasurableHelper.less(arr[node/2],arr[node], cmp, metaData)) break;
|
||||
MeasurableHelper.exch(arr, node, node/2, metaData);
|
||||
node = node/2;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,62 @@
|
||||
package com.hithomelabs.princeton1.module8;
|
||||
|
||||
import com.hithomelabs.princeton1.module5.MeasurableHelper;
|
||||
import com.hithomelabs.princeton1.module5.MeasurableSort;
|
||||
import com.hithomelabs.princeton1.module5.SortingMetaData;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
|
||||
public class HeapSort<E> implements MeasurableSort<E>, MeasurableHelper {
|
||||
|
||||
@Override
|
||||
public void sort(E[] arr, Comparator<E> cmp, SortingMetaData metaData) {
|
||||
int N = arr.length;
|
||||
E[] heapArr = (E[]) new Object[N+1];
|
||||
// * * to simplify we copy original array from
|
||||
System.arraycopy(arr, 0, heapArr, 1, N);
|
||||
// * * An array of size N holds a heap of size N-1
|
||||
if (metaData != null)
|
||||
metaData.startTime();
|
||||
coreSortingLogic(heapArr, N, cmp, metaData);
|
||||
if (metaData != null)
|
||||
metaData.endTime();
|
||||
System.arraycopy(heapArr, 1, arr, 0, N);
|
||||
}
|
||||
|
||||
private void coreSortingLogic(E[] arr, int N, Comparator<E> cmp, SortingMetaData metaData) {
|
||||
// * * Converting array to max-heap an array in place
|
||||
for (int i = N/2; i >= 1; i--)
|
||||
Heap.sink(arr, i, N, cmp, metaData);
|
||||
// * * After converting to max-heap, in every iteration remove the max element of the heap
|
||||
while(N > 1){
|
||||
MeasurableHelper.exch(arr, 1, N--, metaData);
|
||||
Heap.sink(arr, 1, N, cmp, metaData);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void sort(E[] arr, Comparator<E> cmp) {
|
||||
int N = arr.length;
|
||||
E[] heapArr = (E[]) new Object[N+1];
|
||||
// * * to simplify we copy original array from
|
||||
System.arraycopy(arr, 0, heapArr, 1, N);
|
||||
|
||||
// * * An array of size N holds a heap of size N-1
|
||||
coreSortingLogic(heapArr, N, cmp, null);
|
||||
System.arraycopy(heapArr, 1, arr, 0, N);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sort(E[] arr) {
|
||||
int N = arr.length;
|
||||
E[] heapArr = (E[]) new Object[N+1];
|
||||
// * * to simplify we copy original array from
|
||||
System.arraycopy(arr, 0, heapArr, 1, N);
|
||||
|
||||
// * * An array of size N holds a heap of size N-1
|
||||
coreSortingLogic(heapArr, N,null, null);
|
||||
System.arraycopy(heapArr, 1, arr, 0, N);
|
||||
}
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
package com.hithomelabs.princeton1.module8;
|
||||
|
||||
import com.hithomelabs.princeton1.common.Apple;
|
||||
import org.junit.jupiter.api.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Random;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class HeapSortTest {
|
||||
|
||||
private HeapSort<Apple> heap;
|
||||
private ArrayList<Apple> apples;
|
||||
private Random random;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
heap = new HeapSort<Apple>();
|
||||
apples = new ArrayList<Apple>();
|
||||
random = new Random();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Sort a heap of a 100 random sized apples")
|
||||
public void testSort(){
|
||||
for(int i = 0; i < 100; i++)
|
||||
apples.add(new Apple(random.nextInt(1000)));
|
||||
Apple[] arr = apples.toArray(new Apple[apples.size()]);
|
||||
heap.sort(arr);
|
||||
apples.sort(null);
|
||||
Apple[] sorted = apples.toArray(new Apple[apples.size()]);
|
||||
Assertions.assertArrayEquals(sorted, arr);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Sort a heap of a 100 random sized with Comparator")
|
||||
public void testComparatorSort(){
|
||||
for(int i = 0; i < 100; i++)
|
||||
apples.add(new Apple(random.nextInt(1000)));
|
||||
Apple[] arr = apples.toArray(new Apple[apples.size()]);
|
||||
heap.sort(arr, Apple.COMPARE_BY_SIZE);
|
||||
apples.sort(Apple.COMPARE_BY_SIZE);
|
||||
Apple[] sorted = apples.toArray(new Apple[apples.size()]);
|
||||
Assertions.assertArrayEquals(sorted, arr);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Sort 100 apples in descending order")
|
||||
public void testSortBorder(){
|
||||
for(int i = 0; i < 100; i++)
|
||||
apples.add(new Apple(100 - i));
|
||||
Apple[] arr = apples.toArray(new Apple[apples.size()]);
|
||||
heap.sort(arr);
|
||||
apples.sort(null);
|
||||
Apple[] sorted = apples.toArray(new Apple[apples.size()]);
|
||||
Assertions.assertArrayEquals(sorted, arr);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void tearDown() {
|
||||
}
|
||||
}
|
@ -17,4 +17,5 @@ include 'module5'
|
||||
include 'module6'
|
||||
include 'common'
|
||||
include 'module7'
|
||||
include 'module8'
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user