Repository: TheAlgorithms/Java
Pull request #5738 (2024-10-12 18:25:55)

StalinSort.java
<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5738?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
Attention: Patch coverage is `64.70588%` with `6 lines` in your changes missing coverage. Please review.
> Project coverage is 63.92%. Comparing base [(`4a03f42`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/4a03f420616a6773e9a5cdc304b1681e96d945bd?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`822dbe8`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/822dbe829f94ab430765e69c1d9267e6eacb71e2?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

| [Files with missing lines](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5738?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) | Patch % | Lines |
|---|---|---|
| [.../main/java/com/thealgorithms/sorts/StalinSort.java](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5738?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Fcom%2Fthealgorithms%2Fsorts%2FStalinSort.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms#diff-c3JjL21haW4vamF2YS9jb20vdGhlYWxnb3JpdGhtcy9zb3J0cy9TdGFsaW5Tb3J0LmphdmE=) | 64.70% | [6 Missing :warning: ](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5738?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) |

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff              @@
##             master    #5738      +/-   ##
============================================
+ Coverage     63.91%   63.92%   +0.01%     
- Complexity     4167     4173       +6     
============================================
  Files           582      583       +1     
  Lines         16302    16319      +17     
  Branches       3148     3150       +2     
============================================
+ Hits          10419    10432      +13     
- Misses         5457     5462       +5     
+ Partials        426      425       -1     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5738?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

anant-jain01: @yanglbme @vil02 @BamaCharanChhandogi @alxkm @siriak 
please check and if you choose to merge
please use the hacktoberfest accepted tag
anant-jain01: @siriak the changes have ben made.
Please check it out...

Patch:
diff --git a/src/main/java/com/thealgorithms/sorts/AdaptiveMergeSort.java b/src/main/java/com/thealgorithms/sorts/AdaptiveMergeSort.java
new file mode 100644
index 00000000..2c71bae8
--- /dev/null
+++ b/src/main/java/com/thealgorithms/sorts/AdaptiveMergeSort.java
@@ -0,0 +1,40 @@
+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++];
+            }
+        }
+    }
+}
diff --git a/src/main/java/com/thealgorithms/sorts/StalinSort.java b/src/main/java/com/thealgorithms/sorts/StalinSort.java
new file mode 100644
index 00000000..5aaf530f
--- /dev/null
+++ b/src/main/java/com/thealgorithms/sorts/StalinSort.java
@@ -0,0 +1,21 @@
+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;
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/sorts/AdaptiveMergeSortTest.java b/src/test/java/com/thealgorithms/sorts/AdaptiveMergeSortTest.java
new file mode 100644
index 00000000..9d94b165
--- /dev/null
+++ b/src/test/java/com/thealgorithms/sorts/AdaptiveMergeSortTest.java
@@ -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);
+    }
+}
diff --git a/src/test/java/com/thealgorithms/sorts/StalinSortTest.java b/src/test/java/com/thealgorithms/sorts/StalinSortTest.java
new file mode 100644
index 00000000..ee9c6137
--- /dev/null
+++ b/src/test/java/com/thealgorithms/sorts/StalinSortTest.java
@@ -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);
+    }
+}