Skip to content

Commit 5454e2f

Browse files
Add DarkSort Algorithm (#6141)
1 parent 466ff0b commit 5454e2f

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.thealgorithms.sorts;
2+
3+
/**
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.
8+
*/
9+
class DarkSort {
10+
11+
/**
12+
* Sorts the array using the Dark Sort algorithm.
13+
*
14+
* @param unsorted the array to be sorted
15+
* @return sorted array
16+
*/
17+
public Integer[] sort(Integer[] unsorted) {
18+
if (unsorted == null || unsorted.length <= 1) {
19+
return unsorted;
20+
}
21+
22+
int max = findMax(unsorted); // Find the maximum value in the array
23+
24+
// Create a temporary array for counting occurrences
25+
int[] temp = new int[max + 1];
26+
27+
// Count occurrences of each element
28+
for (int value : unsorted) {
29+
temp[value]++;
30+
}
31+
32+
// Reconstruct the sorted array
33+
int index = 0;
34+
for (int i = 0; i < temp.length; i++) {
35+
while (temp[i] > 0) {
36+
unsorted[index++] = i;
37+
temp[i]--;
38+
}
39+
}
40+
41+
return unsorted;
42+
}
43+
44+
/**
45+
* Helper method to find the maximum value in an array.
46+
*
47+
* @param arr the array
48+
* @return the maximum value
49+
*/
50+
private int findMax(Integer[] arr) {
51+
int max = arr[0];
52+
for (int value : arr) {
53+
if (value > max) {
54+
max = value;
55+
}
56+
}
57+
return max;
58+
}
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.thealgorithms.sorts;
2+
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNull;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class DarkSortTest {
9+
10+
@Test
11+
void testSortWithIntegers() {
12+
Integer[] unsorted = {5, 3, 8, 6, 2, 7, 4, 1};
13+
Integer[] expected = {1, 2, 3, 4, 5, 6, 7, 8};
14+
15+
DarkSort darkSort = new DarkSort();
16+
Integer[] sorted = darkSort.sort(unsorted);
17+
18+
assertArrayEquals(expected, sorted);
19+
}
20+
21+
@Test
22+
void testEmptyArray() {
23+
Integer[] unsorted = {};
24+
Integer[] expected = {};
25+
26+
DarkSort darkSort = new DarkSort();
27+
Integer[] sorted = darkSort.sort(unsorted);
28+
29+
assertArrayEquals(expected, sorted);
30+
}
31+
32+
@Test
33+
void testSingleElementArray() {
34+
Integer[] unsorted = {42};
35+
Integer[] expected = {42};
36+
37+
DarkSort darkSort = new DarkSort();
38+
Integer[] sorted = darkSort.sort(unsorted);
39+
40+
assertArrayEquals(expected, sorted);
41+
}
42+
43+
@Test
44+
void testAlreadySortedArray() {
45+
Integer[] unsorted = {1, 2, 3, 4, 5};
46+
Integer[] expected = {1, 2, 3, 4, 5};
47+
48+
DarkSort darkSort = new DarkSort();
49+
Integer[] sorted = darkSort.sort(unsorted);
50+
51+
assertArrayEquals(expected, sorted);
52+
}
53+
54+
@Test
55+
void testDuplicateElementsArray() {
56+
Integer[] unsorted = {4, 2, 7, 2, 1, 4};
57+
Integer[] expected = {1, 2, 2, 4, 4, 7};
58+
59+
DarkSort darkSort = new DarkSort();
60+
Integer[] sorted = darkSort.sort(unsorted);
61+
62+
assertArrayEquals(expected, sorted);
63+
}
64+
65+
@Test
66+
void testNullArray() {
67+
Integer[] unsorted = null;
68+
69+
DarkSort darkSort = new DarkSort();
70+
Integer[] sorted = darkSort.sort(unsorted);
71+
72+
assertNull(sorted, "Sorting a null array should return null");
73+
}
74+
}

0 commit comments

Comments
 (0)