MergeSort: Simplify merge function (#3774)

* 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

* simplify merge function in MergeSort

* refactor one-liners

Co-authored-by: Debasish Biswas <debasishbsws.abc@gmail.com>
This commit is contained in:
Narek Karapetian
2022-11-24 14:24:17 -08:00
committed by GitHub
parent 260f1b2bee
commit 72468cc707
4 changed files with 44 additions and 54 deletions

View File

@ -106,7 +106,7 @@ class InsertionSortTest {
}
private void testWithRandomArray(Function<Double[], Double[]> sortAlgorithm) {
int randomSize = (int) (SortUtilsRandomGenerator.generateDouble() * 10_000);
int randomSize = SortUtilsRandomGenerator.generateInt(10_000);
Double[] array = SortUtilsRandomGenerator.generateArray(randomSize);
Double[] sorted = sortAlgorithm.apply(array);
assertTrue(SortUtils.isSorted(sorted));

View File

@ -1,13 +1,19 @@
package com.thealgorithms.sorts;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class MergeSortTest {
private static MergeSort mergeSort= new MergeSort();
private MergeSort mergeSort;
@BeforeEach
void setUp() {
mergeSort = new MergeSort();
}
@Test
void shouldAcceptWhenEmptyArrayIsPassed() {
@ -79,5 +85,11 @@ public class MergeSortTest {
assertArrayEquals(expected, sorted);
}
@Test
void shouldAcceptWhenRandomArrayIsPassed() {
int randomSize = SortUtilsRandomGenerator.generateInt(10_000);
Double[] array = SortUtilsRandomGenerator.generateArray(randomSize);
Double[] sorted = mergeSort.sort(array);
assertTrue(SortUtils.isSorted(sorted));
}
}