forked from Hithomelabs/Princeton1
Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
60e8425dd9 | |||
38de5fe0a5 | |||
8c47ac248c | |||
bbc040dcd4 |
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,7 +1,6 @@
|
|||||||
# Ignore Gradle project-specific cache directory
|
# Ignore Gradle project-specific cache directory
|
||||||
.gradle
|
.gradle
|
||||||
# Igonre the .idea directory
|
|
||||||
.idea
|
|
||||||
# Ignore Gradle build output directory
|
# Ignore Gradle build output directory
|
||||||
build
|
build
|
||||||
bin
|
bin
|
1
.idea/.name
generated
Normal file
1
.idea/.name
generated
Normal file
@ -0,0 +1 @@
|
|||||||
|
Pricenton1
|
9
.idea/Princeton1.iml
generated
Normal file
9
.idea/Princeton1.iml
generated
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="JAVA_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||||
|
<exclude-output />
|
||||||
|
<content url="file://$MODULE_DIR$" />
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
2
.idea/gradle.xml
generated
2
.idea/gradle.xml
generated
@ -4,6 +4,8 @@
|
|||||||
<component name="GradleSettings">
|
<component name="GradleSettings">
|
||||||
<option name="linkedExternalProjectsSettings">
|
<option name="linkedExternalProjectsSettings">
|
||||||
<GradleProjectSettings>
|
<GradleProjectSettings>
|
||||||
|
<option name="delegatedBuild" value="false" />
|
||||||
|
<option name="testRunner" value="PLATFORM" />
|
||||||
<option name="externalProjectPath" value="$PROJECT_DIR$" />
|
<option name="externalProjectPath" value="$PROJECT_DIR$" />
|
||||||
<option name="modules">
|
<option name="modules">
|
||||||
<set>
|
<set>
|
||||||
|
8
.idea/modules.xml
generated
8
.idea/modules.xml
generated
@ -1,8 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project version="4">
|
|
||||||
<component name="ProjectModuleManager">
|
|
||||||
<modules>
|
|
||||||
<module fileurl="file://$PROJECT_DIR$/.idea/modules/module4/Pricenton1.module4.test.iml" filepath="$PROJECT_DIR$/.idea/modules/module4/Pricenton1.module4.test.iml" />
|
|
||||||
</modules>
|
|
||||||
</component>
|
|
||||||
</project>
|
|
125
Roadmaps/sorts.md
Normal file
125
Roadmaps/sorts.md
Normal 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 |
|
@ -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";
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,47 @@
|
|||||||
|
package com.hithomelabs.clients.Benchmarks;
|
||||||
|
|
||||||
|
import com.hithomelabs.clients.Benchmarks.Sortables.*;
|
||||||
|
import com.hithomelabs.princeton1.common.Apple;
|
||||||
|
import com.hithomelabs.princeton1.module5.MeasurableSort;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class BenchMark {
|
||||||
|
|
||||||
|
private ArraySize size;
|
||||||
|
private List<MeasurableSort<Apple>> algorithms;
|
||||||
|
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 = new ArraySize(size);
|
||||||
|
this.algorithms = algorithms;
|
||||||
|
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() {
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -0,0 +1,41 @@
|
|||||||
|
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 com.hithomelabs.princeton1.module7.ThreeWayQuick;
|
||||||
|
import com.hithomelabs.princeton1.module8.HeapSort;
|
||||||
|
|
||||||
|
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>());
|
||||||
|
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();
|
||||||
|
b4.run();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -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() + " |" );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
package com.hithomelabs.clients.Benchmarks;
|
||||||
|
|
||||||
|
public interface Sortable<E> {
|
||||||
|
|
||||||
|
public E[] getArray();
|
||||||
|
|
||||||
|
public String getOrdering();
|
||||||
|
}
|
@ -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";
|
||||||
|
}
|
||||||
|
}
|
@ -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";
|
||||||
|
}
|
||||||
|
}
|
@ -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";
|
||||||
|
}
|
||||||
|
}
|
@ -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";
|
||||||
|
}
|
||||||
|
}
|
@ -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";
|
||||||
|
}
|
||||||
|
}
|
@ -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,18 @@
|
|||||||
|
package com.hithomelabs.clients.module8;
|
||||||
|
|
||||||
|
import com.hithomelabs.princeton1.common.Apple;
|
||||||
|
import com.hithomelabs.princeton1.module8.PriorityQueue;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
public class PriorityQueueClient {
|
||||||
|
public static void main(String[] args){
|
||||||
|
PriorityQueue<Apple> priorityQueue = new PriorityQueue<>();
|
||||||
|
for(int i=0; i<100; i++){
|
||||||
|
priorityQueue.enqueue(new Apple(new Random().nextInt(100)));
|
||||||
|
}
|
||||||
|
while (!priorityQueue.isEmpty()){
|
||||||
|
System.out.println(priorityQueue.dequeue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,9 +1,18 @@
|
|||||||
package com.hithomelabs.princeton1.common;
|
package com.hithomelabs.princeton1.common;
|
||||||
|
|
||||||
|
import java.util.Comparator;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
public class Apple implements Comparable<Apple> {
|
public class Apple implements Comparable<Apple> {
|
||||||
private int size;
|
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) {
|
public Apple(int size) {
|
||||||
this.size = size;
|
this.size = size;
|
||||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -2,31 +2,66 @@ package com.hithomelabs.princeton1.module4;
|
|||||||
|
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
public class ArrayQueue<Item> extends Queue<Item> {
|
||||||
|
|
||||||
|
private int capacity=3;
|
||||||
|
private Item[] arr = (Item[]) new Object[capacity];
|
||||||
|
private int head=0,tail=0;
|
||||||
|
|
||||||
public class ArrayQueue<E> extends Queue<E>{
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isEmpty() {
|
public boolean isEmpty() {
|
||||||
return false;
|
return (tail - head) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void resize(int newCapacity){
|
||||||
|
Item[] temp = arr;
|
||||||
|
arr = (Item[]) new Object[newCapacity];
|
||||||
|
int iNew=0,iOld=head;
|
||||||
|
while(iOld<tail){
|
||||||
|
arr[iNew]=temp[iOld];
|
||||||
|
iNew++;
|
||||||
|
iOld++;
|
||||||
|
}
|
||||||
|
|
||||||
|
capacity=newCapacity;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public E dequeue() {
|
public Item dequeue() {
|
||||||
|
if(isEmpty())
|
||||||
return null;
|
return null;
|
||||||
|
// System.out.println(head+" "+arr[head]);
|
||||||
|
Item element = arr[head++];
|
||||||
|
if(size() < capacity/4)
|
||||||
|
resize(capacity/2);
|
||||||
|
return element;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void enqueue(E element) {
|
public void enqueue(Item element) {
|
||||||
|
if(tail == capacity)
|
||||||
|
resize(capacity * 2);
|
||||||
|
arr[tail++] = element;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int size() {
|
public int size() {
|
||||||
return 0;
|
return (tail - head);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Iterator<E> iterator() {
|
public Iterator<Item> iterator() {
|
||||||
return null;
|
return new Iterator<Item>() {
|
||||||
|
int current = head;
|
||||||
|
@Override
|
||||||
|
public boolean hasNext() {
|
||||||
|
return current != tail;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Item next() {
|
||||||
|
return arr[current++];
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,32 +1,109 @@
|
|||||||
package com.hithomelabs.princeton1.module4;
|
package com.hithomelabs.princeton1.module4;
|
||||||
import java.util.Iterator;
|
|
||||||
import javax.annotation.Nonnull;
|
|
||||||
|
|
||||||
// Concrete implementation of stack using arrays
|
import java.util.Iterator;
|
||||||
// Creating a generic stack of type E
|
|
||||||
public class ArrayStack<E> extends Stack<E> {
|
public class ArrayStack<Item> extends Stack<Item>{
|
||||||
|
|
||||||
|
private int capacity=3;
|
||||||
|
private int top=0;
|
||||||
|
private Item[] arr = (Item[]) new Object[capacity];
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isEmpty() {
|
public boolean isEmpty() {
|
||||||
return false;
|
return top == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void resize(int newCapacity){
|
||||||
|
Item[] temp = arr;
|
||||||
|
arr = (Item[]) new Object[newCapacity];
|
||||||
|
|
||||||
|
for(int i=0; i<top; i++){
|
||||||
|
arr[i]=temp[i];
|
||||||
|
}
|
||||||
|
capacity=newCapacity;
|
||||||
|
// System.out.println("New Capacity: "+capacity);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void push(E element) {
|
public void push(Item element) {
|
||||||
|
if(top == capacity)
|
||||||
|
resize(capacity * 2);
|
||||||
|
arr[top++] = element;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public E pop() {
|
public Item pop() {
|
||||||
|
|
||||||
|
//System.out.println(top);
|
||||||
|
|
||||||
|
if(top == 0)
|
||||||
|
//throw new java.util.NoSuchElementException();
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
|
Item element = arr[--top];
|
||||||
|
arr[top] = null;
|
||||||
|
|
||||||
|
if(top < capacity/4)
|
||||||
|
resize(capacity/2);
|
||||||
|
return element;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int size() {
|
public int size() {
|
||||||
return 0;
|
return top;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Iterator<E> iterator() {
|
public Iterator<Item> iterator() {
|
||||||
return null;
|
return new Iterator<Item>() {
|
||||||
|
private int current=0;
|
||||||
|
@Override
|
||||||
|
public boolean hasNext() {
|
||||||
|
return current<top;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Item next() {
|
||||||
|
return arr[current++];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args){
|
||||||
|
Stack<Integer> s = new ArrayStack<>();
|
||||||
|
|
||||||
|
try{
|
||||||
|
s.push(0);
|
||||||
|
s.push(1);
|
||||||
|
s.push(2);
|
||||||
|
s.push(3);
|
||||||
|
s.push(4);
|
||||||
|
s.push(5);
|
||||||
|
s.push(6);
|
||||||
|
s.push(7);
|
||||||
|
|
||||||
|
for(Integer i: s)
|
||||||
|
System.out.println(i);
|
||||||
|
|
||||||
|
s.pop();
|
||||||
|
s.pop();
|
||||||
|
s.pop();
|
||||||
|
s.pop();
|
||||||
|
s.pop();
|
||||||
|
s.pop();
|
||||||
|
s.pop();
|
||||||
|
s.pop();
|
||||||
|
// s.pop();
|
||||||
|
|
||||||
|
for(Integer i: s)
|
||||||
|
System.out.println(i);
|
||||||
|
|
||||||
|
} catch (java.util.NoSuchElementException e) {
|
||||||
|
System.out.println("Cannot pop an empty stack");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,136 @@
|
|||||||
|
package com.hithomelabs.princeton1.module4;
|
||||||
|
|
||||||
|
|
||||||
|
import java.util.Iterator;
|
||||||
|
|
||||||
|
public class Deque<Item> implements Iterable<Item> {
|
||||||
|
|
||||||
|
private Node head;
|
||||||
|
private Node tail;
|
||||||
|
private int capacity;
|
||||||
|
|
||||||
|
private class Node{
|
||||||
|
Item value;
|
||||||
|
Node next;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Deque(){
|
||||||
|
this.head = null;
|
||||||
|
this.tail = null;
|
||||||
|
this.capacity = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isEmpty(){
|
||||||
|
return this.capacity == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int size(){
|
||||||
|
return this.capacity;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void addFirst(Item item){
|
||||||
|
Node temp=this.head;
|
||||||
|
this.head=new Node();
|
||||||
|
this.head.value = item;
|
||||||
|
this.head.next = temp;
|
||||||
|
// System.out.println(this.isEmpty());
|
||||||
|
if(this.isEmpty())
|
||||||
|
this.tail=this.head;
|
||||||
|
this.capacity++;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addLast(Item item){
|
||||||
|
Node temp = new Node();
|
||||||
|
temp.value = item;
|
||||||
|
System.out.println(this.isEmpty());
|
||||||
|
if(!this.isEmpty()) {
|
||||||
|
this.tail.next = temp;
|
||||||
|
this.tail = this.tail.next;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
this.head = temp;
|
||||||
|
this.tail = temp;
|
||||||
|
}
|
||||||
|
this.capacity++;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Item removeFirst(){
|
||||||
|
Item item = this.head.value;
|
||||||
|
this.head=this.head.next;
|
||||||
|
this.capacity--;
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Item removeLast(){
|
||||||
|
Item item = this.tail.value;
|
||||||
|
Node temp = this.head;
|
||||||
|
|
||||||
|
if(capacity <= 1){
|
||||||
|
this.tail=null;
|
||||||
|
this.head=null;
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
while(temp.next.next != null){
|
||||||
|
temp=temp.next;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.tail=temp;
|
||||||
|
this.tail.next=null;
|
||||||
|
this.capacity--;
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Iterator<Item> iterator() {
|
||||||
|
|
||||||
|
return new Iterator<Item>() {
|
||||||
|
private Node current=head;
|
||||||
|
@Override
|
||||||
|
public boolean hasNext() {
|
||||||
|
return this.current != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Item next() {
|
||||||
|
Item item = this.current.value;
|
||||||
|
this.current=this.current.next;
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args){
|
||||||
|
Deque<String> deque = new Deque<String>();
|
||||||
|
|
||||||
|
deque.addLast("qwe");
|
||||||
|
deque.addFirst("asd");
|
||||||
|
deque.addLast("zxc");
|
||||||
|
|
||||||
|
for(String s: deque){
|
||||||
|
System.out.println(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
deque.addLast("qwess");
|
||||||
|
|
||||||
|
System.out.println();
|
||||||
|
|
||||||
|
for(String s: deque){
|
||||||
|
System.out.println(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
deque.removeFirst();
|
||||||
|
deque.removeLast();
|
||||||
|
deque.removeFirst();
|
||||||
|
deque.removeLast();
|
||||||
|
|
||||||
|
System.out.println();
|
||||||
|
|
||||||
|
for(String s: deque){
|
||||||
|
System.out.println(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -1,31 +1,103 @@
|
|||||||
package com.hithomelabs.princeton1.module4;
|
package com.hithomelabs.princeton1.module4;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
|
||||||
public class LinkedQueue<E> extends Queue<E>{
|
public class LinkedQueue<Item> extends Queue<Item>{
|
||||||
|
private Node head = null, tail = null;
|
||||||
|
private int size = 0;
|
||||||
|
|
||||||
|
private class Node{
|
||||||
|
Item value;
|
||||||
|
Node next;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isEmpty() {
|
public boolean isEmpty() {
|
||||||
return false;
|
return size == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public E dequeue() {
|
public Item dequeue() {
|
||||||
|
if(size == 0)
|
||||||
|
// throw new java.util.NoSuchElementException();
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
|
Item element = head.value;
|
||||||
|
head = head.next;
|
||||||
|
size--;
|
||||||
|
if(isEmpty())
|
||||||
|
tail=head;
|
||||||
|
return element;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void enqueue(E element) {
|
public void enqueue(Item element) {
|
||||||
|
Node newNode = new Node();
|
||||||
|
newNode.value = element;
|
||||||
|
newNode.next = null;
|
||||||
|
if(isEmpty()){
|
||||||
|
tail=newNode;
|
||||||
|
head=tail;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
tail.next = newNode;
|
||||||
|
tail = tail.next;
|
||||||
|
}
|
||||||
|
size++;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int size() {
|
public int size() {
|
||||||
return 0;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Iterator<E> iterator() {
|
public Iterator<Item> iterator() {
|
||||||
return null;
|
return new Iterator<Item>() {
|
||||||
|
Node current = head;
|
||||||
|
@Override
|
||||||
|
public boolean hasNext() {
|
||||||
|
return current != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Item next() {
|
||||||
|
Item element = current.value;
|
||||||
|
current = current.next;
|
||||||
|
return element;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args){
|
||||||
|
Queue<Integer> q = new LinkedQueue<>();
|
||||||
|
|
||||||
|
try{
|
||||||
|
System.out.println("Queue empty?: " + q.isEmpty());
|
||||||
|
q.enqueue(1);
|
||||||
|
q.enqueue(2);
|
||||||
|
q.enqueue(3);
|
||||||
|
q.enqueue(4);
|
||||||
|
|
||||||
|
for(int value: q)
|
||||||
|
System.out.println(value);
|
||||||
|
|
||||||
|
System.out.println("Queue empty?: " + q.isEmpty());
|
||||||
|
System.out.println("Queue Size?: " + q.size());
|
||||||
|
|
||||||
|
q.dequeue();
|
||||||
|
q.dequeue();
|
||||||
|
// q.dequeue();
|
||||||
|
// q.dequeue();
|
||||||
|
// q.dequeue();
|
||||||
|
|
||||||
|
for(int value: q)
|
||||||
|
System.out.println(value);
|
||||||
|
|
||||||
|
} catch (java.util.NoSuchElementException e) {
|
||||||
|
System.out.println("Cannot dequeue empty queue");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,30 +1,86 @@
|
|||||||
package com.hithomelabs.princeton1.module4;
|
package com.hithomelabs.princeton1.module4;
|
||||||
|
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
|
||||||
// Creating a concrete linked Implementation of Stack
|
public class LinkedStack<Item> extends Stack<Item> {
|
||||||
public class LinkedStack<E> extends Stack<E>{
|
|
||||||
|
private Node top = null;
|
||||||
|
private int size = 0;
|
||||||
|
|
||||||
|
private class Node{
|
||||||
|
Item value;
|
||||||
|
Node next;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isEmpty() {
|
public boolean isEmpty() {
|
||||||
return false;
|
return size == 0 ;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void push(E element) {
|
public void push(Item element) {
|
||||||
|
Node newNode = new Node();
|
||||||
|
newNode.value = element;
|
||||||
|
newNode.next = top;
|
||||||
|
top = newNode;
|
||||||
|
// System.out.println(top.value);
|
||||||
|
size++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public E pop() {
|
public Item pop() {
|
||||||
return null;
|
if(size == 0){
|
||||||
|
throw new java.util.NoSuchElementException();
|
||||||
|
}
|
||||||
|
Item element = top.value;
|
||||||
|
top=top.next;
|
||||||
|
size--;
|
||||||
|
|
||||||
|
return element;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int size() {
|
public int size() {
|
||||||
return 0;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Iterator<E> iterator() {
|
public Iterator<Item> iterator() {
|
||||||
return null;
|
return new Iterator<Item>() {
|
||||||
|
private Node current = top;
|
||||||
|
@Override
|
||||||
|
public boolean hasNext() {
|
||||||
|
return current != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Item next() {
|
||||||
|
Item element = current.value;
|
||||||
|
current=current.next;
|
||||||
|
return element;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args){
|
||||||
|
Stack <String> s = new LinkedStack<>();
|
||||||
|
|
||||||
|
try {
|
||||||
|
System.out.println("String empty?: " + s.isEmpty());
|
||||||
|
// s.pop();
|
||||||
|
s.push("12");
|
||||||
|
s.push("13");
|
||||||
|
s.pop();
|
||||||
|
s.push("14");
|
||||||
|
System.out.println("String empty?: " + s.isEmpty());
|
||||||
|
System.out.println("String size?: " + s.size());
|
||||||
|
|
||||||
|
for(String value: s){
|
||||||
|
System.out.print(value+" ");
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.out.println("Cannot pop an empty stack");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,19 +0,0 @@
|
|||||||
package com.hithomelabs.princeton1.module5;
|
|
||||||
|
|
||||||
public abstract class AbstractCustomSorts<E> {
|
|
||||||
|
|
||||||
public abstract void sort(E[] arr);
|
|
||||||
|
|
||||||
// TODO: Implement this method
|
|
||||||
public void exch(E[] arr, int j, int i) {
|
|
||||||
E temp = arr[j];
|
|
||||||
arr[j] = arr[i];
|
|
||||||
arr[i] = temp;
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Implement this method
|
|
||||||
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,31 +1,46 @@
|
|||||||
package com.hithomelabs.princeton1.module5;
|
package com.hithomelabs.princeton1.module5;
|
||||||
|
|
||||||
public class Insertion<E> extends AbstractCustomSorts<E> {
|
import java.util.Comparator;
|
||||||
@Override
|
|
||||||
|
public class Insertion<E> implements MeasurableSort<E>, MeasurableHelper{
|
||||||
|
|
||||||
public void sort(E[] arr) {
|
public void sort(E[] arr) {
|
||||||
|
coreSortLogic(arr, null, null);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void coreSortLogic(E[] arr, Comparator<E> cmp, SortingMetaData metaData) {
|
||||||
|
if (arr == null) return;
|
||||||
|
else {
|
||||||
int N = arr.length;
|
int N = arr.length;
|
||||||
|
// * * swap arr[i] with each element greater to it's left
|
||||||
for(int i=0; i<N; i++){
|
for (int i = 1; i < N; i++) {
|
||||||
for(int j=i; j>0; j--){
|
int j = i;
|
||||||
if(less( (Comparable<E>) arr[j], arr[j-1]))
|
while (j >= 1 && MeasurableHelper.less(arr[j], arr[j - 1], cmp, metaData)) {
|
||||||
exch(arr, j, j-1);
|
MeasurableHelper.exch(arr, j, j - 1, metaData);
|
||||||
else
|
j = j - 1;
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void sort(E[] arr, int h) {
|
|
||||||
int N = arr.length;
|
|
||||||
|
|
||||||
for(int i=0; i<N; i++){
|
|
||||||
for(int j=i; j>0; j=j-h){
|
|
||||||
if(less( (Comparable<E>) arr[j], arr[j-1]))
|
|
||||||
exch(arr, j, j-1);
|
|
||||||
else
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@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);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Insertion Sort";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -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,22 +1,47 @@
|
|||||||
package com.hithomelabs.princeton1.module5;
|
package com.hithomelabs.princeton1.module5;
|
||||||
|
|
||||||
|
|
||||||
|
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
|
* * 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]
|
* * Then proceeds to do the same swapping arr[i] with arr[i:arr.length-1]
|
||||||
*/
|
*/
|
||||||
public class Selection<E> extends AbstractCustomSorts<E>{
|
|
||||||
@Override
|
|
||||||
public void sort(E[] arr){
|
public void sort(E[] arr){
|
||||||
int N=arr.length;
|
coreSortLogic(arr, null, null);
|
||||||
|
}
|
||||||
|
|
||||||
for(int i=0; i<N; i++){
|
private void coreSortLogic(E[] arr, Comparator<E> cmp, SortingMetaData metaData){
|
||||||
int min=i;
|
if (arr == null) return;
|
||||||
for(int j=i+1; j<N; j++){
|
Comparable<E>[] arr1 = (Comparable<E>[]) arr;
|
||||||
if(less((Comparable<E>) arr[j], arr[min]))
|
for(int i = 0; i < arr1.length - 1; i++){
|
||||||
min=j;
|
int minIndex = i;
|
||||||
}
|
for(int j = i+1; j < arr.length; j ++){
|
||||||
exch(arr, min, i);
|
if (MeasurableHelper.less(arr[j], arr[minIndex], cmp, metaData)) minIndex = j;
|
||||||
}
|
}
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Selection Sort";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,13 +1,9 @@
|
|||||||
package com.hithomelabs.princeton1.module5;
|
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
|
public class Shell<E> implements MeasurableSort<E>, MeasurableHelper {
|
||||||
private class MetaData{
|
|
||||||
|
|
||||||
int compares;
|
|
||||||
int swaps;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* * We will be performing h sort
|
* * We will be performing h sort
|
||||||
@ -24,31 +20,58 @@ public class Shell<E> extends AbstractCustomSorts<E> {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void sort(E[] arr) {
|
public void sort(E[] arr) {
|
||||||
Insertion<E> sortingAlgorithm = new Insertion<>();
|
coreSortLogic(arr,null,null);
|
||||||
|
}
|
||||||
|
|
||||||
int h=1, N=arr.length;
|
private void coreSortLogic(E[] arr, Comparator<E> cmp, SortingMetaData metaData){
|
||||||
while(h < N/3)
|
int N = arr.length;
|
||||||
|
int h = 1;
|
||||||
|
// * * Calculates the largest value of h greater than n
|
||||||
|
while (3 * h + 1 < N) {
|
||||||
h = 3 * h + 1;
|
h = 3 * h + 1;
|
||||||
|
}
|
||||||
while (h >= 1) {
|
while (h >= 1) {
|
||||||
sortingAlgorithm.sort(arr, h);
|
h = hsort(arr, h, cmp, metaData);
|
||||||
h = h / 3;
|
h = h / 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private int hsort(E[] arr, int h, MetaData metadata) {
|
private int hsort(E[] arr, int h, Comparator<E> cmp, SortingMetaData metaData) {
|
||||||
return 0;
|
int N = arr.length;
|
||||||
|
for(int i = h; i < N; i++){
|
||||||
|
int j = i;
|
||||||
|
while(j >= h && MeasurableHelper.less(arr[j], arr[j-h], cmp, metaData)){
|
||||||
|
MeasurableHelper.exch(arr, j, j-h, metaData);
|
||||||
|
j = j - h;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return h;
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
* Sample implementation of insertion sort as h-sort of h = 1
|
* Sample implementation of insertion sort as h-sort of h = 1
|
||||||
* Will just be comparing the number of saps taken across both implementations
|
* Will just be comparing the number of saps taken across both implementations
|
||||||
*/
|
*/
|
||||||
public void insertionSort(E[] arr){
|
public void insertionSort(E[] arr){
|
||||||
MetaData metaData = new MetaData();
|
|
||||||
int h = 1;
|
int h = 1;
|
||||||
h = hsort(arr, h, metaData);
|
h = hsort(arr, h, null, null);
|
||||||
System.out.println("Array sorted (insertion sort) with " + metaData.compares + " compares and " + metaData.swaps + " swaps");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@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);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Shell Sort";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,41 @@
|
|||||||
|
package com.hithomelabs.princeton1.module5;
|
||||||
|
|
||||||
|
public class SortingMetaData {
|
||||||
|
|
||||||
|
private double tick;
|
||||||
|
private double tok;
|
||||||
|
private double exchanges;
|
||||||
|
private double 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
@ -6,91 +6,51 @@ import org.junit.jupiter.api.DisplayName;
|
|||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import com.hithomelabs.princeton1.common.Apple;
|
import com.hithomelabs.princeton1.common.Apple;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
||||||
|
|
||||||
class SortTest {
|
class SortTest {
|
||||||
|
|
||||||
private Apple[] apples, applesCopy;
|
private ArrayList<Apple> apples;
|
||||||
private AbstractCustomSorts<Apple> sortingAlgorithm;
|
private ComparableSort<Apple> sortingAlgorithm;
|
||||||
private Random random;
|
private Random random;
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void setUp() {
|
void setUp() {
|
||||||
apples = new Apple[100];
|
apples = new ArrayList<Apple>();
|
||||||
// applesCopy = new Apple[100];
|
|
||||||
//sortingAlgorithm = new Selection<Apple>();
|
//sortingAlgorithm = new Selection<Apple>();
|
||||||
random = new Random();
|
random = new Random();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void testSort() {
|
private void testSort(ComparableSort<Apple> sortingAlgorithm) {
|
||||||
int value;
|
for (int i = 0; i < 100; i++)
|
||||||
for (int i = 0; i < 100; i++) {
|
apples.add(new Apple(random.nextInt(100)));
|
||||||
value = random.nextInt(100);
|
|
||||||
apples[i] = new Apple(value);
|
|
||||||
// applesCopy[i] = new Apple(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean test(){
|
|
||||||
for(int i=0;i<99;i++){
|
|
||||||
if(apples[i].compareTo(apples[i+1]) > 0)
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Testing Insertion sort functionality")
|
@DisplayName("Testing Insertion sort functionality")
|
||||||
public void testInsertionSort() {
|
public void testInsertionSort() {
|
||||||
sortingAlgorithm = new Insertion<Apple>();
|
sortingAlgorithm = new Insertion<Apple>();
|
||||||
//populates random apples
|
testSort(sortingAlgorithm);
|
||||||
testSort();
|
|
||||||
|
|
||||||
//calling sort function
|
|
||||||
sortingAlgorithm.sort(apples);
|
|
||||||
|
|
||||||
//checking if sorted array has any element which is less than preceeding one
|
|
||||||
assertTrue(test());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Testing Selection sort functionality")
|
@DisplayName("Testing Selection sort functionality")
|
||||||
public void testSelectionSort() {
|
public void testSelectionSort() {
|
||||||
sortingAlgorithm = new Selection<Apple>();
|
sortingAlgorithm = new Selection<Apple>();
|
||||||
//populates random apples
|
testSort(sortingAlgorithm);
|
||||||
testSort();
|
|
||||||
|
|
||||||
//calling sort function
|
|
||||||
sortingAlgorithm.sort(apples);
|
|
||||||
|
|
||||||
//checking if sorted array has any element which is less than preceeding one
|
|
||||||
assertTrue(test());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Testing Shell sort functionality")
|
@DisplayName("Testing Shell sort functionality")
|
||||||
public void testShellSort() {
|
public void testShellSort() {
|
||||||
sortingAlgorithm = new Shell<Apple>();
|
sortingAlgorithm = new Shell<>();
|
||||||
//populates random apples
|
testSort(sortingAlgorithm);
|
||||||
testSort();
|
|
||||||
|
|
||||||
//calling sort function
|
|
||||||
sortingAlgorithm.sort(apples);
|
|
||||||
|
|
||||||
//checking if sorted array has any element which is less than preceeding one
|
|
||||||
assertTrue(test());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@AfterEach
|
@AfterEach
|
||||||
void tearDown() {
|
void tearDown() {
|
||||||
sortingAlgorithm = null;
|
sortingAlgorithm = null;
|
||||||
apples = null;
|
apples = null;
|
||||||
applesCopy = null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,63 +1,79 @@
|
|||||||
package com.hithomelabs.princeton1.module6;
|
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.Random;
|
import java.util.Arrays;
|
||||||
|
import java.util.Comparator;
|
||||||
|
|
||||||
|
public class Merge<E> implements MeasurableSort<E>, MeasurableHelper {
|
||||||
public class Merge<E> extends AbstractCustomSorts<E> {
|
|
||||||
@Override
|
@Override
|
||||||
public void sort(E[] arr) {
|
public void sort(E[] arr) {
|
||||||
E[] aux = (E[]) new Object[arr.length];
|
coreSortLogic(arr, null, null);
|
||||||
sort(arr, aux, 0, arr.length-1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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, cmp, metaData);
|
||||||
|
}
|
||||||
|
|
||||||
private void sort(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;
|
if (hi <= lo) return;
|
||||||
int mid = lo + (hi - lo) / 2;
|
int mid = lo + (hi - lo) / 2;
|
||||||
sort(arr,aux, lo, mid);
|
mergesort(arr, aux, lo, mid, cmp, metaData);
|
||||||
sort(arr,aux, mid+1,hi);
|
mergesort(arr, aux, mid + 1, hi, cmp, metaData);
|
||||||
merge(arr,aux,lo, mid, hi);
|
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];
|
||||||
|
|
||||||
for(int k=lo; k<=hi ;k++)
|
int i = lo;
|
||||||
aux[k] = arr[k];
|
int j = mid + 1;
|
||||||
|
|
||||||
int i=lo, j = mid+1;
|
|
||||||
|
|
||||||
for (int k = lo; k <= hi; k++) {
|
for (int k = lo; k <= hi; k++) {
|
||||||
if(i>mid) arr[k] = aux[j++];
|
// * If i has already reached mid, no need to compare we insert at pointer k
|
||||||
else if(j>hi) arr[k] = aux[i++];
|
if (i > mid) {
|
||||||
else if(less((Comparable<E>) aux[i], aux[j] )) arr[k] = aux[i++];
|
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++];
|
else arr[k] = aux[j++];
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// public static void main(String[] args){
|
@Override
|
||||||
// Integer[] arrTest = new Integer[6];
|
public void sort(E[] arr, Comparator<E> cmp, SortingMetaData metaData) {
|
||||||
//
|
if (metaData != null)
|
||||||
// for(int i=0;i<6;i++){
|
metaData.startTime();
|
||||||
// arrTest[i] = new Random().nextInt(100);
|
coreSortLogic(arr, cmp, metaData);
|
||||||
// System.out.println(arrTest[i]);
|
if (metaData != null)
|
||||||
// }
|
metaData.endTime();
|
||||||
//
|
}
|
||||||
// System.out.println();
|
|
||||||
//
|
|
||||||
// Merge<Integer> sortAlgo = new Merge<>();
|
|
||||||
//
|
|
||||||
// sortAlgo.sort(arrTest);
|
|
||||||
//
|
|
||||||
// for(Integer i: arrTest)
|
|
||||||
// System.out.println(i);
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void sort(E[] arr, Comparator<E> cmp) {
|
||||||
|
coreSortLogic(arr, cmp, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Merge Sort";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
package com.hithomelabs.princeton1.module6;
|
package com.hithomelabs.princeton1.module6;
|
||||||
|
|
||||||
import com.hithomelabs.princeton1.module5.AbstractCustomSorts;
|
import com.hithomelabs.princeton1.module5.ComparableSort;
|
||||||
import com.hithomelabs.princeton1.common.Apple;
|
import com.hithomelabs.princeton1.common.Apple;
|
||||||
import org.junit.jupiter.api.AfterEach;
|
import org.junit.jupiter.api.AfterEach;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
@ -14,7 +14,7 @@ import static org.junit.jupiter.api.Assertions.*;
|
|||||||
|
|
||||||
class MergeTest {
|
class MergeTest {
|
||||||
|
|
||||||
private AbstractCustomSorts<Apple> sortingAlgorithm;
|
private ComparableSort<Apple> sortingAlgorithm;
|
||||||
private ArrayList<Apple> apples;
|
private ArrayList<Apple> apples;
|
||||||
private Random random;
|
private Random random;
|
||||||
|
|
||||||
|
@ -1,58 +1,79 @@
|
|||||||
package com.hithomelabs.princeton1.module7;
|
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.Arrays;
|
import java.util.Comparator;
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.Random;
|
|
||||||
|
|
||||||
|
|
||||||
public class Quick<E> extends AbstractCustomSorts<E> {
|
public class Quick<E> implements MeasurableSort<E>, MeasurableHelper {
|
||||||
@Override
|
@Override
|
||||||
public void sort(E[] arr) {
|
public void sort(E[] arr) {
|
||||||
// Collections.shuffle(Arrays.asList(arr));
|
int N = arr.length;
|
||||||
sort(arr, 0, arr.length-1);
|
quickSort(arr, 0, N - 1, null, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void sort(E[] arr, int lo, int hi){
|
public void altSort(E[] arr) {
|
||||||
|
int N = arr.length;
|
||||||
|
altQuickSort(arr, 0, N-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void altQuickSort(E[] arr, int lo, int hi) {
|
||||||
if (lo >= hi) return;
|
if (lo >= hi) return;
|
||||||
int pivot = partition(arr, lo, hi);
|
int i = lo + 1;
|
||||||
sort(arr, lo, pivot-1);
|
int j = i;
|
||||||
sort(arr, pivot+1,hi);
|
while(j <= hi){
|
||||||
|
if(MeasurableHelper.less((Comparable<E>) arr[j], arr[lo], null, null)){
|
||||||
|
MeasurableHelper.exch(arr, i, j, null);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
MeasurableHelper.exch(arr, i-1, lo,null);
|
||||||
|
altQuickSort(arr, lo, i-2);
|
||||||
|
altQuickSort(arr, i, hi);
|
||||||
}
|
}
|
||||||
|
|
||||||
private int partition(E[] arr, int lo, int hi){
|
private void quickSort(E[] arr, int lo, int hi, Comparator<E> cmp, SortingMetaData metaData) {
|
||||||
int pivot=lo, i=lo, j=hi+1;
|
|
||||||
|
|
||||||
while(i<j){
|
if (lo >= hi) return;
|
||||||
while(less((Comparable<E>) arr[++i], arr[pivot]))
|
int i = lo;
|
||||||
|
int j = hi+1;
|
||||||
|
while(true){
|
||||||
|
while(MeasurableHelper.less( arr[++i], arr[lo], cmp, metaData)){
|
||||||
if(i == hi) break;
|
if(i == hi) break;
|
||||||
|
}
|
||||||
while(less((Comparable<E>) arr[pivot], arr[--j]))
|
while(!MeasurableHelper.less(arr[--j], arr[lo], cmp, metaData)){
|
||||||
if (j == lo ) break;
|
if (j == lo ) break;
|
||||||
|
|
||||||
if(i>=j) break;
|
|
||||||
exch(arr,i, j);
|
|
||||||
}
|
}
|
||||||
exch(arr,pivot,j);
|
if(j<=i) break;
|
||||||
return j;
|
MeasurableHelper.exch(arr, i , j, metaData);
|
||||||
|
}
|
||||||
|
MeasurableHelper.exch(arr, j, lo, metaData);
|
||||||
|
quickSort(arr, lo, j-1, cmp, metaData);
|
||||||
|
quickSort(arr, j+1, hi, cmp, metaData);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// public static void main(String[] args){
|
@Override
|
||||||
// Integer[] arrTest = new Integer[6];
|
public void sort(E[] arr, Comparator<E> cmp) {
|
||||||
//
|
int N = arr.length;
|
||||||
// for(int i=0;i<6;i++){
|
quickSort(arr, 0, N - 1, cmp, null);
|
||||||
// arrTest[i] = new Random().nextInt(100);
|
}
|
||||||
// System.out.println(arrTest[i]);
|
|
||||||
// }
|
public void sort(E[] arr, Comparator<E> cmp, SortingMetaData metaData) {
|
||||||
//
|
if(metaData != null){
|
||||||
// System.out.println();
|
metaData.startTime();
|
||||||
//
|
}
|
||||||
// Quick<Integer> sortAlgo = new Quick<>();
|
int N = arr.length;
|
||||||
//
|
quickSort(arr, 0, N - 1, cmp, metaData);
|
||||||
// sortAlgo.sort(arrTest);
|
if (metaData != null)
|
||||||
//
|
metaData.endTime();
|
||||||
// for(Integer i: arrTest)
|
}
|
||||||
// System.out.println(i);
|
|
||||||
// }
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Quick Sort";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,58 @@
|
|||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Three Way Quick Sort";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -33,10 +33,8 @@ class QuickTest {
|
|||||||
Assertions.assertArrayEquals(sorted, arr);
|
Assertions.assertArrayEquals(sorted, arr);
|
||||||
}
|
}
|
||||||
|
|
||||||
// * * Optional test for alternate sort implmentation
|
|
||||||
/*
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("testing Quick sort default implementation")
|
@DisplayName("testing Quick sort alternate implementation")
|
||||||
public void testAltSort(){
|
public void testAltSort(){
|
||||||
|
|
||||||
for(int i = 0; i < 100; i++)
|
for(int i = 0; i < 100; i++)
|
||||||
@ -47,8 +45,18 @@ class QuickTest {
|
|||||||
Apple[] sorted = apples.toArray(new Apple[apples.size()]);
|
Apple[] sorted = apples.toArray(new Apple[apples.size()]);
|
||||||
Assertions.assertArrayEquals(sorted, arr);
|
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
|
@AfterEach
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -13,6 +13,7 @@ dependencies {
|
|||||||
testImplementation platform('org.junit:junit-bom:5.10.0')
|
testImplementation platform('org.junit:junit-bom:5.10.0')
|
||||||
testImplementation 'org.junit.jupiter:junit-jupiter'
|
testImplementation 'org.junit.jupiter:junit-jupiter'
|
||||||
implementation project(':module5')
|
implementation project(':module5')
|
||||||
|
implementation project(':module4')
|
||||||
testImplementation project(':common')
|
testImplementation project(':common')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,30 +5,39 @@ 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
|
* * 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
|
* * Sink Node T with to it's appropriate place in a Heap of size N, if it's children exist and are greater
|
||||||
* * Implement sink API to sink a node if it's sub-heap is not heap-order
|
|
||||||
*/
|
*/
|
||||||
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 (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
|
// * * 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> 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
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,27 +1,78 @@
|
|||||||
package com.hithomelabs.princeton1.module8;
|
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, 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
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--)
|
||||||
|
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
|
@Override
|
||||||
public void sort(E[] arr) {
|
public void sort(E[] arr) {
|
||||||
int N = arr.length;
|
int N = arr.length;
|
||||||
|
|
||||||
E[] heapArr = (E[]) new Object[N+1];
|
E[] heapArr = (E[]) new Object[N+1];
|
||||||
// * * to simplify we copy original array and write it to the new array starting index 1
|
// * * to simplify we copy original array from
|
||||||
System.arraycopy(arr, 0, heapArr, 1, N);
|
System.arraycopy(arr, 0, heapArr, 1, N);
|
||||||
|
|
||||||
// * * An array of size N holds a heap of size N-1
|
// * * An array of size N holds a heap of size N-1
|
||||||
coreSortingLogic(heapArr, N);
|
coreSortingLogic(heapArr, N,null, null);
|
||||||
// * * Re-copying the sorted array to the original
|
|
||||||
System.arraycopy(heapArr, 1, arr, 0, N);
|
System.arraycopy(heapArr, 1, arr, 0, N);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
@Override
|
||||||
* * Implement the core sorting logic
|
public String toString() {
|
||||||
* * P.S the provision of making the index 0 null for ease of use has already been done above
|
return "Heap Sort";
|
||||||
*/
|
|
||||||
private void coreSortingLogic(E[] arr, int N) {
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,66 @@
|
|||||||
|
package com.hithomelabs.princeton1.module8;
|
||||||
|
|
||||||
|
import com.hithomelabs.princeton1.module4.Queue;
|
||||||
|
import com.hithomelabs.princeton1.module5.ComparableHelper;
|
||||||
|
|
||||||
|
import java.util.Iterator;
|
||||||
|
|
||||||
|
public class PriorityQueue<E> extends Queue<E> implements ComparableHelper {
|
||||||
|
private int size;
|
||||||
|
private E[] arr;
|
||||||
|
private static final int CAPACITY = 100;
|
||||||
|
private int capacity;
|
||||||
|
|
||||||
|
public PriorityQueue(){
|
||||||
|
this(CAPACITY);
|
||||||
|
}
|
||||||
|
|
||||||
|
public PriorityQueue(int capacity){
|
||||||
|
arr = (E[]) new Object[capacity+1];
|
||||||
|
this.capacity = capacity;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isEmpty() {
|
||||||
|
return size == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public E dequeue() {
|
||||||
|
if(isEmpty())
|
||||||
|
return null;
|
||||||
|
E value = arr[1];
|
||||||
|
ComparableHelper.exch(arr, 1, size);
|
||||||
|
arr[size--] = null;
|
||||||
|
Heap.sink(arr, 1, size, null, null);
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void enqueue(E element) {
|
||||||
|
arr[++size] = element;
|
||||||
|
Heap.swim(arr, size, size, null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int size() {
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Iterator iterator() {
|
||||||
|
return new Iterator() {
|
||||||
|
|
||||||
|
int index = 1;
|
||||||
|
@Override
|
||||||
|
public boolean hasNext() {
|
||||||
|
return index <= size;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object next() {
|
||||||
|
return arr[index++];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
@ -33,6 +33,18 @@ class HeapSortTest {
|
|||||||
Assertions.assertArrayEquals(sorted, arr);
|
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
|
@Test
|
||||||
@DisplayName("Sort 100 apples in descending order")
|
@DisplayName("Sort 100 apples in descending order")
|
||||||
public void testSortBorder(){
|
public void testSortBorder(){
|
||||||
|
Loading…
Reference in New Issue
Block a user