Thanos Sort
The hardest choices require the strongest wills. This algorithm faces the reality of a disordered array and enacts a radical solution. It is not about rearranging elements, but about achieving a state of perfect balance through reduction. The process is random, dispassionate, and fair to all elements, regardless of their value. When it's done, half the data will still exist. A small price to pay for salvation.
Demo
20 elements • 4x Speed
No Data
Did you know?
- This algorithm asks the question: "What did it cost?" The answer is "Everything." (or, more accurately, about of the data).
- While this specific implementation is due to memory shifting, a highly optimized version using a linear filter scan could theoretically reach .
- If the array reduces to a single element or becomes empty, it is considered perfectly balanced.
How it Works
-
Scan the array to check if it is currently sorted.
-
If the array is sorted, the work is done.
-
If disorder is found... Snap.
-
The Snap selects half of the current elements at random.
-
Delete the selected elements from the array instantly.
-
Repeat this process until balance is achieved, or the array is reduced to a single, trivial element.
Complexity Analysis
Advantages
- Decisive Action: Runs in linear time , as the problem size is halved with each pass. Faster than any hero can manage.
- Inevitable Success: It will always produce a sorted array, eventually.
- Perfectly Balanced: As all things should be.
Disadvantages
- The Cost: You could not live with your own failure. Where did that bring you? Back to me. This algorithm is lossy and will permanently delete data.
- Unpredictable Survivors: "You will never know" which elements will survive the snap.
- A Mercy, Not a Permutation: It violates the fundamental contract of sorting—to preserve the original set of elements.
Implementation
function thanosSort(arr) {
const isSorted = (a) => {
for(let i = 0; i < a.length - 1; i++) {
if(a[i] > a[i+1]) return false;
}
return true;
};
while (!isSorted(arr)) {
// Determine how many to remove
const countToRemove = Math.floor(arr.length / 2);
for (let i = 0; i < countToRemove; i++) {
// Pick a random element to sacrifice for the greater good
const targetIndex = Math.floor(Math.random() * arr.length);
// Delete the chosen element
arr.splice(targetIndex, 1);
}
}
return arr;
}