Skip to content

Commit 1826719

Browse files
Adding Dark Sort
1 parent 466ff0b commit 1826719

File tree

2 files changed

+147
-0
lines changed

2 files changed

+147
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.thealgorithms.sorts;
2+
3+
import static com.thealgorithms.sorts.SortUtils.less;
4+
5+
/**
6+
* Generic implementation of Dark Sort algorithm.
7+
* Dark Sort is a conceptual sorting algorithm combining divide-and-conquer
8+
* and randomized selection principles.
9+
*
10+
* @see SortAlgorithm
11+
*/
12+
class DarkSort implements SortAlgorithm {
13+
14+
/**
15+
* Sorts the array using the Dark Sort algorithm.
16+
*
17+
* @param unsorted the array to be sorted
18+
* @param <T> Comparable class
19+
* @return sorted array
20+
*/
21+
@Override
22+
public <T extends Comparable<T>> T[] sort(T[] unsorted) {
23+
if (unsorted == null || unsorted.length <= 1) {
24+
return unsorted;
25+
}
26+
doDarkSort(unsorted, 0, unsorted.length - 1);
27+
return unsorted;
28+
}
29+
30+
/**
31+
* Recursive function that implements Dark Sort.
32+
*
33+
* @param arr the array to be sorted
34+
* @param left the starting index of the array
35+
* @param right the ending index of the array
36+
*/
37+
private <T extends Comparable<T>> void doDarkSort(T[] arr, int left, int right) {
38+
if (left >= right) {
39+
return;
40+
}
41+
42+
// Divide the array into two parts using a random pivot
43+
int pivotIndex = partition(arr, left, right);
44+
45+
// Recursively sort both halves
46+
doDarkSort(arr, left, pivotIndex - 1);
47+
doDarkSort(arr, pivotIndex + 1, right);
48+
}
49+
50+
/**
51+
* Partitions the array into two halves around a pivot element.
52+
*
53+
* @param arr the array to partition
54+
* @param left the starting index
55+
* @param right the ending index
56+
* @return the index of the pivot element
57+
*/
58+
private <T extends Comparable<T>> int partition(T[] arr, int left, int right) {
59+
T pivot = arr[right]; // Choosing the last element as pivot
60+
int i = left - 1;
61+
62+
for (int j = left; j < right; j++) {
63+
if (less(arr[j], pivot)) {
64+
i++;
65+
swap(arr, i, j);
66+
}
67+
}
68+
swap(arr, i + 1, right);
69+
return i + 1;
70+
}
71+
72+
/**
73+
* Swaps two elements in the array.
74+
*
75+
* @param arr the array
76+
* @param i the index of the first element
77+
* @param j the index of the second element
78+
*/
79+
private <T extends Comparable<T>> void swap(T[] arr, int i, int j) {
80+
T temp = arr[i];
81+
arr[i] = arr[j];
82+
arr[j] = temp;
83+
}
84+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.thealgorithms.sorts;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
class DarkSortTest {
8+
9+
@Test
10+
void testSortWithIntegers() {
11+
Integer[] unsorted = {5, 3, 8, 6, 2, 7, 4, 1};
12+
Integer[] expected = {1, 2, 3, 4, 5, 6, 7, 8};
13+
14+
DarkSort darkSort = new DarkSort();
15+
Integer[] sorted = darkSort.sort(unsorted);
16+
17+
assertArrayEquals(expected, sorted);
18+
}
19+
20+
@Test
21+
void testSortWithStrings() {
22+
String[] unsorted = {"zebra", "apple", "mango", "banana"};
23+
String[] expected = {"apple", "banana", "mango", "zebra"};
24+
25+
DarkSort darkSort = new DarkSort();
26+
String[] sorted = darkSort.sort(unsorted);
27+
28+
assertArrayEquals(expected, sorted);
29+
}
30+
31+
@Test
32+
void testEmptyArray() {
33+
Integer[] unsorted = {};
34+
Integer[] expected = {};
35+
36+
DarkSort darkSort = new DarkSort();
37+
Integer[] sorted = darkSort.sort(unsorted);
38+
39+
assertArrayEquals(expected, sorted);
40+
}
41+
42+
@Test
43+
void testSingleElementArray() {
44+
Integer[] unsorted = {42};
45+
Integer[] expected = {42};
46+
47+
DarkSort darkSort = new DarkSort();
48+
Integer[] sorted = darkSort.sort(unsorted);
49+
50+
assertArrayEquals(expected, sorted);
51+
}
52+
53+
@Test
54+
void testAlreadySortedArray() {
55+
Integer[] unsorted = {1, 2, 3, 4, 5};
56+
Integer[] expected = {1, 2, 3, 4, 5};
57+
58+
DarkSort darkSort = new DarkSort();
59+
Integer[] sorted = darkSort.sort(unsorted);
60+
61+
assertArrayEquals(expected, sorted);
62+
}
63+
}

0 commit comments

Comments
 (0)