A New and Efficient Sorting Algorithm

Revision en4, by hitler_sort, 2025-07-14 02:05:00

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 (Nazi version)
C++ Code (clean version)
Python Code (clean)
C Code (Nazi version & optimized for time)
Story behind the algorithm (Nazi)

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)$$$ $$$\blacksquare$$$

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}^{\sqrt{n}/2}c(\frac{n}{2})log(\frac{n}{2})

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)