Skip to content

Commit c59956b

Browse files
Changes in Dark Sort
1 parent 722cdea commit c59956b

File tree

2 files changed

+59
-60
lines changed

2 files changed

+59
-60
lines changed
Lines changed: 33 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package 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
}

src/test/java/com/thealgorithms/sorts/DarkSortTest.java

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.thealgorithms.sorts;
22

33
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
45
import static org.junit.jupiter.api.Assertions.assertNull;
56

67
import org.junit.jupiter.api.Test;
@@ -18,17 +19,6 @@ void testSortWithIntegers() {
1819
assertArrayEquals(expected, sorted);
1920
}
2021

21-
@Test
22-
void testSortWithStrings() {
23-
String[] unsorted = {"zebra", "apple", "mango", "banana"};
24-
String[] expected = {"apple", "banana", "mango", "zebra"};
25-
26-
DarkSort darkSort = new DarkSort();
27-
String[] sorted = darkSort.sort(unsorted);
28-
29-
assertArrayEquals(expected, sorted);
30-
}
31-
3222
@Test
3323
void testEmptyArray() {
3424
Integer[] unsorted = {};
@@ -62,6 +52,17 @@ void testAlreadySortedArray() {
6252
assertArrayEquals(expected, sorted);
6353
}
6454

55+
@Test
56+
void testDuplicateElementsArray() {
57+
Integer[] unsorted = {4, 2, 7, 2, 1, 4};
58+
Integer[] expected = {1, 2, 2, 4, 4, 7};
59+
60+
DarkSort darkSort = new DarkSort();
61+
Integer[] sorted = darkSort.sort(unsorted);
62+
63+
assertArrayEquals(expected, sorted);
64+
}
65+
6566
@Test
6667
void testNullArray() {
6768
Integer[] unsorted = null;
@@ -71,4 +72,18 @@ void testNullArray() {
7172

7273
assertNull(sorted, "Sorting a null array should return null");
7374
}
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(
84+
IllegalArgumentException.class,
85+
() -> darkSort.sort(unsorted),
86+
"DarkSort only supports Integer arrays."
87+
);
88+
}
7489
}

0 commit comments

Comments
 (0)