forked from Hithomelabs/Princeton1
Mode commonly used objects to a new module common, added dependencies and test dependencies for other modules on common, added Merge sort, client and tests
This commit is contained in:
parent
178f0461ee
commit
90f8b14ee5
2
.idea/gradle.xml
generated
2
.idea/gradle.xml
generated
@ -9,8 +9,10 @@
|
|||||||
<set>
|
<set>
|
||||||
<option value="$PROJECT_DIR$" />
|
<option value="$PROJECT_DIR$" />
|
||||||
<option value="$PROJECT_DIR$/clients" />
|
<option value="$PROJECT_DIR$/clients" />
|
||||||
|
<option value="$PROJECT_DIR$/common" />
|
||||||
<option value="$PROJECT_DIR$/module4" />
|
<option value="$PROJECT_DIR$/module4" />
|
||||||
<option value="$PROJECT_DIR$/module5" />
|
<option value="$PROJECT_DIR$/module5" />
|
||||||
|
<option value="$PROJECT_DIR$/module6" />
|
||||||
</set>
|
</set>
|
||||||
</option>
|
</option>
|
||||||
</GradleProjectSettings>
|
</GradleProjectSettings>
|
||||||
|
@ -14,6 +14,9 @@ dependencies {
|
|||||||
testImplementation 'org.junit.jupiter:junit-jupiter'
|
testImplementation 'org.junit.jupiter:junit-jupiter'
|
||||||
implementation project(':module4')
|
implementation project(':module4')
|
||||||
implementation project(':module5')
|
implementation project(':module5')
|
||||||
|
implementation project(':module6')
|
||||||
|
implementation project(':common')
|
||||||
|
testImplementation project(':common')
|
||||||
}
|
}
|
||||||
java {
|
java {
|
||||||
toolchain {
|
toolchain {
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
package com.hithomelabs.clients.module5;
|
package com.hithomelabs.clients.module5;
|
||||||
|
|
||||||
import com.hithomelabs.princeton1.module5.Insertion;
|
import com.hithomelabs.princeton1.module5.Insertion;
|
||||||
import com.hithomelabs.princeton1.module5.Apple;
|
import com.hithomelabs.princeton1.common.Apple;
|
||||||
import com.hithomelabs.princeton1.module5.Orange;
|
import com.hithomelabs.princeton1.common.Orange;
|
||||||
|
|
||||||
public class InsertionClient {
|
public class InsertionClient {
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
package com.hithomelabs.clients.module5;
|
package com.hithomelabs.clients.module5;
|
||||||
|
|
||||||
import com.hithomelabs.princeton1.module5.Apple;
|
import com.hithomelabs.princeton1.common.Apple;
|
||||||
import com.hithomelabs.princeton1.module5.Shell;
|
import com.hithomelabs.princeton1.module5.Shell;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
@ -0,0 +1,23 @@
|
|||||||
|
package com.hithomelabs.clients.module6;
|
||||||
|
|
||||||
|
import com.hithomelabs.princeton1.common.Apple;
|
||||||
|
import com.hithomelabs.princeton1.module6.Merge;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
public class MergeClient {
|
||||||
|
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int size = 100;
|
||||||
|
Apple[] apples = new Apple[size];
|
||||||
|
Merge<Apple> merge = new Merge<Apple>();
|
||||||
|
|
||||||
|
for (int i = 0; i < apples.length; i++) {
|
||||||
|
apples[i] = new Apple(new Random().nextInt(1000));
|
||||||
|
}
|
||||||
|
merge.sort(apples);
|
||||||
|
for (int i = 0; i < apples.length; i++)
|
||||||
|
System.out.println(apples[i]);
|
||||||
|
}
|
||||||
|
}
|
24
common/build.gradle
Normal file
24
common/build.gradle
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
plugins {
|
||||||
|
id 'java'
|
||||||
|
}
|
||||||
|
|
||||||
|
group = 'com.hithomelabs.princeton1.common'
|
||||||
|
version = 'unspecified'
|
||||||
|
|
||||||
|
repositories {
|
||||||
|
mavenCentral()
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
testImplementation platform('org.junit:junit-bom:5.10.0')
|
||||||
|
testImplementation 'org.junit.jupiter:junit-jupiter'
|
||||||
|
}
|
||||||
|
|
||||||
|
java {
|
||||||
|
toolchain {
|
||||||
|
languageVersion = JavaLanguageVersion.of(11)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
test {
|
||||||
|
useJUnitPlatform()
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package com.hithomelabs.princeton1.module5;
|
package com.hithomelabs.princeton1.common;
|
||||||
|
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package com.hithomelabs.princeton1.module5;
|
package com.hithomelabs.princeton1.common;
|
||||||
|
|
||||||
public class Orange {
|
public class Orange {
|
||||||
private int size;
|
private int size;
|
@ -12,6 +12,7 @@ repositories {
|
|||||||
dependencies {
|
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'
|
||||||
|
testImplementation project(':common')
|
||||||
}
|
}
|
||||||
java {
|
java {
|
||||||
toolchain {
|
toolchain {
|
||||||
|
@ -53,7 +53,7 @@ public class Shell<E> extends AbstractCustomSorts<E> {
|
|||||||
return 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){
|
||||||
|
@ -4,12 +4,10 @@ import org.junit.jupiter.api.AfterEach;
|
|||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.DisplayName;
|
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 java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
|
||||||
|
|
||||||
class SortTest {
|
class SortTest {
|
||||||
|
|
||||||
private ArrayList<Apple> apples;
|
private ArrayList<Apple> apples;
|
||||||
@ -26,11 +24,7 @@ class SortTest {
|
|||||||
private void testSort(AbstractCustomSorts<Apple> sortingAlgorithm) {
|
private void testSort(AbstractCustomSorts<Apple> sortingAlgorithm) {
|
||||||
for (int i = 0; i < 100; i++)
|
for (int i = 0; i < 100; i++)
|
||||||
apples.add(new Apple(random.nextInt(100)));
|
apples.add(new Apple(random.nextInt(100)));
|
||||||
Apple[] arr = apples.toArray(new Apple[apples.size()]);
|
|
||||||
apples.sort(null);
|
|
||||||
Apple[] sorted = apples.toArray(new Apple[apples.size()]);
|
|
||||||
sortingAlgorithm.sort(arr);
|
|
||||||
assertArrayEquals(sorted, arr);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -54,7 +48,6 @@ class SortTest {
|
|||||||
testSort(sortingAlgorithm);
|
testSort(sortingAlgorithm);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@AfterEach
|
@AfterEach
|
||||||
void tearDown() {
|
void tearDown() {
|
||||||
sortingAlgorithm = null;
|
sortingAlgorithm = null;
|
||||||
|
25
module6/build.gradle
Normal file
25
module6/build.gradle
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
plugins {
|
||||||
|
id 'java'
|
||||||
|
}
|
||||||
|
|
||||||
|
group = 'com.hithomelabs.princeton1.module6'
|
||||||
|
version = 'unspecified'
|
||||||
|
|
||||||
|
repositories {
|
||||||
|
mavenCentral()
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
testImplementation platform('org.junit:junit-bom:5.10.0')
|
||||||
|
testImplementation 'org.junit.jupiter:junit-jupiter'
|
||||||
|
implementation project(':module5')
|
||||||
|
testImplementation project(':common')
|
||||||
|
}
|
||||||
|
java {
|
||||||
|
toolchain {
|
||||||
|
languageVersion = JavaLanguageVersion.of(11)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
test {
|
||||||
|
useJUnitPlatform()
|
||||||
|
}
|
@ -0,0 +1,46 @@
|
|||||||
|
package com.hithomelabs.princeton1.module6;
|
||||||
|
|
||||||
|
import com.hithomelabs.princeton1.module5.AbstractCustomSorts;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
public class Merge<E> extends AbstractCustomSorts<E> {
|
||||||
|
@Override
|
||||||
|
public void sort(E[] arr) {
|
||||||
|
|
||||||
|
int N = arr.length;
|
||||||
|
// * * aux is a helper array required for merge
|
||||||
|
E[] aux = Arrays.copyOf(arr, N);
|
||||||
|
mergesort(arr, aux, 0, N-1);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void mergesort(E[] arr, E[] aux, int lo, int hi) {
|
||||||
|
|
||||||
|
if (hi <= lo) return;
|
||||||
|
int mid = lo + (hi - lo)/2;
|
||||||
|
mergesort(arr, aux, lo, mid);
|
||||||
|
mergesort(arr, aux, mid+1, hi);
|
||||||
|
merge(arr, aux, lo, mid, hi);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void merge(E[] arr, E[] aux, int lo, int mid, int hi) {
|
||||||
|
|
||||||
|
// * * creating backup of original array
|
||||||
|
for (int i = lo; i <= hi; i++)
|
||||||
|
aux[i] = arr[i];
|
||||||
|
|
||||||
|
int i = lo;
|
||||||
|
int j = mid+1;
|
||||||
|
for (int k = lo; k <= hi; k++){
|
||||||
|
// * If i has already reached mid, no need to compare we insert at pointer k
|
||||||
|
if(i > mid) arr[k] = aux[j++];
|
||||||
|
else if(j > hi) arr[k] = aux[i++];
|
||||||
|
else if(less((Comparable<E>) aux[i], aux[j])) arr[k] = aux[i++];
|
||||||
|
else arr[k] = aux[j++];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -0,0 +1,44 @@
|
|||||||
|
package com.hithomelabs.princeton1.module6;
|
||||||
|
|
||||||
|
import com.hithomelabs.princeton1.module5.AbstractCustomSorts;
|
||||||
|
import com.hithomelabs.princeton1.common.Apple;
|
||||||
|
import org.junit.jupiter.api.AfterEach;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class MergeTest {
|
||||||
|
|
||||||
|
private AbstractCustomSorts<Apple> sortingAlgorithm;
|
||||||
|
private ArrayList<Apple> apples;
|
||||||
|
private Random random;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
apples = new ArrayList<>();
|
||||||
|
random = new Random();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Merge Sort test client")
|
||||||
|
public void testSort(){
|
||||||
|
sortingAlgorithm = new Merge<Apple>();
|
||||||
|
for(int i = 0; i < 10; i++)
|
||||||
|
apples.add(new Apple(random.nextInt(100)));
|
||||||
|
Apple[] arr = apples.toArray(new Apple[apples.size()]);
|
||||||
|
apples.sort(null);
|
||||||
|
Apple[] sorted = apples.toArray(new Apple[apples.size()]);
|
||||||
|
sortingAlgorithm.sort(arr);
|
||||||
|
assertArrayEquals(sorted, arr);
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterEach
|
||||||
|
void tearDown() {
|
||||||
|
sortingAlgorithm = null;
|
||||||
|
}
|
||||||
|
}
|
@ -11,7 +11,9 @@ plugins {
|
|||||||
}
|
}
|
||||||
|
|
||||||
rootProject.name = 'Pricenton1'
|
rootProject.name = 'Pricenton1'
|
||||||
include('module4')
|
include 'module4'
|
||||||
include 'clients'
|
include 'clients'
|
||||||
include 'module5'
|
include 'module5'
|
||||||
|
include 'module6'
|
||||||
|
include 'common'
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user