File tree Expand file tree Collapse file tree 2 files changed +6
-27
lines changed
main/java/com/thealgorithms/sorts
test/java/com/thealgorithms/sorts Expand file tree Collapse file tree 2 files changed +6
-27
lines changed Original file line number Diff line number Diff line change 55 *
66 * Dark Sort uses a temporary array to count occurrences of elements and
77 * reconstructs the sorted array based on the counts.
8- *
9- * @see SortAlgorithm
108 */
11- class DarkSort implements SortAlgorithm {
9+ class DarkSort {
1210
1311 /**
1412 * Sorts the array using the Dark Sort algorithm.
1513 *
1614 * @param unsorted the array to be sorted
17- * @param <T> Comparable class
1815 * @return sorted array
1916 */
20- @ Override
21- public <T extends Comparable <T >> T [] sort (T [] unsorted ) {
17+ public Integer [] sort (Integer [] unsorted ) {
2218 if (unsorted == null || unsorted .length <= 1 ) {
2319 return unsorted ;
2420 }
2521
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
3323
3424 // Create a temporary array for counting occurrences
3525 int [] temp = new int [max + 1 ];
3626
3727 // Count occurrences of each element
38- for (int value : arr ) {
28+ for (int value : unsorted ) {
3929 temp [value ]++;
4030 }
4131
4232 // Reconstruct the sorted array
4333 int index = 0 ;
4434 for (int i = 0 ; i < temp .length ; i ++) {
4535 while (temp [i ] > 0 ) {
46- arr [index ++] = i ;
36+ unsorted [index ++] = i ;
4737 temp [i ]--;
4838 }
4939 }
5040
51- return ( T []) arr ;
41+ return unsorted ;
5242 }
5343
5444 /**
Original file line number Diff line number Diff line change 22
33import static org .junit .jupiter .api .Assertions .assertArrayEquals ;
44import static org .junit .jupiter .api .Assertions .assertNull ;
5- import static org .junit .jupiter .api .Assertions .assertThrows ;
65
76import org .junit .jupiter .api .Test ;
87
@@ -72,14 +71,4 @@ void testNullArray() {
7271
7372 assertNull (sorted , "Sorting a null array should return null" );
7473 }
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- }
8574}
You can’t perform that action at this time.
0 commit comments