1
1
package com .thealgorithms .sorts ;
2
2
3
- import static com .thealgorithms .sorts .SortUtils .less ;
4
-
5
3
/**
6
- * Generic implementation of Dark Sort algorithm.
7
- * Dark Sort is a conceptual sorting algorithm combining divide-and-conquer
8
- * and randomized selection principles.
4
+ * Dark Sort algorithm implementation.
5
+ *
6
+ * Dark Sort uses a temporary array to count occurrences of elements and
7
+ * reconstructs the sorted array based on the counts.
9
8
*
10
9
* @see SortAlgorithm
11
10
*/
@@ -19,67 +18,52 @@ class DarkSort implements SortAlgorithm {
19
18
* @return sorted array
20
19
*/
21
20
@ Override
22
-
23
21
public <T extends Comparable <T >> T [] sort (T [] unsorted ) {
24
22
if (unsorted == null || unsorted .length <= 1 ) {
25
23
return unsorted ;
26
24
}
27
- doDarkSort (unsorted , 0 , unsorted .length - 1 );
28
- return unsorted ;
29
- }
30
25
31
- /**
32
- * Recursive function that implements Dark Sort.
33
- *
34
- * @param arr the array to be sorted
35
- * @param left the starting index of the array
36
- * @param right the ending index of the array
37
- */
38
- private <T extends Comparable <T >> void doDarkSort (T [] arr , int left , int right ) {
39
- if (left >= right ) {
40
- return ;
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." );
41
29
}
42
30
43
- // Divide the array into two parts using a random pivot
44
- int pivotIndex = partition (arr , left , right );
31
+ Integer [] arr = ( Integer []) unsorted ;
32
+ int max = findMax (arr ); // Find the maximum value in the array
45
33
46
- // Recursively sort both halves
47
- doDarkSort (arr , left , pivotIndex - 1 );
48
- doDarkSort (arr , pivotIndex + 1 , right );
49
- }
34
+ // Create a temporary array for counting occurrences
35
+ int [] temp = new int [max + 1 ];
50
36
51
- /**
52
- * Partitions the array into two halves around a pivot element.
53
- *
54
- * @param arr the array to partition
55
- * @param left the starting index
56
- * @param right the ending index
57
- * @return the index of the pivot element
58
- */
59
- private <T extends Comparable <T >> int partition (T [] arr , int left , int right ) {
60
- T pivot = arr [right ]; // Choosing the last element as pivot
61
- int i = left - 1 ;
37
+ // Count occurrences of each element
38
+ for (int value : arr ) {
39
+ temp [value ]++;
40
+ }
62
41
63
- for (int j = left ; j < right ; j ++) {
64
- if (less (arr [j ], pivot )) {
65
- i ++;
66
- swap (arr , i , j );
42
+ // Reconstruct the sorted array
43
+ int index = 0 ;
44
+ for (int i = 0 ; i < temp .length ; i ++) {
45
+ while (temp [i ] > 0 ) {
46
+ arr [index ++] = i ;
47
+ temp [i ]--;
67
48
}
68
49
}
69
- swap ( arr , i + 1 , right );
70
- return i + 1 ;
50
+
51
+ return ( T []) arr ;
71
52
}
72
53
73
54
/**
74
- * Swaps two elements in the array.
55
+ * Helper method to find the maximum value in an array.
75
56
*
76
57
* @param arr the array
77
- * @param i the index of the first element
78
- * @param j the index of the second element
58
+ * @return the maximum value
79
59
*/
80
- private <T extends Comparable <T >> void swap (T [] arr , int i , int j ) {
81
- T temp = arr [i ];
82
- arr [i ] = arr [j ];
83
- arr [j ] = temp ;
60
+ private int findMax (Integer [] arr ) {
61
+ int max = arr [0 ];
62
+ for (int value : arr ) {
63
+ if (value > max ) {
64
+ max = value ;
65
+ }
66
+ }
67
+ return max ;
84
68
}
85
69
}
0 commit comments