File tree 2 files changed +6
-27
lines changed
main/java/com/thealgorithms/sorts
test/java/com/thealgorithms/sorts 2 files changed +6
-27
lines changed Original file line number Diff line number Diff line change 5
5
*
6
6
* Dark Sort uses a temporary array to count occurrences of elements and
7
7
* reconstructs the sorted array based on the counts.
8
- *
9
- * @see SortAlgorithm
10
8
*/
11
- class DarkSort implements SortAlgorithm {
9
+ class DarkSort {
12
10
13
11
/**
14
12
* Sorts the array using the Dark Sort algorithm.
15
13
*
16
14
* @param unsorted the array to be sorted
17
- * @param <T> Comparable class
18
15
* @return sorted array
19
16
*/
20
- @ Override
21
- public <T extends Comparable <T >> T [] sort (T [] unsorted ) {
17
+ public Integer [] sort (Integer [] unsorted ) {
22
18
if (unsorted == null || unsorted .length <= 1 ) {
23
19
return unsorted ;
24
20
}
25
21
26
- // Dark Sort works only for integers, so we cast and check
27
- if (!(unsorted instanceof Integer [])) {
28
- throw new IllegalArgumentException ("Dark Sort only supports Integer arrays." );
29
- }
30
-
31
- Integer [] arr = (Integer []) unsorted ;
32
- int max = findMax (arr ); // Find the maximum value in the array
22
+ int max = findMax (unsorted ); // Find the maximum value in the array
33
23
34
24
// Create a temporary array for counting occurrences
35
25
int [] temp = new int [max + 1 ];
36
26
37
27
// Count occurrences of each element
38
- for (int value : arr ) {
28
+ for (int value : unsorted ) {
39
29
temp [value ]++;
40
30
}
41
31
42
32
// Reconstruct the sorted array
43
33
int index = 0 ;
44
34
for (int i = 0 ; i < temp .length ; i ++) {
45
35
while (temp [i ] > 0 ) {
46
- arr [index ++] = i ;
36
+ unsorted [index ++] = i ;
47
37
temp [i ]--;
48
38
}
49
39
}
50
40
51
- return ( T []) arr ;
41
+ return unsorted ;
52
42
}
53
43
54
44
/**
Original file line number Diff line number Diff line change 2
2
3
3
import static org .junit .jupiter .api .Assertions .assertArrayEquals ;
4
4
import static org .junit .jupiter .api .Assertions .assertNull ;
5
- import static org .junit .jupiter .api .Assertions .assertThrows ;
6
5
7
6
import org .junit .jupiter .api .Test ;
8
7
@@ -72,14 +71,4 @@ void testNullArray() {
72
71
73
72
assertNull (sorted , "Sorting a null array should return null" );
74
73
}
75
-
76
- @ Test
77
- void testNonIntegerArray () {
78
- String [] unsorted = {"zebra" , "apple" , "mango" , "banana" };
79
-
80
- DarkSort darkSort = new DarkSort ();
81
-
82
- // DarkSort should throw an IllegalArgumentException for non-integer arrays
83
- assertThrows (IllegalArgumentException .class , () -> darkSort .sort (unsorted ), "DarkSort only supports Integer arrays." );
84
- }
85
74
}
You can’t perform that action at this time.
0 commit comments