-
Notifications
You must be signed in to change notification settings - Fork 20.5k
StalinSort.java #5738
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
+167
−0
Merged
StalinSort.java #5738
Changes from 52 commits
Commits
Show all changes
56 commits
Select commit
Hold shift + click to select a range
924b5de
Create StalinSort.java
anant-jain01 bed6b83
Create StalinSortTest.java
anant-jain01 ae3b9d1
Update pom.xml
anant-jain01 ca18ef8
Update pom.xml
anant-jain01 60cd02f
Update pom.xml
anant-jain01 ac7b48f
Update StalinSortTest.java
anant-jain01 e7f7e3b
Update StalinSortTest.java
anant-jain01 450b6c8
Update StalinSort.java
anant-jain01 92527fb
Update StalinSortTest.java
anant-jain01 cf2374d
Update StalinSortTest.java
anant-jain01 4eb8ce7
Update StalinSortTest.java
anant-jain01 3104b11
Update StalinSortTest.java
anant-jain01 efccc4a
Update StalinSortTest.java
anant-jain01 10f97f0
Update StalinSortTest.java
anant-jain01 9722f7f
Update StalinSortTest.java
anant-jain01 6acf621
Update StalinSort.java
anant-jain01 eebe41c
Update StalinSortTest.java
anant-jain01 d185e9c
Merge branch 'master' into master
anant-jain01 00a6f78
Update StalinSortTest.java
anant-jain01 4e42137
Update StalinSort.java
anant-jain01 a022b6a
Update pom.xml
anant-jain01 3d7b1b6
Update StalinSort.java
anant-jain01 e6f271a
Update StalinSortTest.java
anant-jain01 fceee06
Update pom.xml
anant-jain01 c650a9b
Update pom.xml
anant-jain01 b84af74
Update StalinSortTest.java
anant-jain01 d0b7113
Update StalinSort.java
anant-jain01 490b879
Update pom.xml
anant-jain01 08aae77
Update pom.xml
anant-jain01 9926906
Update StalinSortTest.java
anant-jain01 6cdebe2
Update StalinSort.java
anant-jain01 4e6a279
Update StalinSortTest.java
anant-jain01 c4982a0
Update pom.xml
anant-jain01 2f1a6ff
Update StalinSortTest.java
anant-jain01 9c1f40f
Update pom.xml
anant-jain01 6952bb1
Update pom.xml
anant-jain01 f740fde
Update pom.xml
anant-jain01 daf86c2
Update StalinSort.java
anant-jain01 c0ad91f
Update pom.xml
anant-jain01 dc274b6
Update StalinSortTest.java
anant-jain01 822dbe8
Update StalinSortTest.java
anant-jain01 9b485b7
Create AdaptiveMergeSort.java
anant-jain01 520792b
Create AdaptiveMergeSortTest.java
anant-jain01 51172f7
Update AdaptiveMergeSort.java
anant-jain01 8770502
Update AdaptiveMergeSort.java
anant-jain01 19f83d9
Update AdaptiveMergeSortTest.java
anant-jain01 31c386a
Update AdaptiveMergeSort.java
anant-jain01 428f96e
Update AdaptiveMergeSort.java
anant-jain01 0a550c1
Update AdaptiveMergeSort.java
anant-jain01 44c2856
Update AdaptiveMergeSort.java
anant-jain01 eacb7f2
Update AdaptiveMergeSort.java
anant-jain01 7cf4bdb
Update AdaptiveMergeSort.java
anant-jain01 be7d4ff
Update AdaptiveMergeSort.java
anant-jain01 bff1ecb
Update StalinSort.java
anant-jain01 f95b22e
Merge branch 'master' into master
anant-jain01 5ae799a
Merge branch 'master' into master
siriak 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
53 changes: 53 additions & 0 deletions
53
src/main/java/com/thealgorithms/sorts/AdaptiveMergeSort.java
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,53 @@ | ||
package com.thealgorithms.sorts; | ||
|
||
public class AdaptiveMergeSort implements SortAlgorithm { | ||
|
||
@SuppressWarnings("unchecked") | ||
public <T extends Comparable<T>> T[] sort(T[] array) { | ||
if (array.length <= 1) { | ||
return array; | ||
} | ||
T[] aux = array.clone(); | ||
sort(array, aux, 0, array.length - 1); | ||
return array; | ||
} | ||
|
||
private <T extends Comparable<T>> void sort(T[] array, T[] aux, int low, int high) { | ||
if (low >= high) { | ||
return; | ||
} | ||
int mid = low + (high - low) / 2; | ||
sort(array, aux, low, mid); | ||
sort(array, aux, mid + 1, high); | ||
merge(array, aux, low, mid, high); | ||
} | ||
|
||
private <T extends Comparable<T>> void merge(T[] array, T[] aux, int low, int mid, int high) { | ||
System.arraycopy(array, low, aux, low, high - low + 1); | ||
|
||
int i = low; | ||
int j = mid + 1; | ||
for (int k = low; k <= high; k++) { | ||
if (i > mid) { | ||
array[k] = aux[j++]; | ||
} else if (j > high) { | ||
array[k] = aux[i++]; | ||
} else if (aux[j].compareTo(aux[i]) < 0) { | ||
array[k] = aux[j++]; | ||
} else { | ||
array[k] = aux[i++]; | ||
} | ||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
AdaptiveMergeSort adaptiveMergeSort = new AdaptiveMergeSort(); | ||
// Integer Input | ||
Integer[] integers = {4, 23, 6, 78, 1, 54, 231, 9, 12}; | ||
SortUtils.print(adaptiveMergeSort.sort(integers)); | ||
|
||
// String Input | ||
String[] strings = {"c", "a", "e", "b", "d"}; | ||
SortUtils.print(adaptiveMergeSort.sort(strings)); | ||
} | ||
} |
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,35 @@ | ||
package com.thealgorithms.sorts; | ||
|
||
public class StalinSort implements SortAlgorithm { | ||
@SuppressWarnings("unchecked") | ||
public <T extends Comparable<T>> T[] sort(T[] array) { | ||
if (array.length == 0) { | ||
return array; | ||
} | ||
|
||
int currentIndex = 0; | ||
for (int i = 1; i < array.length; i++) { | ||
if (array[i].compareTo(array[currentIndex]) >= 0) { | ||
currentIndex++; | ||
array[currentIndex] = array[i]; | ||
} | ||
} | ||
|
||
// Create a result array with sorted elements | ||
T[] result = (T[]) java.lang.reflect.Array.newInstance(array.getClass().getComponentType(), currentIndex + 1); | ||
System.arraycopy(array, 0, result, 0, currentIndex + 1); | ||
return result; | ||
} | ||
|
||
// Driver Program | ||
public static void main(String[] args) { | ||
anant-jain01 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
// Integer Input | ||
Integer[] integers = {4, 23, 6, 78, 1, 54, 231, 9, 12}; | ||
StalinSort stalinSort = new StalinSort(); | ||
// print a sorted array | ||
SortUtils.print(stalinSort.sort(integers)); | ||
// String Input | ||
String[] strings = {"c", "a", "e", "b", "d"}; | ||
SortUtils.print(stalinSort.sort(strings)); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/test/java/com/thealgorithms/sorts/AdaptiveMergeSortTest.java
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,53 @@ | ||
package com.thealgorithms.sorts; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertArrayEquals; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
public class AdaptiveMergeSortTest { | ||
|
||
@Test | ||
public void testSortIntegers() { | ||
AdaptiveMergeSort adaptiveMergeSort = new AdaptiveMergeSort(); | ||
Integer[] input = {4, 23, 6, 78, 1, 54, 231, 9, 12}; | ||
Integer[] expected = {1, 4, 6, 9, 12, 23, 54, 78, 231}; | ||
Integer[] result = adaptiveMergeSort.sort(input); | ||
assertArrayEquals(expected, result); | ||
} | ||
|
||
@Test | ||
public void testSortStrings() { | ||
AdaptiveMergeSort adaptiveMergeSort = new AdaptiveMergeSort(); | ||
String[] input = {"c", "a", "e", "b", "d"}; | ||
String[] expected = {"a", "b", "c", "d", "e"}; | ||
String[] result = adaptiveMergeSort.sort(input); | ||
assertArrayEquals(expected, result); | ||
} | ||
|
||
@Test | ||
public void testSortWithDuplicates() { | ||
AdaptiveMergeSort adaptiveMergeSort = new AdaptiveMergeSort(); | ||
Integer[] input = {1, 3, 2, 2, 5, 4}; | ||
Integer[] expected = {1, 2, 2, 3, 4, 5}; | ||
Integer[] result = adaptiveMergeSort.sort(input); | ||
assertArrayEquals(expected, result); | ||
} | ||
|
||
@Test | ||
public void testSortEmptyArray() { | ||
AdaptiveMergeSort adaptiveMergeSort = new AdaptiveMergeSort(); | ||
Integer[] input = {}; | ||
Integer[] expected = {}; | ||
Integer[] result = adaptiveMergeSort.sort(input); | ||
assertArrayEquals(expected, result); | ||
} | ||
|
||
@Test | ||
public void testSortSingleElement() { | ||
AdaptiveMergeSort adaptiveMergeSort = new AdaptiveMergeSort(); | ||
Integer[] input = {42}; | ||
Integer[] expected = {42}; | ||
Integer[] result = adaptiveMergeSort.sort(input); | ||
assertArrayEquals(expected, result); | ||
} | ||
} |
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,53 @@ | ||
package com.thealgorithms.sorts; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertArrayEquals; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
public class StalinSortTest { | ||
|
||
@Test | ||
public void testSortIntegers() { | ||
StalinSort stalinSort = new StalinSort(); | ||
Integer[] input = {4, 23, 6, 78, 1, 54, 231, 9, 12}; | ||
Integer[] expected = {4, 23, 78, 231}; | ||
Integer[] result = stalinSort.sort(input); | ||
assertArrayEquals(expected, result); | ||
} | ||
|
||
@Test | ||
public void testSortStrings() { | ||
StalinSort stalinSort = new StalinSort(); | ||
String[] input = {"c", "a", "e", "b", "d"}; | ||
String[] expected = {"c", "e"}; | ||
String[] result = stalinSort.sort(input); | ||
assertArrayEquals(expected, result); | ||
} | ||
|
||
@Test | ||
public void testSortWithDuplicates() { | ||
StalinSort stalinSort = new StalinSort(); | ||
Integer[] input = {1, 3, 2, 2, 5, 4}; | ||
Integer[] expected = {1, 3, 5}; | ||
Integer[] result = stalinSort.sort(input); | ||
assertArrayEquals(expected, result); | ||
} | ||
|
||
@Test | ||
public void testSortEmptyArray() { | ||
StalinSort stalinSort = new StalinSort(); | ||
Integer[] input = {}; | ||
Integer[] expected = {}; | ||
Integer[] result = stalinSort.sort(input); | ||
assertArrayEquals(expected, result); | ||
} | ||
|
||
@Test | ||
public void testSortSingleElement() { | ||
StalinSort stalinSort = new StalinSort(); | ||
Integer[] input = {42}; | ||
Integer[] expected = {42}; | ||
Integer[] result = stalinSort.sort(input); | ||
assertArrayEquals(expected, result); | ||
} | ||
} |
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.