Making all sorts measurable and compatible to use of comparator
This commit is contained in:
parent
6a43e7d183
commit
dd1ec6e2cd
@ -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;}
|
||||
|
||||
}
|
@ -4,14 +4,4 @@ public interface ComparableSort<E> {
|
||||
|
||||
void sort(E[] arr);
|
||||
|
||||
default void exch(E[] arr, int j, int i) {
|
||||
E temp = arr[i];
|
||||
arr[i] = arr[j];
|
||||
arr[j] = temp;
|
||||
}
|
||||
|
||||
default boolean less(Comparable<E> e1, E e2) {
|
||||
return e1.compareTo(e2) < 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,15 +0,0 @@
|
||||
package com.hithomelabs.princeton1.module5;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
public interface ComparatorComparableSort<E> extends ComparableSort<E> {
|
||||
|
||||
public void sort(E[] arr, Comparator<E> cmp);
|
||||
|
||||
public default boolean less(E v, E w, Comparator<E> cmp){
|
||||
if (cmp == null)
|
||||
return ComparableSort.super.less((Comparable<E>) v, w);
|
||||
else
|
||||
return cmp.compare(v, w) < 0;
|
||||
}
|
||||
}
|
@ -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);
|
||||
|
||||
}
|
@ -2,7 +2,7 @@ package com.hithomelabs.princeton1.module5;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
public class Insertion<E> implements MeasurableSort<E> {
|
||||
public class Insertion<E> implements MeasurableSort<E>, MeasurableHelper{
|
||||
|
||||
public void sort(E[] arr) {
|
||||
coreSortLogic(arr, null, null);
|
||||
@ -16,8 +16,8 @@ public class Insertion<E> implements MeasurableSort<E> {
|
||||
// * * swap arr[i] with each element greater to it's left
|
||||
for (int i = 1; i < N; i++) {
|
||||
int j = i;
|
||||
while (j >= 1 && less(arr[j], arr[j - 1], cmp, metaData)) {
|
||||
exch(arr, j, j - 1, metaData);
|
||||
while (j >= 1 && MeasurableHelper.less(arr[j], arr[j - 1], cmp, metaData)) {
|
||||
MeasurableHelper.exch(arr, j, j - 1, metaData);
|
||||
j = j - 1;
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
@ -2,20 +2,7 @@ package com.hithomelabs.princeton1.module5;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
public interface MeasurableSort<E> extends ComparatorComparableSort<E> {
|
||||
|
||||
|
||||
default boolean less(E v, E w, Comparator<E> cmp, SortingMetaData metaData) {
|
||||
if (metaData != null)
|
||||
metaData.incrementCompares();
|
||||
return ComparatorComparableSort.super.less(v, w, cmp);
|
||||
}
|
||||
|
||||
default void exch(E[] arr, int j, int i, SortingMetaData metaData) {
|
||||
if (metaData != null)
|
||||
metaData.incrementExchanges();
|
||||
ComparatorComparableSort.super.exch(arr, j, i);
|
||||
}
|
||||
public interface MeasurableSort<E> extends ComparatorSort<E> {
|
||||
|
||||
public void sort(E[] arr, Comparator<E> cmp, SortingMetaData metaData);
|
||||
|
||||
|
@ -3,7 +3,7 @@ package com.hithomelabs.princeton1.module5;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
public class Selection<E> implements MeasurableSort<E> {
|
||||
public class Selection<E> implements MeasurableSort<E>, MeasurableHelper {
|
||||
|
||||
/*
|
||||
* * Selection sort "selects" the smallest element and swaps it with arr[0] of the array
|
||||
@ -19,9 +19,9 @@ public class Selection<E> implements MeasurableSort<E> {
|
||||
for(int i = 0; i < arr1.length - 1; i++){
|
||||
int minIndex = i;
|
||||
for(int j = i+1; j < arr.length; j ++){
|
||||
if (less(arr[j], arr[minIndex], cmp, metaData)) minIndex = j;
|
||||
if (MeasurableHelper.less(arr[j], arr[minIndex], cmp, metaData)) minIndex = j;
|
||||
}
|
||||
exch(arr, i, minIndex, metaData);
|
||||
MeasurableHelper.exch(arr, i, minIndex, metaData);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -3,7 +3,7 @@ package com.hithomelabs.princeton1.module5;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
|
||||
public class Shell<E> implements MeasurableSort<E> {
|
||||
public class Shell<E> implements MeasurableSort<E>, MeasurableHelper {
|
||||
|
||||
/*
|
||||
* * We will be performing h sort
|
||||
@ -40,8 +40,8 @@ public class Shell<E> implements MeasurableSort<E> {
|
||||
int N = arr.length;
|
||||
for(int i = h; i < N; i++){
|
||||
int j = i;
|
||||
while(j >= h && less(arr[j], arr[j-h], cmp, metaData)){
|
||||
exch(arr, j, j-h, metaData);
|
||||
while(j >= h && MeasurableHelper.less(arr[j], arr[j-h], cmp, metaData)){
|
||||
MeasurableHelper.exch(arr, j, j-h, metaData);
|
||||
j = j - h;
|
||||
}
|
||||
}
|
||||
|
@ -1,13 +1,14 @@
|
||||
package com.hithomelabs.princeton1.module6;
|
||||
|
||||
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> implements MeasurableSort<E> {
|
||||
public class Merge<E> implements MeasurableSort<E>, MeasurableHelper {
|
||||
@Override
|
||||
public void sort(E[] arr) {
|
||||
coreSortLogic(arr, null, null);
|
||||
@ -48,7 +49,7 @@ public class Merge<E> implements MeasurableSort<E> {
|
||||
arr[k] = aux[i++];
|
||||
if (metaData != null)
|
||||
metaData.incrementCompares();
|
||||
} else if (less( aux[i], aux[j], cmp, metaData)) {
|
||||
} else if (MeasurableHelper.less( aux[i], aux[j], cmp, metaData)) {
|
||||
arr[k] = aux[i++];
|
||||
}
|
||||
else arr[k] = aux[j++];
|
||||
|
@ -1,12 +1,13 @@
|
||||
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 Quick<E> implements MeasurableSort<E> {
|
||||
public class Quick<E> implements MeasurableSort<E>, MeasurableHelper {
|
||||
@Override
|
||||
public void sort(E[] arr) {
|
||||
int N = arr.length;
|
||||
@ -23,13 +24,13 @@ import java.util.Comparator;
|
||||
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);
|
||||
}
|
||||
@ -40,16 +41,16 @@ import java.util.Comparator;
|
||||
int i = lo;
|
||||
int j = hi+1;
|
||||
while(true){
|
||||
while(less( arr[++i], arr[lo], cmp, metaData)){
|
||||
while(MeasurableHelper.less( arr[++i], arr[lo], cmp, metaData)){
|
||||
if(i == hi) break;
|
||||
}
|
||||
while(!less(arr[--j], arr[lo], cmp, metaData)){
|
||||
while(!MeasurableHelper.less(arr[--j], arr[lo], cmp, metaData)){
|
||||
if (j == lo ) break;
|
||||
}
|
||||
if(j<=i) break;
|
||||
exch(arr, i , j, metaData);
|
||||
MeasurableHelper.exch(arr, i , j, metaData);
|
||||
}
|
||||
exch(arr, j, lo, metaData);
|
||||
MeasurableHelper.exch(arr, j, lo, metaData);
|
||||
quickSort(arr, lo, j-1, cmp, metaData);
|
||||
quickSort(arr, j+1, hi, cmp, metaData);
|
||||
|
||||
|
@ -1,41 +1,53 @@
|
||||
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;
|
||||
|
||||
public class ThreeWayQuick<E> extends AbstractCustomSorts<E> {
|
||||
import java.util.Comparator;
|
||||
|
||||
public class ThreeWayQuick<E> implements MeasurableSort<E>, MeasurableHelper {
|
||||
@Override
|
||||
public void sort(E[] arr) {
|
||||
coreSortingLogic(arr);
|
||||
coreSortingLogic(arr, null, null);
|
||||
}
|
||||
|
||||
private void coreSortingLogic(E[] arr) {
|
||||
private void coreSortingLogic(E[] arr, Comparator<E> cmp, SortingMetaData metaData) {
|
||||
int N = arr.length;
|
||||
threeWaySort(arr, 0, N - 1);
|
||||
threeWaySort(arr, 0, N - 1, cmp, metaData);
|
||||
}
|
||||
|
||||
private void threeWaySort(E[] arr, int lo, int hi) {
|
||||
private void threeWaySort(E[] arr, int lo, int hi, Comparator<E> cmp, SortingMetaData metaData) {
|
||||
|
||||
if(hi <= lo)
|
||||
if (hi <= lo)
|
||||
return;
|
||||
int k = hi;
|
||||
int i = lo + 1;
|
||||
int j = lo;
|
||||
|
||||
while (true) {
|
||||
if (((Comparable<E>) arr[i]).compareTo(arr[j]) < 0) {
|
||||
exch(arr, i, j++);
|
||||
i++;
|
||||
} else if (((Comparable<E>) arr[i]).compareTo(arr[j]) > 0)
|
||||
exch(arr, i, k--);
|
||||
else {
|
||||
i++;
|
||||
}
|
||||
|
||||
if (k < i)
|
||||
break;
|
||||
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);
|
||||
threeWaySort(arr, k + 1, hi);
|
||||
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++)
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -5,48 +5,40 @@ package com.hithomelabs.princeton1.module8;
|
||||
* * 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.AbstractCustomSorts;
|
||||
|
||||
public class Heap{
|
||||
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){
|
||||
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 (less(arr[j], arr[j+1])) j++;
|
||||
if (MeasurableHelper.less(arr[j], arr[j+1], cmp, metaData)) j++;
|
||||
}
|
||||
if (!less(arr[root], arr[j])) break;
|
||||
exch(arr, root, 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){
|
||||
public static <T extends Comparable<T>> void swim(T[] arr, int node, int N, Comparator<T> cmp, SortingMetaData metaData){
|
||||
while(node > 1){
|
||||
if(! less(arr[node/2],arr[node])) break;
|
||||
exch(arr, node, node/2);
|
||||
if(! MeasurableHelper.less(arr[node/2],arr[node], cmp, metaData)) break;
|
||||
MeasurableHelper.exch(arr, node, node/2, metaData);
|
||||
node = node/2;
|
||||
}
|
||||
}
|
||||
|
||||
private static <T> void exch(T[] arr, int i, int j){
|
||||
T temp = arr[i];
|
||||
arr[i] = arr[j];
|
||||
arr[j] = temp;
|
||||
}
|
||||
|
||||
private static <T> boolean less(T v, T w){
|
||||
|
||||
if(((Comparable<T>)v).compareTo(w) < 1 ) return true;
|
||||
else return false;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -1,31 +1,62 @@
|
||||
package com.hithomelabs.princeton1.module8;
|
||||
|
||||
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 HeapSort<E> extends AbstractCustomSorts<E> {
|
||||
public class HeapSort<E> implements MeasurableSort<E>, MeasurableHelper {
|
||||
|
||||
@Override
|
||||
public void sort(E[] arr) {
|
||||
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);
|
||||
coreSortingLogic(heapArr, N, cmp, null);
|
||||
System.arraycopy(heapArr, 1, arr, 0, N);
|
||||
}
|
||||
|
||||
private void coreSortingLogic(E[] arr, int N) {
|
||||
// * * Converting array to max-heap an array in place
|
||||
for (int i = N/2; i >= 1; i--)
|
||||
Heap.sink(arr, i, N);
|
||||
// * * After converting to max-heap, in every iteration remove the max element of the heap
|
||||
while(N > 1){
|
||||
exch(arr, 1, N--);
|
||||
Heap.sink(arr, 1, 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);
|
||||
}
|
||||
}
|
||||
|
@ -33,6 +33,18 @@ class HeapSortTest {
|
||||
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(){
|
||||
|
Loading…
Reference in New Issue
Block a user