Benchmarks complete
All checks were successful
sample gradle build and test / build (pull_request) Successful in 1m5s

This commit is contained in:
hitanshu310 2025-02-18 04:05:28 +05:30 committed by hitanshu
parent acc65dc756
commit 743f6a761e
19 changed files with 429 additions and 37 deletions

125
Roadmaps/sorts.md Normal file
View File

@ -0,0 +1,125 @@
### Sorting an array of size (n) : 32
n^2 for this array is : 1024.0<br>
nlogn for this array is : 160.0
| ARRAY NATURE | ALGORITHM | TIME ELAPSED (ns) | COMPARES | EXCHANGES |
|--|--|--|--|--|
| ALREADY SORTED | Selection Sort | 146007.0 | 496.0 | 31.0 |
| ALREADY SORTED | Insertion Sort | 18685.0 | 31.0 | 0.0 |
| ALREADY SORTED | Shell Sort | 10791.0 | 78.0 | 0.0 |
| ALREADY SORTED | Merge Sort | 56837.0 | 160.0 | 0.0 |
| ALREADY SORTED | Quick Sort | 44935.0 | 558.0 | 31.0 |
| ALREADY SORTED | Three Way Quick Sort | 84400.0 | 357.0 | 195.0 |
| ALREADY SORTED | Heap Sort | 35678.0 | 231.0 | 146.0 |
| PARTIALLY SORTED | Selection Sort | 31420.0 | 496.0 | 31.0 |
| PARTIALLY SORTED | Insertion Sort | 10620.0 | 100.0 | 70.0 |
| PARTIALLY SORTED | Shell Sort | 12814.0 | 121.0 | 48.0 |
| PARTIALLY SORTED | Merge Sort | 28103.0 | 160.0 | 0.0 |
| PARTIALLY SORTED | Quick Sort | 17834.0 | 187.0 | 30.0 |
| PARTIALLY SORTED | Three Way Quick Sort | 53461.0 | 240.0 | 145.0 |
| PARTIALLY SORTED | Heap Sort | 10079.0 | 226.0 | 142.0 |
| REVERSE SORTED | Selection Sort | 31490.0 | 496.0 | 31.0 |
| REVERSE SORTED | Insertion Sort | 51979.0 | 496.0 | 496.0 |
| REVERSE SORTED | Shell Sort | 19998.0 | 114.0 | 58.0 |
| REVERSE SORTED | Merge Sort | 24306.0 | 160.0 | 0.0 |
| REVERSE SORTED | Quick Sort | 38042.0 | 542.0 | 31.0 |
| REVERSE SORTED | Three Way Quick Sort | 55355.0 | 496.0 | 496.0 |
| REVERSE SORTED | Heap Sort | 8726.0 | 202.0 | 112.0 |
| RANDOMLY ORDERED | Selection Sort | 38714.0 | 496.0 | 31.0 |
| RANDOMLY ORDERED | Insertion Sort | 28294.0 | 282.0 | 255.0 |
| RANDOMLY ORDERED | Shell Sort | 15409.0 | 152.0 | 89.0 |
| RANDOMLY ORDERED | Merge Sort | 25057.0 | 160.0 | 0.0 |
| RANDOMLY ORDERED | Quick Sort | 18345.0 | 228.0 | 37.0 |
| RANDOMLY ORDERED | Three Way Quick Sort | 27702.0 | 229.0 | 157.0 |
| RANDOMLY ORDERED | Heap Sort | 9458.0 | 218.0 | 133.0 |
| FEW FREQUENTLY OCCURING | Selection Sort | 32061.0 | 496.0 | 31.0 |
| FEW FREQUENTLY OCCURING | Insertion Sort | 21601.0 | 213.0 | 183.0 |
| FEW FREQUENTLY OCCURING | Shell Sort | 11792.0 | 118.0 | 50.0 |
| FEW FREQUENTLY OCCURING | Merge Sort | 26440.0 | 160.0 | 0.0 |
| FEW FREQUENTLY OCCURING | Quick Sort | 17994.0 | 203.0 | 38.0 |
| FEW FREQUENTLY OCCURING | Three Way Quick Sort | 17162.0 | 192.0 | 80.0 |
| FEW FREQUENTLY OCCURING | Heap Sort | 9138.0 | 223.0 | 128.0 |
### Sorting an array of size (n) : 512
n^2 for this array is : 262144.0<br>
nlogn for this array is : 4608.0
| ARRAY NATURE | ALGORITHM | TIME ELAPSED (ns) | COMPARES | EXCHANGES |
|--|--|--|--|--|
| ALREADY SORTED | Selection Sort | 4850717.0 | 130816.0 | 511.0 |
| ALREADY SORTED | Insertion Sort | 35989.0 | 511.0 | 0.0 |
| ALREADY SORTED | Shell Sort | 158500.0 | 2529.0 | 0.0 |
| ALREADY SORTED | Merge Sort | 449562.0 | 4608.0 | 0.0 |
| ALREADY SORTED | Quick Sort | 3002463.0 | 131838.0 | 511.0 |
| ALREADY SORTED | Three Way Quick Sort | 1673142.0 | 25227.0 | 13149.0 |
| ALREADY SORTED | Heap Sort | 254052.0 | 7958.0 | 4464.0 |
| PARTIALLY SORTED | Selection Sort | 1965597.0 | 130816.0 | 511.0 |
| PARTIALLY SORTED | Insertion Sort | 2597005.0 | 29114.0 | 28607.0 |
| PARTIALLY SORTED | Shell Sort | 425297.0 | 5355.0 | 2949.0 |
| PARTIALLY SORTED | Merge Sort | 298236.0 | 4608.0 | 0.0 |
| PARTIALLY SORTED | Quick Sort | 188748.0 | 6429.0 | 952.0 |
| PARTIALLY SORTED | Three Way Quick Sort | 300219.0 | 9526.0 | 5537.0 |
| PARTIALLY SORTED | Heap Sort | 221880.0 | 7689.0 | 4266.0 |
| REVERSE SORTED | Selection Sort | 1964334.0 | 130816.0 | 511.0 |
| REVERSE SORTED | Insertion Sort | 8566013.0 | 130816.0 | 130816.0 |
| REVERSE SORTED | Shell Sort | 501150.0 | 4098.0 | 1790.0 |
| REVERSE SORTED | Merge Sort | 133242.0 | 4608.0 | 0.0 |
| REVERSE SORTED | Quick Sort | 2777496.0 | 131582.0 | 511.0 |
| REVERSE SORTED | Three Way Quick Sort | 4018770.0 | 130816.0 | 130816.0 |
| REVERSE SORTED | Heap Sort | 316731.0 | 7203.0 | 3772.0 |
| RANDOMLY ORDERED | Selection Sort | 2139577.0 | 130816.0 | 511.0 |
| RANDOMLY ORDERED | Insertion Sort | 1740760.0 | 66207.0 | 65704.0 |
| RANDOMLY ORDERED | Shell Sort | 845052.0 | 5834.0 | 3503.0 |
| RANDOMLY ORDERED | Merge Sort | 170153.0 | 4608.0 | 0.0 |
| RANDOMLY ORDERED | Quick Sort | 137871.0 | 5685.0 | 1068.0 |
| RANDOMLY ORDERED | Three Way Quick Sort | 266034.0 | 6785.0 | 4434.0 |
| RANDOMLY ORDERED | Heap Sort | 304848.0 | 7658.0 | 4172.0 |
| FEW FREQUENTLY OCCURING | Selection Sort | 5186584.0 | 130816.0 | 511.0 |
| FEW FREQUENTLY OCCURING | Insertion Sort | 1422557.0 | 57576.0 | 57067.0 |
| FEW FREQUENTLY OCCURING | Shell Sort | 432791.0 | 4097.0 | 1723.0 |
| FEW FREQUENTLY OCCURING | Merge Sort | 154453.0 | 4608.0 | 0.0 |
| FEW FREQUENTLY OCCURING | Quick Sort | 278238.0 | 15531.0 | 906.0 |
| FEW FREQUENTLY OCCURING | Three Way Quick Sort | 63401.0 | 2489.0 | 969.0 |
| FEW FREQUENTLY OCCURING | Heap Sort | 280381.0 | 7212.0 | 3819.0 |
### Sorting an array of size (n) : 1024
n^2 for this array is : 1048576.0<br>
nlogn for this array is : 10240.0
| ARRAY NATURE | ALGORITHM | TIME ELAPSED (ns) | COMPARES | EXCHANGES |
|--|--|--|--|--|
| ALREADY SORTED | Selection Sort | 7498749.0 | 523776.0 | 1023.0 |
| ALREADY SORTED | Insertion Sort | 22482.0 | 1023.0 | 0.0 |
| ALREADY SORTED | Shell Sort | 311771.0 | 5601.0 | 0.0 |
| ALREADY SORTED | Merge Sort | 276915.0 | 10240.0 | 0.0 |
| ALREADY SORTED | Quick Sort | 6711786.0 | 525822.0 | 1023.0 |
| ALREADY SORTED | Three Way Quick Sort | 3649069.0 | 70999.0 | 36808.0 |
| ALREADY SORTED | Heap Sort | 546827.0 | 18060.0 | 9968.0 |
| PARTIALLY SORTED | Selection Sort | 5672566.0 | 523776.0 | 1023.0 |
| PARTIALLY SORTED | Insertion Sort | 1121306.0 | 119251.0 | 118229.0 |
| PARTIALLY SORTED | Shell Sort | 888765.0 | 12869.0 | 7515.0 |
| PARTIALLY SORTED | Merge Sort | 998493.0 | 10240.0 | 0.0 |
| PARTIALLY SORTED | Quick Sort | 220528.0 | 13342.0 | 2143.0 |
| PARTIALLY SORTED | Three Way Quick Sort | 1015716.0 | 18425.0 | 10928.0 |
| PARTIALLY SORTED | Heap Sort | 412432.0 | 17351.0 | 9501.0 |
| REVERSE SORTED | Selection Sort | 5445486.0 | 523776.0 | 1023.0 |
| REVERSE SORTED | Insertion Sort | 4216956.0 | 523776.0 | 523776.0 |
| REVERSE SORTED | Shell Sort | 618302.0 | 9175.0 | 4430.0 |
| REVERSE SORTED | Merge Sort | 923041.0 | 10240.0 | 0.0 |
| REVERSE SORTED | Quick Sort | 7040860.0 | 525310.0 | 1023.0 |
| REVERSE SORTED | Three Way Quick Sort | 3.3696654E7 | 523776.0 | 523776.0 |
| REVERSE SORTED | Heap Sort | 374319.0 | 16407.0 | 8542.0 |
| RANDOMLY ORDERED | Selection Sort | 538151.0 | 523776.0 | 1023.0 |
| RANDOMLY ORDERED | Insertion Sort | 2208077.0 | 263988.0 | 262976.0 |
| RANDOMLY ORDERED | Shell Sort | 985999.0 | 13931.0 | 8766.0 |
| RANDOMLY ORDERED | Merge Sort | 1100978.0 | 10240.0 | 0.0 |
| RANDOMLY ORDERED | Quick Sort | 244102.0 | 13790.0 | 2377.0 |
| RANDOMLY ORDERED | Three Way Quick Sort | 911649.0 | 16118.0 | 10074.0 |
| RANDOMLY ORDERED | Heap Sort | 414616.0 | 17306.0 | 9311.0 |
| FEW FREQUENTLY OCCURING | Selection Sort | 536507.0 | 523776.0 | 1023.0 |
| FEW FREQUENTLY OCCURING | Insertion Sort | 1991185.0 | 241533.0 | 240512.0 |
| FEW FREQUENTLY OCCURING | Shell Sort | 561314.0 | 8621.0 | 3374.0 |
| FEW FREQUENTLY OCCURING | Merge Sort | 990428.0 | 10240.0 | 0.0 |
| FEW FREQUENTLY OCCURING | Quick Sort | 746566.0 | 59026.0 | 1844.0 |
| FEW FREQUENTLY OCCURING | Three Way Quick Sort | 229926.0 | 4749.0 | 1894.0 |
| FEW FREQUENTLY OCCURING | Heap Sort | 172988.0 | 16171.0 | 8449.0 |

