Stalin-Sort

I just love this idea:

I     came up with a single pass O(n) sort algorithm I call Stalin-Sort. U iterate   down the list of elements checking if they are in order. Any element which is  out of order is eliminated. At the end u have a sorted list

But when you have an input such as [42,1,2,3,4,5,6] you just get
[42] instead of [1,2,3,4,5,6]. You can get a better
result if you use divide and conquer. And that is something Stalin
would do, don’t you think? So I implemented a recursive solution in
Java. It tries all possible results, which is not a single pass and therefore
defeats the purpose of having a fast O(n) algorithm. And my implementation isn’t even in-place. A new data structure has to be created. That’s why I also added an
implementation using a linked list, which is what Mathew describes.

The code is on pastebin: https://pastebin.com/rEmvZSiA

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s