11package com .thealgorithms .sorts ;
22
3- import static com .thealgorithms .sorts .SortUtils .less ;
4-
53/**
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.
98 *
109 * @see SortAlgorithm
1110 */
@@ -19,67 +18,52 @@ class DarkSort implements SortAlgorithm {
1918 * @return sorted array
2019 */
2120 @ Override
22-
2321 public <T extends Comparable <T >> T [] sort (T [] unsorted ) {
2422 if (unsorted == null || unsorted .length <= 1 ) {
2523 return unsorted ;
2624 }
27- doDarkSort (unsorted , 0 , unsorted .length - 1 );
28- return unsorted ;
29- }
3025
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." );
4129 }
4230
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
4533
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 ];
5036
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+ }
6241
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 ]--;
6748 }
6849 }
69- swap ( arr , i + 1 , right );
70- return i + 1 ;
50+
51+ return ( T []) arr ;
7152 }
7253
7354 /**
74- * Swaps two elements in the array.
55+ * Helper method to find the maximum value in an array.
7556 *
7657 * @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
7959 */
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 ;
8468 }
8569}
0 commit comments