forked from Hithomelabs/Princeton1
Compare commits
7 Commits
c425ddb53e
...
b8da0cb3b1
Author | SHA1 | Date | |
---|---|---|---|
b8da0cb3b1 | |||
5a73f4ffda | |||
d091dc4c5d | |||
9ab321b545 | |||
d6d3111f51 | |||
05db13b2ee | |||
90f8b14ee5 |
2
.idea/gradle.xml
generated
2
.idea/gradle.xml
generated
@ -9,8 +9,10 @@
|
||||
<set>
|
||||
<option value="$PROJECT_DIR$" />
|
||||
<option value="$PROJECT_DIR$/clients" />
|
||||
<option value="$PROJECT_DIR$/common" />
|
||||
<option value="$PROJECT_DIR$/module4" />
|
||||
<option value="$PROJECT_DIR$/module5" />
|
||||
<option value="$PROJECT_DIR$/module6" />
|
||||
</set>
|
||||
</option>
|
||||
</GradleProjectSettings>
|
||||
|
@ -14,6 +14,9 @@ dependencies {
|
||||
testImplementation 'org.junit.jupiter:junit-jupiter'
|
||||
implementation project(':module4')
|
||||
implementation project(':module5')
|
||||
implementation project(':module6')
|
||||
implementation project(':common')
|
||||
testImplementation project(':common')
|
||||
}
|
||||
java {
|
||||
toolchain {
|
||||
|
@ -1,8 +1,8 @@
|
||||
package com.hithomelabs.clients.module5;
|
||||
|
||||
import com.hithomelabs.princeton1.module5.Insertion;
|
||||
import com.hithomelabs.princeton1.module5.Apple;
|
||||
import com.hithomelabs.princeton1.module5.Orange;
|
||||
import com.hithomelabs.princeton1.common.Apple;
|
||||
import com.hithomelabs.princeton1.common.Orange;
|
||||
|
||||
public class InsertionClient {
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
package com.hithomelabs.clients.module5;
|
||||
|
||||
import com.hithomelabs.princeton1.module5.Apple;
|
||||
import com.hithomelabs.princeton1.common.Apple;
|
||||
import com.hithomelabs.princeton1.module5.Shell;
|
||||
|
||||
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;
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.hithomelabs.princeton1.module5;
|
||||
package com.hithomelabs.princeton1.common;
|
||||
|
||||
public class Orange {
|
||||
private int size;
|
@ -12,6 +12,7 @@ repositories {
|
||||
dependencies {
|
||||
testImplementation platform('org.junit:junit-bom:5.10.0')
|
||||
testImplementation 'org.junit.jupiter:junit-jupiter'
|
||||
testImplementation project(':common')
|
||||
}
|
||||
java {
|
||||
toolchain {
|
||||
|
@ -49,7 +49,7 @@ public class Shell<E> extends AbstractCustomSorts<E> {
|
||||
}
|
||||
}
|
||||
/*
|
||||
! 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
|
||||
*/
|
||||
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.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import com.hithomelabs.princeton1.common.Apple;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Random;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class SortTest {
|
||||
|
||||
private ArrayList<Apple> apples;
|
||||
@ -26,11 +24,7 @@ class SortTest {
|
||||
private void testSort(AbstractCustomSorts<Apple> sortingAlgorithm) {
|
||||
for (int i = 0; i < 100; 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);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -54,7 +48,6 @@ class SortTest {
|
||||
testSort(sortingAlgorithm);
|
||||
}
|
||||
|
||||
|
||||
@AfterEach
|
||||
void tearDown() {
|
||||
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) {
|
||||
merge_sort(arr, 0, arr.length-1);
|
||||
}
|
||||
|
||||
private void merge_sort(E[] arr, int low_index, int high_index){
|
||||
if(high_index - low_index > 0) {
|
||||
int mid_index = low_index + (high_index - low_index)/2;
|
||||
merge_sort(arr, low_index, mid_index);
|
||||
merge_sort(arr, mid_index + 1, high_index);
|
||||
|
||||
// Copy array
|
||||
E[] auxArr = (E[]) new Object[high_index - low_index + 1];
|
||||
int index = 0;
|
||||
for(int i=low_index; i<= high_index; i++)
|
||||
auxArr[index++] = arr[i];
|
||||
|
||||
// Sort
|
||||
int low_index_ptr = 0;
|
||||
int mid_index_ptr = (auxArr.length - 1)/2;
|
||||
int high_index_ptr = mid_index_ptr + 1;
|
||||
if(!less((Comparable<E>) auxArr[mid_index_ptr], auxArr[high_index_ptr])) {
|
||||
for (int i = low_index; i <= high_index; i++) {
|
||||
if (low_index_ptr > mid_index_ptr) {
|
||||
arr[i] = auxArr[high_index_ptr];
|
||||
high_index_ptr++;
|
||||
} else if (high_index_ptr >= auxArr.length)
|
||||
arr[i] = auxArr[low_index_ptr++];
|
||||
else if (less((Comparable<E>) auxArr[low_index_ptr], auxArr[high_index_ptr]))
|
||||
arr[i] = auxArr[low_index_ptr++];
|
||||
else
|
||||
arr[i] = auxArr[high_index_ptr++];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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'
|
||||
include('module4')
|
||||
include 'module4'
|
||||
include 'clients'
|
||||
include 'module5'
|
||||
include 'module6'
|
||||
include 'common'
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user