Heap Sort: Simplify (#3777)

* 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 heap sort

* Update src/main/java/com/thealgorithms/sorts/HeapSort.java

* Update src/main/java/com/thealgorithms/sorts/HeapSort.java

Co-authored-by: Debasish Biswas <debasishbsws.abc@gmail.com>
This commit is contained in:
Narek Karapetian
2022-11-25 09:03:04 -08:00
committed by GitHub
parent 72468cc707
commit 7692e8f47d
2 changed files with 124 additions and 134 deletions

View File

@ -1,35 +1,95 @@
package com.thealgorithms.sorts;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class HeapSortTest {
private HeapSort heapSort = new HeapSort();
@Test
void testHeapSortCase1() {
Integer[] array = { 49, 4, 36, 9, 144, 1 };
Integer[] sorted = heapSort.sort(array);
Integer[] expected = { 1, 4, 9, 36, 49, 144 };
assertArrayEquals(expected, sorted);
private HeapSort heapSort;
@BeforeEach
void setUp() {
heapSort = new HeapSort();
}
@Test
void testHeapSortCase2() {
Integer[] array = { };
@Test
void shouldAcceptWhenEmptyArrayIsPassed() {
Integer[] array = new Integer[]{};
Integer[] expected = new Integer[]{};
Integer[] sorted = heapSort.sort(array);
Integer[] expected = { };
assertArrayEquals(expected, sorted);
}
@Test
void testHeapSortCase3 () {
Integer[] array = { -3, 5, 3, 4, 3, 7, 40, -20, 30, 0 };
Integer[] sorted = heapSort.sort(array);
Integer[] expected = { -20, -3, 0, 3, 3, 4, 5, 7, 30, 40 };
assertArrayEquals(expected, sorted);
}
@Test
void shouldAcceptWhenSingleValuedArrayIsPassed() {
Integer[] array = new Integer[]{2};
Integer[] expected = new Integer[]{2};
Integer[] sorted = heapSort.sort(array);
assertArrayEquals(expected, sorted);
}
@Test
void shouldAcceptWhenArrayWithAllPositiveValuesIsPassed() {
Integer[] array = new Integer[]{60, 7, 55, 9, 999, 3};
Integer[] expected = new Integer[]{3, 7, 9, 55, 60, 999};
Integer[] sorted = heapSort.sort(array);
assertArrayEquals(expected, sorted);
}
@Test
void shouldAcceptWhenArrayWithAllNegativeValuesIsPassed() {
Integer[] array = new Integer[]{-60, -7, -55, -9, -999, -3};
Integer[] expected = new Integer[]{-999, -60, -55, -9, -7, -3};
Integer[] sorted = heapSort.sort(array);
assertArrayEquals(expected, sorted);
}
@Test
void shouldAcceptWhenArrayWithRealNumberValuesIsPassed() {
Integer[] array = new Integer[]{60, -7, 55, 9, -999, -3};
Integer[] expected = new Integer[]{-999, -7, -3, 9, 55, 60};
Integer[] sorted = heapSort.sort(array);
assertArrayEquals(expected, sorted);
}
@Test
void shouldAcceptWhenArrayWithDuplicateValueIsPassed() {
Integer[] array = new Integer[]{60, 7, 55, 55, 999, 3};
Integer[] expected = new Integer[]{3, 7, 55, 55, 60, 999};
Integer[] sorted = heapSort.sort(array);
assertArrayEquals(expected, sorted);
}
@Test
void shouldAcceptWhenStringValueArrayIsPassed() {
String[] array = {"z", "a", "x", "b", "y"};
String[] expected = {"a", "b", "x", "y", "z"};
String[] sorted = heapSort.sort(array);
assertArrayEquals(expected, sorted);
}
@Test
void shouldAcceptWhenRandomArrayIsPassed() {
int randomSize = SortUtilsRandomGenerator.generateInt(10_000);
Double[] array = SortUtilsRandomGenerator.generateArray(randomSize);
Double[] sorted = heapSort.sort(array);
assertTrue(SortUtils.isSorted(sorted));
}
}