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$/module6" />
|
||||
<option value="$PROJECT_DIR$/module7" />
|
||||
<option value="$PROJECT_DIR$/module8" />
|
||||
</set>
|
||||
</option>
|
||||
</GradleProjectSettings>
|
||||
|
@ -6,6 +6,9 @@ public abstract class AbstractCustomSorts<E> {
|
||||
|
||||
// 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
|
||||
|
@ -3,7 +3,29 @@ package com.hithomelabs.princeton1.module5;
|
||||
public class Insertion<E> extends AbstractCustomSorts<E> {
|
||||
@Override
|
||||
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>{
|
||||
@Override
|
||||
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
|
||||
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) {
|
||||
|
@ -6,51 +6,91 @@ 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.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Random;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class SortTest {
|
||||
|
||||
private ArrayList<Apple> apples;
|
||||
private Apple[] apples, applesCopy;
|
||||
private AbstractCustomSorts<Apple> sortingAlgorithm;
|
||||
private Random random;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
apples = new ArrayList<Apple>();
|
||||
apples = new Apple[100];
|
||||
// applesCopy = new Apple[100];
|
||||
//sortingAlgorithm = new Selection<Apple>();
|
||||
random = new Random();
|
||||
}
|
||||
|
||||
private void testSort(AbstractCustomSorts<Apple> sortingAlgorithm) {
|
||||
for (int i = 0; i < 100; i++)
|
||||
apples.add(new Apple(random.nextInt(100)));
|
||||
private void testSort() {
|
||||
int value;
|
||||
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
|
||||
@DisplayName("Testing Insertion sort functionality")
|
||||
public void testInsertionSort() {
|
||||
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
|
||||
@DisplayName("Testing Selection sort functionality")
|
||||
public void testSelectionSort() {
|
||||
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
|
||||
@DisplayName("Testing Shell sort functionality")
|
||||
public void testShellSort() {
|
||||
sortingAlgorithm = new Shell<>();
|
||||
testSort(sortingAlgorithm);
|
||||
sortingAlgorithm = new Shell<Apple>();
|
||||
//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
|
||||
void tearDown() {
|
||||
sortingAlgorithm = null;
|
||||
apples = null;
|
||||
applesCopy = null;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user