-
Notifications
You must be signed in to change notification settings - Fork 20k
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
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1826719
Adding Dark Sort
sahilkumarvalecha 647a5a0
Add test case for null input in DarkSortTest
sahilkumarvalecha 17eca6b
Formatting error in darksort solved
sahilkumarvalecha f171582
Build Error Fixed
sahilkumarvalecha 722cdea
Build and Cling Error Fixed
sahilkumarvalecha c59956b
Changes in Dark Sort
sahilkumarvalecha 9f46b4c
Clang Format linter check resolved
sahilkumarvalecha 755196f
Accepting Only Integers In DarKSort
sahilkumarvalecha File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
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 <T> Comparable class | ||
* @return sorted array | ||
*/ | ||
@Override | ||
|
||
public <T extends Comparable<T>> T[] sort(T[] unsorted) { | ||
siriak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 <T extends Comparable<T>> 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 <T extends Comparable<T>> 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 <T extends Comparable<T>> void swap(T[] arr, int i, int j) { | ||
T temp = arr[i]; | ||
arr[i] = arr[j]; | ||
arr[j] = temp; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.thealgorithms.sorts; | ||
|
||
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 | ||
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); | ||
} | ||
|
||
@Test | ||
void testNullArray() { | ||
Integer[] unsorted = null; | ||
|
||
DarkSort darkSort = new DarkSort(); | ||
Integer[] sorted = darkSort.sort(unsorted); | ||
|
||
assertNull(sorted, "Sorting a null array should return null"); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.