|
1 | 1 | package com.thealgorithms.sorts;
|
2 | 2 |
|
3 |
| -import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
4 |
| - |
5 |
| -import org.junit.jupiter.api.Test; |
6 |
| - |
7 |
| -public class StalinSortTest { |
8 |
| - |
9 |
| - private StalinSort stalinSort = new StalinSort(); |
10 |
| - |
11 |
| - @Test |
12 |
| - public void stalinSortEmptyArray() { |
13 |
| - Integer[] inputArray = {}; |
14 |
| - Integer[] outputArray = stalinSort.sort(inputArray); |
15 |
| - Integer[] expectedOutput = {}; |
16 |
| - assertArrayEquals(outputArray, expectedOutput); |
17 |
| - } |
18 |
| - |
19 |
| - @Test |
20 |
| - public void stalinSortSingleIntegerArray() { |
21 |
| - Integer[] inputArray = {4}; |
22 |
| - Integer[] outputArray = stalinSort.sort(inputArray); |
23 |
| - Integer[] expectedOutput = {4}; |
24 |
| - assertArrayEquals(outputArray, expectedOutput); |
| 3 | +/** |
| 4 | + * @author Anant Jain (https://github.com/anant-jain01) |
| 5 | + * @see https://medium.com/@kaweendra/the-ultimate-sorting-algorithm-6513d6968420 |
| 6 | + */ |
| 7 | +public class StalinSort implements SortAlgorithm { |
| 8 | + |
| 9 | + public <T extends Comparable<T>> T[] sort(T[] array) { |
| 10 | + int currentIndex = 0; |
| 11 | + |
| 12 | + for (int i = 1; i < array.length; i++) { |
| 13 | + if (array[i].compareTo(array[currentIndex]) >= 0) { |
| 14 | + currentIndex++; |
| 15 | + array[currentIndex] = array[i]; |
| 16 | + } |
| 17 | + } |
| 18 | + |
| 19 | + // Create a result array with sorted elements |
| 20 | + T[] result = (T[]) new Comparable[currentIndex + 1]; |
| 21 | + System.arraycopy(array, 0, result, 0, currentIndex + 1); |
| 22 | + |
| 23 | + return result; |
25 | 24 | }
|
26 | 25 |
|
27 |
| - @Test |
28 |
| - public void stalinSortSingleStringArray() { |
29 |
| - String[] inputArray = {"s"}; |
30 |
| - String[] outputArray = stalinSort.sort(inputArray); |
31 |
| - String[] expectedOutput = {"s"}; |
32 |
| - assertArrayEquals(outputArray, expectedOutput); |
33 |
| - } |
| 26 | + // Driver Program |
| 27 | + public static void main(String[] args) { |
| 28 | + // Integer Input |
| 29 | + Integer[] integers = {4, 23, 6, 78, 1, 54, 231, 9, 12}; |
34 | 30 |
|
35 |
| - @Test |
36 |
| - public void stalinSortNonDuplicateIntegerArray() { |
37 |
| - Integer[] inputArray = {6, -1, 99, 27, -15, 23, -36}; |
38 |
| - Integer[] outputArray = stalinSort.sort(inputArray); |
39 |
| - Integer[] expectedOutput = {-36, -15, -1, 6, 23, 27, 99}; |
40 |
| - assertArrayEquals(outputArray, expectedOutput); |
41 |
| - } |
| 31 | + StalinSort stalinSort = new StalinSort(); |
42 | 32 |
|
43 |
| - @Test |
44 |
| - public void stalinSortDuplicateIntegerArray() { |
45 |
| - Integer[] inputArray = {6, -1, 27, -15, 23, 27, -36, 23}; |
46 |
| - Integer[] outputArray = stalinSort.sort(inputArray); |
47 |
| - Integer[] expectedOutput = {-36, -15, -1, 6, 23, 23, 27, 27}; |
48 |
| - assertArrayEquals(outputArray, expectedOutput); |
49 |
| - } |
| 33 | + // print a sorted array |
| 34 | + SortUtils.print(stalinSort.sort(integers)); |
50 | 35 |
|
51 |
| - @Test |
52 |
| - public void stalinSortNonDuplicateStringArray() { |
53 |
| - String[] inputArray = {"s", "b", "k", "a", "d", "c", "h"}; |
54 |
| - String[] outputArray = stalinSort.sort(inputArray); |
55 |
| - String[] expectedOutput = {"a", "b", "c", "d", "h", "k", "s"}; |
56 |
| - assertArrayEquals(outputArray, expectedOutput); |
57 |
| - } |
| 36 | + // String Input |
| 37 | + String[] strings = {"c", "a", "e", "b", "d"}; |
58 | 38 |
|
59 |
| - @Test |
60 |
| - public void stalinSortDuplicateStringArray() { |
61 |
| - String[] inputArray = {"s", "b", "d", "a", "d", "c", "h", "b"}; |
62 |
| - String[] outputArray = stalinSort.sort(inputArray); |
63 |
| - String[] expectedOutput = {"a", "b", "b", "c", "d", "d", "h", "s"}; |
64 |
| - assertArrayEquals(outputArray, expectedOutput); |
| 39 | + SortUtils.print(stalinSort.sort(strings)); |
65 | 40 | }
|
66 | 41 | }
|
0 commit comments