Skip to content

Adding DarkSort Algorithm #6141

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions src/main/java/com/thealgorithms/sorts/DarkSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
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.
*
* @see SortAlgorithm
*/
class DarkSort implements SortAlgorithm {

/**
* Sorts the array using the Dark Sort algorithm.
*
* @param unsorted the array to be sorted
* @param <T> Comparable class
* @return sorted array
*/
@Override
public <T extends Comparable<T>> T[] sort(T[] unsorted) {
if (unsorted == null || unsorted.length <= 1) {
return unsorted;
}

// Dark Sort works only for integers, so we cast and check
if (!(unsorted instanceof Integer[])) {
throw new IllegalArgumentException("Dark Sort only supports Integer arrays.");
}

Integer[] arr = (Integer[]) unsorted;
int max = findMax(arr); // 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 : arr) {
temp[value]++;
}

// Reconstruct the sorted array
int index = 0;
for (int i = 0; i < temp.length; i++) {
while (temp[i] > 0) {
arr[index++] = i;
temp[i]--;
}
}

return (T[]) arr;
}

/**
* 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;
}
}
85 changes: 85 additions & 0 deletions src/test/java/com/thealgorithms/sorts/DarkSortTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.thealgorithms.sorts;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

import org.junit.jupiter.api.Test;

class DarkSortTest {

@Test
void testSortWithIntegers() {
Integer[] unsorted = {5, 3, 8, 6, 2, 7, 4, 1};
Integer[] expected = {1, 2, 3, 4, 5, 6, 7, 8};

DarkSort darkSort = new DarkSort();
Integer[] sorted = darkSort.sort(unsorted);

assertArrayEquals(expected, sorted);
}

@Test
void testEmptyArray() {
Integer[] unsorted = {};
Integer[] expected = {};

DarkSort darkSort = new DarkSort();
Integer[] sorted = darkSort.sort(unsorted);

assertArrayEquals(expected, sorted);
}

@Test
void testSingleElementArray() {
Integer[] unsorted = {42};
Integer[] expected = {42};

DarkSort darkSort = new DarkSort();
Integer[] sorted = darkSort.sort(unsorted);

assertArrayEquals(expected, sorted);
}

@Test
void testAlreadySortedArray() {
Integer[] unsorted = {1, 2, 3, 4, 5};
Integer[] expected = {1, 2, 3, 4, 5};

DarkSort darkSort = new DarkSort();
Integer[] sorted = darkSort.sort(unsorted);

assertArrayEquals(expected, sorted);
}

@Test
void testDuplicateElementsArray() {
Integer[] unsorted = {4, 2, 7, 2, 1, 4};
Integer[] expected = {1, 2, 2, 4, 4, 7};

DarkSort darkSort = new DarkSort();
Integer[] sorted = darkSort.sort(unsorted);

assertArrayEquals(expected, sorted);
}

@Test
void testNullArray() {
Integer[] unsorted = null;

DarkSort darkSort = new DarkSort();
Integer[] sorted = darkSort.sort(unsorted);

assertNull(sorted, "Sorting a null array should return null");
}

@Test
void testNonIntegerArray() {
String[] unsorted = {"zebra", "apple", "mango", "banana"};

DarkSort darkSort = new DarkSort();

// DarkSort should throw an IllegalArgumentException for non-integer arrays
assertThrows(IllegalArgumentException.class, () -> darkSort.sort(unsorted), "DarkSort only supports Integer arrays.");
}
}
Loading