Modify Insertion sort implementation to classical one + add function insertion-sentinel sort. (#3622)

* bug fix for CircularBuffer + refactoring + add unit tests

* change Insertion sort to classical implementation + add isSorted function to SortUtils + add SortUtilsRandomGenerator for generating random values and arrays

* little fix

Co-authored-by: Debasish Biswas <debasishbsws.abc@gmail.com>
This commit is contained in:
Narek Karapetian
2022-11-06 04:24:08 -08:00
committed by GitHub
parent c8ecd23183
commit 58cf08f2fd
6 changed files with 303 additions and 20 deletions

View File

@ -0,0 +1,114 @@
package com.thealgorithms.sorts;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.function.Function;
import static org.junit.jupiter.api.Assertions.*;
class InsertionSortTest {
private InsertionSort insertionSort;
@BeforeEach
void setUp() {
insertionSort = new InsertionSort();
}
@Test
void insertionSortSortEmptyArrayShouldPass() {
testEmptyArray(insertionSort::sort);
testEmptyArray(insertionSort::sentinelSort);
}
private void testEmptyArray(Function<Integer[], Integer[]> sortAlgorithm) {
Integer[] array = {};
Integer[] sorted = sortAlgorithm.apply(array);
Integer[] expected = {};
assertArrayEquals(expected, sorted);
assertTrue(SortUtils.isSorted(sorted));
}
@Test
void insertionSortClassicalSortSingleValueArrayShouldPass() {
testSingleValue(insertionSort::sort);
testSingleValue(insertionSort::sentinelSort);
}
private void testSingleValue(Function<Integer[], Integer[]> sortAlgorithm) {
Integer[] array = {7};
Integer[] sorted = sortAlgorithm.apply(array);
Integer[] expected = {7};
assertArrayEquals(expected, sorted);
assertTrue(SortUtils.isSorted(sorted));
}
@Test
void insertionSortClassicalWithIntegerArrayShouldPass() {
testIntegerArray(insertionSort::sort);
testIntegerArray(insertionSort::sentinelSort);
}
private void testIntegerArray(Function<Integer[], Integer[]> sortAlgorithm) {
Integer[] array = {49, 4, 36, 9, 144, 1};
Integer[] sorted = sortAlgorithm.apply(array);
Integer[] expected = {1, 4, 9, 36, 49, 144};
assertArrayEquals(expected, sorted);
assertTrue(SortUtils.isSorted(sorted));
}
@Test
void insertionSortClassicalForArrayWithNegativeValuesShouldPass() {
testWithNegativeValues(insertionSort::sort);
testWithNegativeValues(insertionSort::sentinelSort);
}
private void testWithNegativeValues(Function<Integer[], Integer[]> sortAlgorithm) {
Integer[] array = {49, -36, -144, -49, 1, 9};
Integer[] sorted = sortAlgorithm.apply(array);
Integer[] expected = {-144, -49, -36, 1, 9, 49};
assertArrayEquals(expected, sorted);
assertTrue(SortUtils.isSorted(sorted));
}
@Test
void insertionSortClassicalForArrayWithDuplicateValuesShouldPass() {
testWithDuplicates(insertionSort::sort);
testWithDuplicates(insertionSort::sentinelSort);
}
private void testWithDuplicates(Function<Integer[], Integer[]> sortAlgorithm) {
Integer[] array = {36, 1, 49, 1, 4, 9};
Integer[] sorted = sortAlgorithm.apply(array);
Integer[] expected = {1, 1, 4, 9, 36, 49};
assertArrayEquals(expected, sorted);
assertTrue(SortUtils.isSorted(sorted));
}
@Test
void insertionSortClassicalWithStringArrayShouldPass() {
testWithStringArray(insertionSort::sort);
testWithStringArray(insertionSort::sentinelSort);
}
private void testWithStringArray(Function<String[], String[]> sortAlgorithm) {
String[] array = {"c", "a", "e", "b", "d"};
String[] sorted = sortAlgorithm.apply(array);
String[] expected = {"a", "b", "c", "d", "e"};
assertArrayEquals(expected, sorted);
assertTrue(SortUtils.isSorted(sorted));
}
@Test
void insertionSortClassicalWithRandomArrayPass() {
testWithRandomArray(insertionSort::sort);
testWithRandomArray(insertionSort::sentinelSort);
}
private void testWithRandomArray(Function<Double[], Double[]> sortAlgorithm) {
int randomSize = (int) (SortUtilsRandomGenerator.generateDouble() * 10_000);
Double[] array = SortUtilsRandomGenerator.generateArray(randomSize);
Double[] sorted = sortAlgorithm.apply(array);
assertTrue(SortUtils.isSorted(sorted));
}
}

View File

@ -0,0 +1,31 @@
package com.thealgorithms.sorts;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
class SortUtilsRandomGeneratorTest {
@RepeatedTest(1000)
void generateArray() {
int size = 1_000;
Double[] doubles = SortUtilsRandomGenerator.generateArray(size);
assertThat(doubles).hasSize(size);
assertThat(doubles).doesNotContainNull();
}
@Test
void generateArrayEmpty() {
int size = 0;
Double[] doubles = SortUtilsRandomGenerator.generateArray(size);
assertThat(doubles).hasSize(size);
}
@RepeatedTest(1000)
void generateDouble() {
Double randomDouble = SortUtilsRandomGenerator.generateDouble();
assertThat(randomDouble).isBetween(0.0, 1.0);
assertThat(randomDouble).isNotEqualTo(1.0);
}
}

View File

@ -0,0 +1,44 @@
package com.thealgorithms.sorts;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class SortUtilsTest {
@Test
void isSortedEmptyArray() {
Double[] emptyArray = {};
assertTrue(SortUtils.isSorted(emptyArray));
}
@Test
void isSortedWithSingleElement() {
Double[] singleElementArray = {1.0};
assertTrue(SortUtils.isSorted(singleElementArray));
}
@Test
void isSortedTrue() {
Integer[] array = {1, 1, 2, 3, 5, 8, 11};
assertTrue(SortUtils.isSorted(array));
Integer[] identicalArray = {1, 1, 1, 1, 1};
assertTrue(SortUtils.isSorted(identicalArray));
Double[] doubles = {-15.123, -15.111, 0.0, 0.12, 0.15};
assertTrue(SortUtils.isSorted(doubles));
}
@Test
void isSortedFalse() {
Double[] array = {1.0, 3.0, -0.15};
assertFalse(SortUtils.isSorted(array));
Integer[] array2 = {14, 15, 16, 1};
assertFalse(SortUtils.isSorted(array2));
Integer[] array3 = {5, 4, 3, 2, 1};
assertFalse(SortUtils.isSorted(array3));
}
}