A New and Efficient Sorting Algorithm

Revision en10, by hitler_sort, 2025-07-16 22:53:07

As we all learned in kindergarten, you can easily sort an array in $$$O(n^{2})$$$ using well-known algorithms such as bubble sort, insertion sort, and such.

We took inspiration from Stalin Sort (SS for short) and improved the upper bound from $$$O(n^{2})$$$ to $$$O(n\sqrt{n}log(n))$$$ with an algorithm that we called Hitler Sort (HS).

In this blog we will describe the algorithm and prove the upper bound on sorting, breaking the previously known bound of $$$O(n^{2})$$$.

The algorithm: We will use the well-known $$$O(nlogn)$$$ time LIS/LDS algorithm to find the LIS and LDS of the input array $$$A$$$ of length $$$n$$$, by the Erdős–Szekeres theorem, $$$max(LIS,LDS)\ge \sqrt{n}$$$ and therfore, if we erase the maximal monotonic subseqence $$$S$$$ from the array, we will be left with an array $$$A\S$$$ of size at most $$$n-\sqrt{n}$$$ that we will sort recursively, lastly we can merge $$$S$$$ and $$$A\S$$$ with the classic merge algorithm in $$$O(n)$$$ time.

C++ Code
Python Code

We will now prove the time complexity of the algorithm. let us denote by $$$T(n)$$$ the running time of the Hitler-Sort algorithm. We notice that finding the LIS/LDS takes $$$O(nlogn)$$$ time, and that the merge operation takes $$$O(n)$$$ time, we further notice that we recursively sort an array of size at most $$$n-\sqrt{n}$$$. therefore we have $$$T(n)\le T(n-\sqrt{n}) + O(n) + O(nlogn) = T(n-\sqrt{n}) + O(nlogn)$$$. And therefore there exists some $$$c \gt 0$$$ such that for all $$$n$$$, $$$T(n) \le T(n-\sqrt{n})+cnlogn$$$. we will prove by induction that $$$T(n)\le cn\sqrt{n}logn$$$ for all $$$n$$$: $$$T(n)\le T(n-\sqrt{n})+cnlogn \le c(n-\sqrt{n})\sqrt{n-\sqrt{n}}log(n-\sqrt{n})+cnlogn \le c(n-\sqrt{n})\sqrt{n}logn+cnlogn = cn\sqrt{n}logn-c\sqrt{n}\sqrt{n}logn+cnlogn=cn\sqrt{n}logn$$$ $$$\implies$$$ $$$T(n)=O(n\sqrt{n}logn)$$$

We will now prove that in the worst-case, $$$T(n)=\Theta (n\sqrt{n}log(n))$$$.

Consider the array $$$A=[\sqrt{n},\sqrt{n}-1,\dots,1,2\sqrt{n},2\sqrt{n}-1,\dots,\sqrt{n}+1,\dots,n,n-1,\dots,n-\sqrt{n}+1]$$$ has $$$n$$$ elements, and is composed of $$$\sqrt{n}$$$ decreasing chains of length $$$\sqrt{n}$$$, and it's LIS and LDS are both $$$\sqrt{n}$$$. We notice that if we remove the LDS (any of them) in each recursive call of Hitler_Sort, the LDS remains $$$\sqrt{n}$$$ and the LIS decreases. we notice that finding the LDS takes $$$\Theta(n_{i}log(n_{i}))$$$ time in each recursive call, and therefore there exists some $$$c \gt 0$$$ such that finding the LDS takes more then $$$cn_{i}log(n_{i})$$$ time in each recursive call. Therefore we get the running time of Hitler_Sort on the array A: $$$T(n) \ge T(n-\sqrt{n})+cnlog(n) \ge T(n-\sqrt{n}-\sqrt{n})+c(n-\sqrt{n})log(n-\sqrt{n})+cnlog(n)$$$ $$$= T(n-2\sqrt{n})+c(n-\sqrt{n})log(n-\sqrt{n})+cnlog(n)$$$ $$$\ge \dots \ge \displaystyle\sum_{k=0}^{\sqrt{n}}c(n-k\sqrt{n})log(n-k\sqrt{n})$$$ $$$\ge \displaystyle\sum_{k=0}^{\frac{\sqrt{n}}{2}}c(n-k\sqrt{n})log(n-k\sqrt{n}) \ge \displaystyle\sum_{k=0}^{\frac{\sqrt{n}}{2}}c(\frac{n}{2})log(\frac{n}{2})$$$ $$$=c\frac{\sqrt{n}}{2}\frac{n}{2}log(\frac{n}{2})$$$ $$$=\frac{c}{4}(n\sqrt{n}log(n)-n\sqrt{n})$$$ $$$\ge \frac{c}{8}(n\sqrt{n}log(n)) = \Theta(n\sqrt{n}log(n)$$$

We proved an upper and lower bound of $$$cn\sqrt{n}log(n)$$$ on the running time of Hitler_Sort on the array $$$A$$$, proving that Hitler_Sort runs in $$$\Theta(n\sqrt{n}log(n))$$$ time worst-case.

In the best-case, on sorted or reversed arrays, Hitler_Sort runs in $$$\Theta(nlog(n))$$$ time and immediately stops.

The average-case analysis is very similar to the worst-case analysis using the fact that the expected value of $$$max(LDS,LIS)$$$ is $$$2\sqrt{n}(1+o(1))$$$, resulting in an expected runtime of $$$\Theta(n\sqrt{n}log(n))$$$.

We managed to improve the prevoiusly known bound of sorting in a comparison-based model from $$$O(n^{2})$$$ to $$$O(n\sqrt{n}log(n))$$$ in the worst case, and it remains an open problem whether this bound can be improved further.

Thank you for reading this far, we hope you enjoyed this blog and would love to hear comments and ideas.

danke und auf Wiedersehen!

Tags sorting, complexity, troll

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en10 English hitler_sort 2025-07-16 22:53:07 2 Tiny change: 'de">\n```c++\n#include' -> 'de">\n```c\n#include'
en9 English hitler_sort 2025-07-16 22:46:09 4 Tiny change: 't $n-\sqrt(n)$ that we ' -> 't $n-\sqrt{n}$ that we '
en8 English hitler_sort 2025-07-14 02:56:22 1 (published)
en7 English hitler_sort 2025-07-14 02:56:07 14441 (saved to drafts)
en6 English hitler_sort 2025-07-14 02:29:07 240 Tiny change: 't{n}logn)$ $\blacksquare$\n\nWe wi' -> 't{n}logn)$\n\nWe wi' (published)
en5 English hitler_sort 2025-07-14 02:23:58 825 Tiny change: 'rac{n}{2})\n\n\n' -> 'rac{n}{2})$\n\n\n'
en4 English hitler_sort 2025-07-14 02:05:00 690
en3 English hitler_sort 2025-07-14 01:51:56 471
en2 English hitler_sort 2025-07-13 22:50:38 316
en1 English hitler_sort 2025-07-13 22:34:20 12406 Initial revision (saved to drafts)