The Ultimate Sorting Algorithm

Salitha Kaweendra
3 min readJan 22, 2024

Greetings, comrades ! Today, we’ll take a look at sorting algorithms, but with a twist that resembles the Soviet era’s iron-fist approach. Brace yourselves as we dive head first into the world of the Stalin sort.

The Dictator of Sorting:

Imagine a sorting algorithm that channels the spirit of Stalin, where elements that dare to disrupt the order are ruthlessly removed from the list and sent to the Gulag. Behold the Stalin sort — The Hammer and Sickle of Sorting Algorithms.


def stalin_sort(arr):
sorted_arr = [arr[0]] # The first element always makes the cut 🥹
for i in range(1, len(arr)):
if arr[i] >= sorted_arr[-1]:
sorted_arr.append(arr[i])
return sorted_arr

In the code above, we create a function that mirrors Stalin’s strict regime. Elements that are not in ascending order are removed from the list, leaving only those deemed worthy to be part of the final, orderly list.

The Iron Fist of Efficiency:

Stalin sort claims to run O(n) time but beware! The final list may end up shorter than expected. Take a moment to think about the fate of elements that don’t comply and are sent to the gulag. But don’t worry, even though the final list is a bit shorter than we expected, it is always sorted and in ascending order. Because order is what matters the most. It is so efficient only a madman will overlook it when sorting an unorderly rebellious list.

unruly_list = [1, 2, 5, 3, 6, 4, 10]
sorted_result = stalin_sort(unruly_list)
print(sorted_result) # Output: [1, 2, 5, 6, 10]

In the example above, elements 3 and 4 didn’t make the cut and were promptly exiled from the list and sent to a place where they would not be able to disrupt the order ever again. They might want to rethink their rebellious ways!

The Plot Twist — Longest Increasing Subsequence:

Surprise, comrades! The Stalin sort is not just a joke, it has a serious alter ego known as the “Longest Increasing Subsequence” (LIS). In the world of arrays, it’s like finding the most loyal followers of Stalin. Those who stand in increasing order of greatness are considered to be the most loyal followers.


def lis(arr):
n = len(arr)
lis_length = [1] * n

for i in range(1, n):
for j in range(0, i):
if arr[i] > arr[j] and lis_length[i] < lis_length[j] + 1:
lis_length[i] = lis_length[j] + 1

return max(lis_length)

In this LIS code snippet, the elements are evaluated for their allegiance to the increasing order cause. It’s a more serious business than Stalin sort, but imagine it as a Soviet spy operation within arrays to find the most loyal followers and remove the unorderly and rebellious elements from the list.

Conclusion:

As we wrap up our exploration of the Stalin sort and its surprise alter ego, the Longest Increasing Subsequence, let us consider its deeper message. In the world of code, the promise of efficiency can sometimes overwhelm the need to take into account a problem’s specific requirements. The Stalin sort, as amusing and efficient as it may be, serves as a reminder that the most efficient algorithm is not always the best choice for every situation.

--

--