View File

@ -0,0 +1,30 @@
package com.hithomelabs.clients.Benchmarks;
public class ArraySize {
private final int size;
public ArraySize(int n){
size = n;
}
public int getSize() {
return size;
}
public double getNSquare(){
return Math.pow(size,2);
}
public double getNlogN(){
return size * Math.log(size)/ Math.log(2);
}
public double getLogN(){
return Math.log(size)/ Math.log(2);
}
@Override
public String toString() {
return "### Sorting an array of size (n) : " + getSize() + " \n" + "n^2 for this array is : " + getNSquare() + "<br> \n" + "nlogn for this array is : " + getNlogN() + "\n";
}
}

View File

@ -1,51 +1,41 @@
package com.hithomelabs.clients.Benchmarks;
import com.hithomelabs.clients.Benchmarks.Sortables.*;
import com.hithomelabs.princeton1.common.Apple;
import com.hithomelabs.princeton1.module5.MeasurableSort;
import com.hithomelabs.princeton1.module5.SortingMetaData;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class BenchMark {
private int size;
private ArraySize size;
private List<MeasurableSort<Apple>> algorithms;
private Apple[] sorted;
private Apple[] reverseSorted;
private Apple[] random;
private List<Sortable<Apple>> sortableCollections;
private static final String header = "| ARRAY NATURE | ALGORITHM | TIME ELAPSED (ns) | COMPARES | EXCHANGES |";
// * Helps get a table in markdown
private static final String markdownTableFormatter = "|--|--|--|--|--|";
BenchMark(int size, List<MeasurableSort<Apple>> algorithms) {
this.size = size;
this.size = new ArraySize(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));
}
sortableCollections = new ArrayList<Sortable<Apple>>();
sortableCollections.add(new AlreadySortedApples(size));
sortableCollections.add(new PartiallySortedApples(size, 1000));
sortableCollections.add(new ReverseSortedApples(size));
sortableCollections.add(new RandomlySizedApples(size, 1000));
sortableCollections.add(new FewFrequentlyOccuringApples(size));
}
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);
System.out.println(size);
System.out.println(header);
System.out.println(markdownTableFormatter);
for(Sortable<Apple> array: sortableCollections){
new Bundle<Apple>(array, algorithms).run(Apple.COMPARE_BY_SIZE);
}
System.out.println();
}
}

