Insertion sort and selection sort with test cases

This commit is contained in:
hitanshu310 2025-02-01 16:34:39 +05:30
parent fdca30f50a
commit 7ba2d22e43
13 changed files with 291 additions and 0 deletions

View File

@ -0,0 +1,36 @@
package com.hithomelabs.clients.module5;
import com.hithomelabs.princeton1.module5.Insertion;
import com.hithomelabs.princeton1.module5.Apple;
import com.hithomelabs.princeton1.module5.Orange;
public class InsertionClient {
public static void main(String[] args){
Apple[] apples = new Apple[3];
Orange[] oranges = new Orange[3];
Insertion<Apple> insertion = new Insertion<>();
apples[0] = new Apple(3);
apples[1] = new Apple(5);
apples[2] = new Apple(4);
insertion.sort(apples);
//* * Sample output
for (int i = 0; i < apples.length; i++)
System.out.println(apples[i]);
oranges[0] = new Orange(4);
oranges[1] = new Orange(1);
// * Should give runtime exception as ClassCastException is a runtime exception
//insertion.sort(oranges);
Insertion<Orange> selection2 = new Insertion<>();
// * Should result in a compile time exception, as casting to Orange will fail
//selection2.sort(apples);
}
}

View File

@ -0,0 +1,66 @@
package com.hithomelabs.clients.module5;
import com.hithomelabs.princeton1.module5.Selection;
import java.util.ArrayList;
public class SelectionClient {
public static void main(String[] args){
Apple[] apples = new Apple[3];
Orange[] oranges = new Orange[3];
Selection<Apple> selection = new Selection<Apple>();
apples[0] = new Apple(3);
apples[1] = new Apple(5);
apples[2] = new Apple(4);
selection.sort(apples);
//* * Sample output
for (int i = 0; i < apples.length; i++)
System.out.println(apples[i]);
oranges[0] = new Orange(4);
oranges[1] = new Orange(1);
// * Should give runtime exception as ClassCastException is a runtime exception
//insertion.sort(oranges);
Selection<Orange> selection2 = new Selection<Orange>();
// * Should result in a compile time exception, as casting to Orange will fail
//selection2.sort(apples);
}
private static class Orange{
private int size;
Orange(int size){
this.size = size;
}
@Override
public String toString() {
return "An orange of size "+size;
}
}
private static class Apple implements Comparable<Apple>{
private int size;
Apple(int size){
this.size = size;
}
@Override
public String toString() {
return "An apple of size "+size;
}
@Override
public int compareTo(Apple that) {
if (this.size < that.size) return -1;
else if (this.size == that.size)return 0;
else return 1;
}
}
}

19
module5/build.gradle Normal file
View File

@ -0,0 +1,19 @@
plugins {
id 'java'
}
group = 'com.hithomelabs.princeton1.module5'
version = 'unspecified'
repositories {
mavenCentral()
}
dependencies {
testImplementation platform('org.junit:junit-bom:5.10.0')
testImplementation 'org.junit.jupiter:junit-jupiter'
}
test {
useJUnitPlatform()
}

View File

@ -0,0 +1,17 @@
package com.hithomelabs.princeton1.module5;
public abstract class AbstractCustomSorts<E> {
public abstract void sort(E[] arr);
public void exch(E[] arr, int j, int i) {
E temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public boolean less(Comparable<E> e1, E e2) {
return e1.compareTo(e2) < 0;
}
}

View File

@ -0,0 +1,37 @@
package com.hithomelabs.princeton1.module5;
import java.util.Objects;
public class Apple implements Comparable<Apple> {
private int size;
public Apple(int size) {
this.size = size;
}
@Override
public boolean equals(Object obj) {
if(this == obj) return true;
if (obj == null || this.getClass() != obj.getClass())
return false;
Apple that = (Apple) obj;
return Objects.equals(this.size, that.size);
}
@Override
public int hashCode() {
return Objects.hash(size);
}
@Override
public String toString() {
return "An apple of size " + size;
}
@Override
public int compareTo(Apple that) {
if (this.size < that.size) return -1;
else if (this.size == that.size) return 0;
else return 1;
}
}

View File

@ -0,0 +1,23 @@
package com.hithomelabs.princeton1.module5;
public class Insertion<E> extends AbstractCustomSorts<E> {
public void sort(E[] arr){
if (arr == null) return;
else{
int N = arr.length;
// * * swap arr[i] with each element greater to it's left
for (int i = 1; i < N; i++){
int j = i;
while(j >= 1 && less((Comparable<E>)arr[j], arr[j-1])){
exch(arr, j, j-1);
j = j-1;
}
}
}
}
}

View File

@ -0,0 +1,14 @@
package com.hithomelabs.princeton1.module5;
public class Orange {
private int size;
public Orange(int size) {
this.size = size;
}
@Override
public String toString() {
return "An orange of size " + size;
}
}

View File

@ -0,0 +1,23 @@
package com.hithomelabs.princeton1.module5;
public class Selection<E> extends AbstractCustomSorts<E>{
/*
* * 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]
*/
public void sort(E[] arr){
if (arr == null) return;
Comparable<E>[] arr1 = (Comparable<E>[]) arr;
for(int i = 0; i < arr1.length - 1; i++){
int minIndex = i;
for(int j = i+1; j < arr.length; j ++){
if (less((Comparable<E>) arr[j], arr[minIndex])) minIndex = j;
}
exch(arr, i, minIndex);
}
}
}

View File

@ -0,0 +1,56 @@
package com.hithomelabs.princeton1.module5;
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 InsertionTest {
private ArrayList<Apple> apples;
private AbstractCustomSorts<Apple> sortingAlgorithm;
private Random random;
@BeforeEach
void setUp() {
apples = new ArrayList<Apple>();
//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)));
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
@DisplayName("Testing Insertion sort functionality")
public void testInsertionSort() {
sortingAlgorithm = new Insertion<Apple>();
testSort(sortingAlgorithm);
}
@Test
@DisplayName("Testing Selection sort functionality")
public void testSelectionSort() {
sortingAlgorithm = new Selection<Apple>();
testSort(sortingAlgorithm);
}
@AfterEach
void tearDown() {
sortingAlgorithm = null;
apples = null;
}
}