Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/main/java/com/thealgorithms/sorts/StalinSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.thealgorithms.sorts;

/**
* @author Anant Jain (https://github.com/anant-jain01)
* @see https://medium.com/@kaweendra/the-ultimate-sorting-algorithm-6513d6968420
*/
public class StalinSort implements SortAlgorithm {

public <T extends Comparable<T>> T[] sort(T[] 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[]) new Comparable[currentIndex + 1];
System.arraycopy(array, 0, result, 0, currentIndex + 1);

return result;
}

// Driver Program
public static void main(String[] args) {
// 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));
}
}