From 18267194faa951c1d133494b6f146f233753f123 Mon Sep 17 00:00:00 2001 From: sahilkumarvalecha <106372522+sahilkumarvalecha@users.noreply.github.com> Date: Thu, 16 Jan 2025 14:02:06 +0500 Subject: [PATCH 1/8] Adding Dark Sort --- .../com/thealgorithms/sorts/DarkSort.java | 84 +++++++++++++++++++ .../com/thealgorithms/sorts/DarkSortTest.java | 63 ++++++++++++++ 2 files changed, 147 insertions(+) create mode 100644 src/main/java/com/thealgorithms/sorts/DarkSort.java create mode 100644 src/test/java/com/thealgorithms/sorts/DarkSortTest.java diff --git a/src/main/java/com/thealgorithms/sorts/DarkSort.java b/src/main/java/com/thealgorithms/sorts/DarkSort.java new file mode 100644 index 000000000000..434bb716c0d6 --- /dev/null +++ b/src/main/java/com/thealgorithms/sorts/DarkSort.java @@ -0,0 +1,84 @@ +package com.thealgorithms.sorts; + +import static com.thealgorithms.sorts.SortUtils.less; + +/** + * Generic implementation of Dark Sort algorithm. + * Dark Sort is a conceptual sorting algorithm combining divide-and-conquer + * and randomized selection principles. + * + * @see SortAlgorithm + */ +class DarkSort implements SortAlgorithm { + + /** + * Sorts the array using the Dark Sort algorithm. + * + * @param unsorted the array to be sorted + * @param Comparable class + * @return sorted array + */ + @Override + public > T[] sort(T[] unsorted) { + if (unsorted == null || unsorted.length <= 1) { + return unsorted; + } + doDarkSort(unsorted, 0, unsorted.length - 1); + return unsorted; + } + + /** + * Recursive function that implements Dark Sort. + * + * @param arr the array to be sorted + * @param left the starting index of the array + * @param right the ending index of the array + */ + private > void doDarkSort(T[] arr, int left, int right) { + if (left >= right) { + return; + } + + // Divide the array into two parts using a random pivot + int pivotIndex = partition(arr, left, right); + + // Recursively sort both halves + doDarkSort(arr, left, pivotIndex - 1); + doDarkSort(arr, pivotIndex + 1, right); + } + + /** + * Partitions the array into two halves around a pivot element. + * + * @param arr the array to partition + * @param left the starting index + * @param right the ending index + * @return the index of the pivot element + */ + private > int partition(T[] arr, int left, int right) { + T pivot = arr[right]; // Choosing the last element as pivot + int i = left - 1; + + for (int j = left; j < right; j++) { + if (less(arr[j], pivot)) { + i++; + swap(arr, i, j); + } + } + swap(arr, i + 1, right); + return i + 1; + } + + /** + * Swaps two elements in the array. + * + * @param arr the array + * @param i the index of the first element + * @param j the index of the second element + */ + private > void swap(T[] arr, int i, int j) { + T temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; + } +} diff --git a/src/test/java/com/thealgorithms/sorts/DarkSortTest.java b/src/test/java/com/thealgorithms/sorts/DarkSortTest.java new file mode 100644 index 000000000000..5502a73a7f37 --- /dev/null +++ b/src/test/java/com/thealgorithms/sorts/DarkSortTest.java @@ -0,0 +1,63 @@ +package com.thealgorithms.sorts; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +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 testSortWithStrings() { + String[] unsorted = {"zebra", "apple", "mango", "banana"}; + String[] expected = {"apple", "banana", "mango", "zebra"}; + + DarkSort darkSort = new DarkSort(); + String[] 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); + } +} From 647a5a0b25d7825602032ceb71239bf5c014e941 Mon Sep 17 00:00:00 2001 From: sahilkumarvalecha <106372522+sahilkumarvalecha@users.noreply.github.com> Date: Thu, 16 Jan 2025 14:35:30 +0500 Subject: [PATCH 2/8] Add test case for null input in DarkSortTest --- .../java/com/thealgorithms/sorts/DarkSort.java | 1 + .../java/com/thealgorithms/sorts/DarkSortTest.java | 14 +++++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/sorts/DarkSort.java b/src/main/java/com/thealgorithms/sorts/DarkSort.java index 434bb716c0d6..23f28e29eaa1 100644 --- a/src/main/java/com/thealgorithms/sorts/DarkSort.java +++ b/src/main/java/com/thealgorithms/sorts/DarkSort.java @@ -19,6 +19,7 @@ class DarkSort implements SortAlgorithm { * @return sorted array */ @Override + public > T[] sort(T[] unsorted) { if (unsorted == null || unsorted.length <= 1) { return unsorted; diff --git a/src/test/java/com/thealgorithms/sorts/DarkSortTest.java b/src/test/java/com/thealgorithms/sorts/DarkSortTest.java index 5502a73a7f37..10dde7baae56 100644 --- a/src/test/java/com/thealgorithms/sorts/DarkSortTest.java +++ b/src/test/java/com/thealgorithms/sorts/DarkSortTest.java @@ -2,7 +2,8 @@ import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertNull; class DarkSortTest { @@ -60,4 +61,15 @@ void testAlreadySortedArray() { 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"); + } + } From 17eca6bee88db6b5ca9bf12f06984cdb8e7df172 Mon Sep 17 00:00:00 2001 From: sahilkumarvalecha <106372522+sahilkumarvalecha@users.noreply.github.com> Date: Thu, 16 Jan 2025 14:39:33 +0500 Subject: [PATCH 3/8] Formatting error in darksort solved --- src/test/java/com/thealgorithms/sorts/DarkSortTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/sorts/DarkSortTest.java b/src/test/java/com/thealgorithms/sorts/DarkSortTest.java index 10dde7baae56..e4c47a779027 100644 --- a/src/test/java/com/thealgorithms/sorts/DarkSortTest.java +++ b/src/test/java/com/thealgorithms/sorts/DarkSortTest.java @@ -1,7 +1,6 @@ package com.thealgorithms.sorts; import org.junit.jupiter.api.Test; - import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertNull; From f171582b8c9b68d7547a80412f539f6d2e496c48 Mon Sep 17 00:00:00 2001 From: sahilkumarvalecha <106372522+sahilkumarvalecha@users.noreply.github.com> Date: Thu, 16 Jan 2025 14:50:55 +0500 Subject: [PATCH 4/8] Build Error Fixed --- src/test/java/com/thealgorithms/sorts/DarkSortTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/thealgorithms/sorts/DarkSortTest.java b/src/test/java/com/thealgorithms/sorts/DarkSortTest.java index e4c47a779027..9b3f070830da 100644 --- a/src/test/java/com/thealgorithms/sorts/DarkSortTest.java +++ b/src/test/java/com/thealgorithms/sorts/DarkSortTest.java @@ -1,6 +1,7 @@ package com.thealgorithms.sorts; import org.junit.jupiter.api.Test; + import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertNull; @@ -70,5 +71,4 @@ void testNullArray() { assertNull(sorted, "Sorting a null array should return null"); } - -} +} \ No newline at end of file From 722cdea50f7d96049e738af73fe40518a1c78332 Mon Sep 17 00:00:00 2001 From: sahilkumarvalecha <106372522+sahilkumarvalecha@users.noreply.github.com> Date: Thu, 16 Jan 2025 14:57:20 +0500 Subject: [PATCH 5/8] Build and Cling Error Fixed --- src/test/java/com/thealgorithms/sorts/DarkSortTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/com/thealgorithms/sorts/DarkSortTest.java b/src/test/java/com/thealgorithms/sorts/DarkSortTest.java index 9b3f070830da..d095a68641ae 100644 --- a/src/test/java/com/thealgorithms/sorts/DarkSortTest.java +++ b/src/test/java/com/thealgorithms/sorts/DarkSortTest.java @@ -1,10 +1,10 @@ package com.thealgorithms.sorts; -import org.junit.jupiter.api.Test; - import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertNull; +import org.junit.jupiter.api.Test; + class DarkSortTest { @Test @@ -71,4 +71,4 @@ void testNullArray() { assertNull(sorted, "Sorting a null array should return null"); } -} \ No newline at end of file +} From c59956b3ee11674ed2ea770b8e53c8b2ca6975f7 Mon Sep 17 00:00:00 2001 From: sahilkumarvalecha <106372522+sahilkumarvalecha@users.noreply.github.com> Date: Fri, 17 Jan 2025 16:45:44 +0500 Subject: [PATCH 6/8] Changes in Dark Sort --- .../com/thealgorithms/sorts/DarkSort.java | 82 ++++++++----------- .../com/thealgorithms/sorts/DarkSortTest.java | 37 ++++++--- 2 files changed, 59 insertions(+), 60 deletions(-) diff --git a/src/main/java/com/thealgorithms/sorts/DarkSort.java b/src/main/java/com/thealgorithms/sorts/DarkSort.java index 23f28e29eaa1..7105a8c92225 100644 --- a/src/main/java/com/thealgorithms/sorts/DarkSort.java +++ b/src/main/java/com/thealgorithms/sorts/DarkSort.java @@ -1,11 +1,10 @@ package com.thealgorithms.sorts; -import static com.thealgorithms.sorts.SortUtils.less; - /** - * Generic implementation of Dark Sort algorithm. - * Dark Sort is a conceptual sorting algorithm combining divide-and-conquer - * and randomized selection principles. + * 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 */ @@ -19,67 +18,52 @@ class DarkSort implements SortAlgorithm { * @return sorted array */ @Override - public > T[] sort(T[] unsorted) { if (unsorted == null || unsorted.length <= 1) { return unsorted; } - doDarkSort(unsorted, 0, unsorted.length - 1); - return unsorted; - } - /** - * Recursive function that implements Dark Sort. - * - * @param arr the array to be sorted - * @param left the starting index of the array - * @param right the ending index of the array - */ - private > void doDarkSort(T[] arr, int left, int right) { - if (left >= right) { - return; + // Dark Sort works only for integers, so we cast and check + if (!(unsorted instanceof Integer[])) { + throw new IllegalArgumentException("Dark Sort only supports Integer arrays."); } - // Divide the array into two parts using a random pivot - int pivotIndex = partition(arr, left, right); + Integer[] arr = (Integer[]) unsorted; + int max = findMax(arr); // Find the maximum value in the array - // Recursively sort both halves - doDarkSort(arr, left, pivotIndex - 1); - doDarkSort(arr, pivotIndex + 1, right); - } + // Create a temporary array for counting occurrences + int[] temp = new int[max + 1]; - /** - * Partitions the array into two halves around a pivot element. - * - * @param arr the array to partition - * @param left the starting index - * @param right the ending index - * @return the index of the pivot element - */ - private > int partition(T[] arr, int left, int right) { - T pivot = arr[right]; // Choosing the last element as pivot - int i = left - 1; + // Count occurrences of each element + for (int value : arr) { + temp[value]++; + } - for (int j = left; j < right; j++) { - if (less(arr[j], pivot)) { - i++; - swap(arr, i, j); + // Reconstruct the sorted array + int index = 0; + for (int i = 0; i < temp.length; i++) { + while (temp[i] > 0) { + arr[index++] = i; + temp[i]--; } } - swap(arr, i + 1, right); - return i + 1; + + return (T[]) arr; } /** - * Swaps two elements in the array. + * Helper method to find the maximum value in an array. * * @param arr the array - * @param i the index of the first element - * @param j the index of the second element + * @return the maximum value */ - private > void swap(T[] arr, int i, int j) { - T temp = arr[i]; - arr[i] = arr[j]; - arr[j] = temp; + private int findMax(Integer[] arr) { + int max = arr[0]; + for (int value : arr) { + if (value > max) { + max = value; + } + } + return max; } } diff --git a/src/test/java/com/thealgorithms/sorts/DarkSortTest.java b/src/test/java/com/thealgorithms/sorts/DarkSortTest.java index d095a68641ae..ff9913566137 100644 --- a/src/test/java/com/thealgorithms/sorts/DarkSortTest.java +++ b/src/test/java/com/thealgorithms/sorts/DarkSortTest.java @@ -1,6 +1,7 @@ package com.thealgorithms.sorts; import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertNull; import org.junit.jupiter.api.Test; @@ -18,17 +19,6 @@ void testSortWithIntegers() { assertArrayEquals(expected, sorted); } - @Test - void testSortWithStrings() { - String[] unsorted = {"zebra", "apple", "mango", "banana"}; - String[] expected = {"apple", "banana", "mango", "zebra"}; - - DarkSort darkSort = new DarkSort(); - String[] sorted = darkSort.sort(unsorted); - - assertArrayEquals(expected, sorted); - } - @Test void testEmptyArray() { Integer[] unsorted = {}; @@ -62,6 +52,17 @@ void testAlreadySortedArray() { 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; @@ -71,4 +72,18 @@ void testNullArray() { 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." + ); + } } From 9f46b4cd2ab98ea3f52ee0c9ae1fe5937e97c5a2 Mon Sep 17 00:00:00 2001 From: sahilkumarvalecha <106372522+sahilkumarvalecha@users.noreply.github.com> Date: Fri, 17 Jan 2025 16:48:55 +0500 Subject: [PATCH 7/8] Clang Format linter check resolved --- src/test/java/com/thealgorithms/sorts/DarkSortTest.java | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/test/java/com/thealgorithms/sorts/DarkSortTest.java b/src/test/java/com/thealgorithms/sorts/DarkSortTest.java index ff9913566137..c30cbb9a8742 100644 --- a/src/test/java/com/thealgorithms/sorts/DarkSortTest.java +++ b/src/test/java/com/thealgorithms/sorts/DarkSortTest.java @@ -1,8 +1,8 @@ package com.thealgorithms.sorts; import static org.junit.jupiter.api.Assertions.assertArrayEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import org.junit.jupiter.api.Test; @@ -80,10 +80,6 @@ void testNonIntegerArray() { DarkSort darkSort = new DarkSort(); // DarkSort should throw an IllegalArgumentException for non-integer arrays - assertThrows( - IllegalArgumentException.class, - () -> darkSort.sort(unsorted), - "DarkSort only supports Integer arrays." - ); + assertThrows(IllegalArgumentException.class, () -> darkSort.sort(unsorted), "DarkSort only supports Integer arrays."); } } From 755196f804f7171cf7fc4b410dfd6ffd7f3eadb4 Mon Sep 17 00:00:00 2001 From: sahilkumarvalecha <106372522+sahilkumarvalecha@users.noreply.github.com> Date: Sat, 18 Jan 2025 22:14:47 +0500 Subject: [PATCH 8/8] Accepting Only Integers In DarKSort --- .../com/thealgorithms/sorts/DarkSort.java | 22 +++++-------------- .../com/thealgorithms/sorts/DarkSortTest.java | 11 ---------- 2 files changed, 6 insertions(+), 27 deletions(-) diff --git a/src/main/java/com/thealgorithms/sorts/DarkSort.java b/src/main/java/com/thealgorithms/sorts/DarkSort.java index 7105a8c92225..4887d7d124ba 100644 --- a/src/main/java/com/thealgorithms/sorts/DarkSort.java +++ b/src/main/java/com/thealgorithms/sorts/DarkSort.java @@ -5,37 +5,27 @@ * * 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 { +class DarkSort { /** * Sorts the array using the Dark Sort algorithm. * * @param unsorted the array to be sorted - * @param Comparable class * @return sorted array */ - @Override - public > T[] sort(T[] unsorted) { + public Integer[] sort(Integer[] 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 + 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 : arr) { + for (int value : unsorted) { temp[value]++; } @@ -43,12 +33,12 @@ public > T[] sort(T[] unsorted) { int index = 0; for (int i = 0; i < temp.length; i++) { while (temp[i] > 0) { - arr[index++] = i; + unsorted[index++] = i; temp[i]--; } } - return (T[]) arr; + return unsorted; } /** diff --git a/src/test/java/com/thealgorithms/sorts/DarkSortTest.java b/src/test/java/com/thealgorithms/sorts/DarkSortTest.java index c30cbb9a8742..1df077e2ad74 100644 --- a/src/test/java/com/thealgorithms/sorts/DarkSortTest.java +++ b/src/test/java/com/thealgorithms/sorts/DarkSortTest.java @@ -2,7 +2,6 @@ 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; @@ -72,14 +71,4 @@ void testNullArray() { 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."); - } }