Skip to content

Commit c42e474

Browse files
author
Alex Klymenko
committed
refactor: adding classificationRatio as parameter
1 parent 742a188 commit c42e474

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

src/main/java/com/thealgorithms/sorts/FlashSort.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,22 @@
2222
* </ol>
2323
*/
2424
public class FlashSort implements SortAlgorithm {
25-
private static final double CLASSIFICATION_RATIO = 0.45;
25+
private final double classificationRatio;
26+
27+
public FlashSort() {
28+
classificationRatio = 0.45;
29+
}
30+
31+
public FlashSort(double classificationRatio) {
32+
if (classificationRatio <= 0 || classificationRatio >= 1) {
33+
throw new IllegalArgumentException("Classification ratio must be between 0 and 1 (exclusive).");
34+
}
35+
this.classificationRatio = classificationRatio;
36+
}
37+
38+
public double getClassificationRatio() {
39+
return classificationRatio;
40+
}
2641

2742
/**
2843
* Sorts an array using the Flash Sort algorithm.
@@ -55,7 +70,7 @@ private <T extends Comparable<? super T>> void flashSort(T[] arr) {
5570
return; // All elements are the same
5671
}
5772

58-
final int m = (int) (CLASSIFICATION_RATIO * arr.length);
73+
final int m = (int) (classificationRatio * arr.length);
5974

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,35 @@
11
package com.thealgorithms.sorts;
22

3+
import org.junit.jupiter.api.Test;
4+
import org.junit.jupiter.params.ParameterizedTest;
5+
import org.junit.jupiter.params.provider.ValueSource;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
import static org.junit.jupiter.api.Assertions.assertThrows;
9+
310
public class FlashSortTest extends SortingAlgorithmTest {
411
@Override
512
SortAlgorithm getSortAlgorithm() {
613
return new FlashSort();
714
}
15+
16+
@Test
17+
public void testDefaultConstructor() {
18+
double defaultRation = 0.45;
19+
FlashSort sorter = new FlashSort();
20+
assertEquals(defaultRation, sorter.getClassificationRatio());
21+
}
22+
23+
@ParameterizedTest
24+
@ValueSource(doubles = {0.1, 0.2, 0.5, 0.9})
25+
public void testCustomConstructorValidRatio(double ratio) {
26+
FlashSort sorter = new FlashSort(ratio);
27+
assertEquals(ratio, sorter.getClassificationRatio());
28+
}
29+
30+
@ParameterizedTest
31+
@ValueSource(doubles = {0, 1, -0.1, 1.1})
32+
public void testCustomConstructorInvalidRatio(double ratio) {
33+
assertThrows(IllegalArgumentException.class, () -> new FlashSort(ratio));
34+
}
835
}

0 commit comments

Comments
 (0)