File tree 2 files changed +44
-2
lines changed
main/java/com/thealgorithms/sorts
test/java/com/thealgorithms/sorts 2 files changed +44
-2
lines changed Original file line number Diff line number Diff line change 22
22
* </ol>
23
23
*/
24
24
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
+ }
26
41
27
42
/**
28
43
* Sorts an array using the Flash Sort algorithm.
@@ -55,7 +70,7 @@ private <T extends Comparable<? super T>> void flashSort(T[] arr) {
55
70
return ; // All elements are the same
56
71
}
57
72
58
- final int m = (int ) (CLASSIFICATION_RATIO * arr .length );
73
+ final int m = (int ) (classificationRatio * arr .length );
59
74
60
75
final int [] classificationArray = new int [m ];
61
76
Original file line number Diff line number Diff line change 1
1
package com .thealgorithms .sorts ;
2
2
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
+
3
10
public class FlashSortTest extends SortingAlgorithmTest {
4
11
@ Override
5
12
SortAlgorithm getSortAlgorithm () {
6
13
return new FlashSort ();
7
14
}
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
+ }
8
35
}
You can’t perform that action at this time.
0 commit comments