forked from Hithomelabs/Princeton1
Mod 5 all sorts
This commit is contained in:
parent
0f662bf89c
commit
f12dd3574e
1
.idea/gradle.xml
generated
1
.idea/gradle.xml
generated
@ -14,6 +14,7 @@
|
|||||||
<option value="$PROJECT_DIR$/module5" />
|
<option value="$PROJECT_DIR$/module5" />
|
||||||
<option value="$PROJECT_DIR$/module6" />
|
<option value="$PROJECT_DIR$/module6" />
|
||||||
<option value="$PROJECT_DIR$/module7" />
|
<option value="$PROJECT_DIR$/module7" />
|
||||||
|
<option value="$PROJECT_DIR$/module8" />
|
||||||
</set>
|
</set>
|
||||||
</option>
|
</option>
|
||||||
</GradleProjectSettings>
|
</GradleProjectSettings>
|
||||||
|
@ -6,6 +6,9 @@ public abstract class AbstractCustomSorts<E> {
|
|||||||
|
|
||||||
// TODO: Implement this method
|
// TODO: Implement this method
|
||||||
public void exch(E[] arr, int j, int i) {
|
public void exch(E[] arr, int j, int i) {
|
||||||
|
E temp = arr[j];
|
||||||
|
arr[j] = arr[i];
|
||||||
|
arr[i] = temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Implement this method
|
// TODO: Implement this method
|
||||||
|
@ -3,7 +3,29 @@ package com.hithomelabs.princeton1.module5;
|
|||||||
public class Insertion<E> extends AbstractCustomSorts<E> {
|
public class Insertion<E> extends AbstractCustomSorts<E> {
|
||||||
@Override
|
@Override
|
||||||
public void sort(E[] arr) {
|
public void sort(E[] arr) {
|
||||||
|
int N = arr.length;
|
||||||
|
|
||||||
|
for(int i=0; i<N; i++){
|
||||||
|
for(int j=i; j>0; j--){
|
||||||
|
if(less( (Comparable<E>) arr[j], arr[j-1]))
|
||||||
|
exch(arr, j, j-1);
|
||||||
|
else
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,7 +7,16 @@ package com.hithomelabs.princeton1.module5;
|
|||||||
public class Selection<E> extends AbstractCustomSorts<E>{
|
public class Selection<E> extends AbstractCustomSorts<E>{
|
||||||
@Override
|
@Override
|
||||||
public void sort(E[] arr) {
|
public void sort(E[] arr) {
|
||||||
|
int N=arr.length;
|
||||||
|
|
||||||
|
for(int i=0; i<N; i++){
|
||||||
|
int min=i;
|
||||||
|
for(int j=i+1; j<N; j++){
|
||||||
|
if(less((Comparable<E>) arr[j], arr[min]))
|
||||||
|
min=j;
|
||||||
|
}
|
||||||
|
exch(arr, min, i);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,6 +24,10 @@ public class Shell<E> extends AbstractCustomSorts<E> {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void sort(E[] arr) {
|
public void sort(E[] arr) {
|
||||||
|
Insertion<E> sortingAlgorithm = new Insertion<>();
|
||||||
|
sortingAlgorithm.sort(arr, 7);
|
||||||
|
sortingAlgorithm.sort(arr, 3);
|
||||||
|
sortingAlgorithm.sort(arr);
|
||||||
}
|
}
|
||||||
|
|
||||||
private int hsort(E[] arr, int h, MetaData metadata) {
|
private int hsort(E[] arr, int h, MetaData metadata) {
|
||||||
|
@ -6,51 +6,91 @@ 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 ArrayList<Apple> apples;
|
private Apple[] apples, applesCopy;
|
||||||
private AbstractCustomSorts<Apple> sortingAlgorithm;
|
private AbstractCustomSorts<Apple> sortingAlgorithm;
|
||||||
private Random random;
|
private Random random;
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void setUp() {
|
void setUp() {
|
||||||
apples = new ArrayList<Apple>();
|
apples = new Apple[100];
|
||||||
|
// applesCopy = new Apple[100];
|
||||||
//sortingAlgorithm = new Selection<Apple>();
|
//sortingAlgorithm = new Selection<Apple>();
|
||||||
random = new Random();
|
random = new Random();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void testSort(AbstractCustomSorts<Apple> sortingAlgorithm) {
|
private void testSort() {
|
||||||
for (int i = 0; i < 100; i++)
|
int value;
|
||||||
apples.add(new Apple(random.nextInt(100)));
|
for (int i = 0; i < 100; i++) {
|
||||||
|
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>();
|
||||||
testSort(sortingAlgorithm);
|
//populates random apples
|
||||||
|
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>();
|
||||||
testSort(sortingAlgorithm);
|
//populates random apples
|
||||||
|
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<>();
|
sortingAlgorithm = new Shell<Apple>();
|
||||||
testSort(sortingAlgorithm);
|
//populates random apples
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user