Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ public class DutchNationalFlagSort implements SortAlgorithm {

@Override
public <T extends Comparable<T>> T[] sort(T[] array) {
if (array.length == 0) {
return array;
}
return dutchNationalFlagSort(array, array[(int) Math.ceil((array.length) / 2.0) - 1]);
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/thealgorithms/sorts/FlashSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ private <T extends Comparable<? super T>> void flashSort(T[] arr) {
return; // All elements are the same
}

final int m = (int) (classificationRatio * arr.length);
// At least one class, otherwise small arrays index into an empty classification table
final int m = Math.max(1, (int) (classificationRatio * arr.length));

final int[] classificationArray = new int[m];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,10 @@ void testOddStringsMidGivenDnfs() {
dutchNationalFlagSort.sort(strings, "e");
assertArrayEquals(strings, stringsResult);
}

@Test
void testEmptyArray() {
Integer[] integers = {};
assertArrayEquals(new Integer[] {}, new DutchNationalFlagSort().sort(integers));
}
}
7 changes: 7 additions & 0 deletions src/test/java/com/thealgorithms/sorts/FlashSortTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.thealgorithms.sorts;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

Expand Down Expand Up @@ -59,6 +60,12 @@ public List<DynamicTest> dynamicTestsForSorting() {
return dynamicTests;
}

@Test
public void testArraysTooSmallForMoreThanOneClass() {
assertArrayEquals(new Integer[] {1, 2}, new FlashSort().sort(new Integer[] {2, 1}));
assertArrayEquals(new Integer[] {1, 2, 3, 4, 5}, new FlashSort(0.1).sort(new Integer[] {5, 3, 1, 4, 2}));
}

private List<DynamicTest> createDynamicTestsForRatio(double ratio) {
List<DynamicTest> dynamicTests = new ArrayList<>();
for (TestMethod testMethod : getTestMethodsFromSuperClass()) {
Expand Down
Loading