View File

@ -7,6 +7,8 @@ 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 com.hithomelabs.princeton1.module7.ThreeWayQuick;
import com.hithomelabs.princeton1.module8.HeapSort;
import java.util.ArrayList;
import java.util.List;
@ -21,14 +23,16 @@ public class BenchMarkingClient {
algorithms.add(new Shell<Apple>());
algorithms.add(new Merge<Apple>());
algorithms.add(new Quick<Apple>());
algorithms.add(new ThreeWayQuick<Apple>());
algorithms.add(new HeapSort<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();
//b1.run();
//b2.run();
//b3.run();
b4.run();
}

View File

@ -0,0 +1,38 @@
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.Comparator;
import java.util.List;
/*
* * A sorting bundle, that bundles one array and passes a copy of that array for all algorithms to sort
*/
public class Bundle<E> {
private Sortable<E> array;
private List<MeasurableSort<E>> algorithms;
private Comparator<E> cmp;
Bundle(Sortable<E> array, List<MeasurableSort<E>> algorithms){
this.array = array;
this.algorithms = algorithms;
}
Bundle (Sortable<E> array, List<MeasurableSort<E>> algorithms, Comparator<E> cmp){
this(array, algorithms);
if (cmp != null) this.cmp = cmp;
}
public void run(Comparator<E> cmp){
for (MeasurableSort<E> sortingAlgorithm: algorithms){
SortingMetaData metaData = new SortingMetaData();
sortingAlgorithm.sort(array.getArray(), cmp, metaData);
System.out.println("| " + array.getOrdering() + " | " + sortingAlgorithm + " | " + metaData.timeElapsed() + " | " + metaData.getCompares() + " | " + metaData.getExchanges() + " |" );
}
}
}

View File

@ -0,0 +1,8 @@
package com.hithomelabs.clients.Benchmarks;
public interface Sortable<E> {
public E[] getArray();
public String getOrdering();
}

View File

@ -0,0 +1,25 @@
package com.hithomelabs.clients.Benchmarks.Sortables;
import com.hithomelabs.clients.Benchmarks.Sortable;
import com.hithomelabs.princeton1.common.Apple;
public class AlreadySortedApples implements Sortable<Apple> {
Apple[] apples;
public AlreadySortedApples(int size){
apples = new Apple[size];
for (int i = 0; i < size; i++)
apples[i] = new Apple(i);
}
@Override
public Apple[] getArray() {
return apples.clone();
}
@Override
public String getOrdering() {
return "ALREADY SORTED";
}
}

View File

@ -0,0 +1,30 @@
package com.hithomelabs.clients.Benchmarks.Sortables;
import com.hithomelabs.clients.Benchmarks.Sortable;
import com.hithomelabs.princeton1.common.Apple;
import java.util.Random;
public class FewFrequentlyOccuringApples implements Sortable<Apple> {
Apple[] apples;
Random random;
private static final int DEFAULT_SIZE_RANGE = 10;
public FewFrequentlyOccuringApples(int size){
random = new Random();
apples = new Apple[size];
for (int i = 0; i < size; i++)
apples[i] = new Apple(random.nextInt(DEFAULT_SIZE_RANGE));
}
@Override
public Apple[] getArray() {
return apples.clone();
}
@Override
public String getOrdering() {
return "FEW FREQUENTLY OCCURING";
}
}

View File

@ -0,0 +1,34 @@
package com.hithomelabs.clients.Benchmarks.Sortables;
import com.hithomelabs.clients.Benchmarks.Sortable;
import com.hithomelabs.princeton1.common.Apple;
import com.hithomelabs.princeton1.module5.ComparableHelper;
import com.hithomelabs.princeton1.module8.HeapSort;
import java.util.Random;
public class PartiallySortedApples implements Sortable<Apple>, ComparableHelper {
Apple[] apples;
Random random;
public PartiallySortedApples(int size, int sizeRange){
random = new Random();
apples = new Apple[size];
for (int i = 0; i < size; i++)
apples[i] = new Apple(random.nextInt(sizeRange));
HeapSort.heapify(apples);
int N = apples.length;
for (int i = 0; i < N/2; i++)
ComparableHelper.exch(apples, i, N-i-1);
}
@Override
public Apple[] getArray() {
return apples.clone();
}
@Override
public String getOrdering() {
return "PARTIALLY SORTED";
}
}

View File

@ -0,0 +1,30 @@
package com.hithomelabs.clients.Benchmarks.Sortables;
import com.hithomelabs.clients.Benchmarks.Sortable;
import com.hithomelabs.princeton1.common.Apple;
import java.util.Random;
public class RandomlySizedApples implements Sortable<Apple> {
Apple[] apples;
Random random;
public RandomlySizedApples(int size, int sizeRange){
random = new Random();
apples = new Apple[size];
for (int i = 0; i < size; i++)
apples[i] = new Apple(random.nextInt(sizeRange));
}
@Override
public Apple[] getArray() {
return apples.clone();
}
@Override
public String getOrdering() {
return "RANDOMLY ORDERED";
}
}

View File

@ -0,0 +1,25 @@
package com.hithomelabs.clients.Benchmarks.Sortables;
import com.hithomelabs.clients.Benchmarks.Sortable;
import com.hithomelabs.princeton1.common.Apple;
public class ReverseSortedApples implements Sortable<Apple> {
Apple[] apples;
public ReverseSortedApples(int size){
apples = new Apple[size];
for (int i = 0; i < size; i++)
apples[i] = new Apple(size-i);
}
@Override
public Apple[] getArray() {
return apples.clone();
}
@Override
public String getOrdering() {
return "REVERSE SORTED";
}
}

View File

@ -37,5 +37,10 @@ public class Insertion<E> implements MeasurableSort<E>, MeasurableHelper{
public void sort(E[] arr, Comparator<E> cmp) {
coreSortLogic(arr, cmp, null);
}
@Override
public String toString() {
return "Insertion Sort";
}
}

View File

@ -38,5 +38,10 @@ public class Selection<E> implements MeasurableSort<E>, MeasurableHelper {
public void sort(E[] arr, Comparator<E> cmp) {
coreSortLogic(arr, cmp, null);
}
@Override
public String toString() {
return "Selection Sort";
}
}

View File

@ -69,4 +69,9 @@ public class Shell<E> implements MeasurableSort<E>, MeasurableHelper {
public void sort(E[] arr, Comparator<E> cmp) {
coreSortLogic(arr, cmp, null);
}
@Override
public String toString() {
return "Shell Sort";
}
}

View File

@ -4,8 +4,8 @@ public class SortingMetaData {
private double tick;
private double tok;
private int exchanges;
private int compares;
private double exchanges;
private double compares;
public double timeElapsed(){
return tok - tick;
@ -27,6 +27,13 @@ public class SortingMetaData {
++exchanges;
}
public double getExchanges(){
return exchanges;
}
public double getCompares(){
return compares;
}
@Override
public String toString() {
return "Time elapsed: " + this.timeElapsed() + " nano-seconds \nCompares: "+this.compares+ "\nSwaps: "+this.exchanges;

View File

@ -69,6 +69,11 @@ public class Merge<E> implements MeasurableSort<E>, MeasurableHelper {
public void sort(E[] arr, Comparator<E> cmp) {
coreSortLogic(arr, cmp, null);
}
@Override
public String toString() {
return "Merge Sort";
}
}

View File

@ -71,4 +71,9 @@ import java.util.Comparator;
if (metaData != null)
metaData.endTime();
}
@Override
public String toString() {
return "Quick Sort";
}
}

View File

@ -50,4 +50,9 @@ public class ThreeWayQuick<E> implements MeasurableSort<E>, MeasurableHelper {
coreSortingLogic(arr, cmp, null);
}
@Override
public String toString() {
return "Three Way Quick Sort";
}
}

View File

@ -24,6 +24,17 @@ public class HeapSort<E> implements MeasurableSort<E>, MeasurableHelper {
System.arraycopy(heapArr, 1, arr, 0, N);
}
public static <T> void heapify(T[] arr){
int N = arr.length;
T[] heapArr = (T[]) 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
for (int i = N/2; i >= 1; i--)
Heap.sink(heapArr, i, N, null, null);
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--)
@ -59,4 +70,9 @@ public class HeapSort<E> implements MeasurableSort<E>, MeasurableHelper {
coreSortingLogic(heapArr, N,null, null);
System.arraycopy(heapArr, 1, arr, 0, N);
}
@Override
public String toString() {
return "Heap Sort";
}
}