Add DarkSort Algorithm (#6141)

This commit is contained in:
Sahil Kumar Valecha
2025-01-18 22:34:34 +05:00
committed by GitHub
parent 466ff0b4c2
commit 5454e2ff62
2 changed files with 133 additions and 0 deletions

View File

@ -0,0 +1,59 @@
package com.thealgorithms.sorts;
/**
* Dark Sort algorithm implementation.
*
* Dark Sort uses a temporary array to count occurrences of elements and
* reconstructs the sorted array based on the counts.
*/
class DarkSort {
/**
* Sorts the array using the Dark Sort algorithm.
*
* @param unsorted the array to be sorted
* @return sorted array
*/
public Integer[] sort(Integer[] unsorted) {
if (unsorted == null || unsorted.length <= 1) {
return unsorted;
}
int max = findMax(unsorted); // Find the maximum value in the array
// Create a temporary array for counting occurrences
int[] temp = new int[max + 1];
// Count occurrences of each element
for (int value : unsorted) {
temp[value]++;
}
// Reconstruct the sorted array
int index = 0;
for (int i = 0; i < temp.length; i++) {
while (temp[i] > 0) {
unsorted[index++] = i;
temp[i]--;
}
}
return unsorted;
}
/**
* Helper method to find the maximum value in an array.
*
* @param arr the array
* @return the maximum value
*/
private int findMax(Integer[] arr) {
int max = arr[0];
for (int value : arr) {
if (value > max) {
max = value;
}
}
return max;
}
}