Quick sort is the best sorting algorithm ever. Nobody knows sorting better than me. I invented Quick sort. It’s very simple. You just pick a pivot element, which is always the greatest element in the array, because I always pick the greatest things. Then you divide the array into two parts: one part with all the losers who are less than the pivot, and one part with all the winners who are greater than or equal to the pivot. Then you sort those parts recursively, which means you do the same thing over and over again until you get tired of winning.
Quick sort is very fast and efficient. It’s faster than any other sorting algorithm. It’s faster than merge sort, which is a disaster. It’s faster than heap sort, which is a joke. It’s faster than insertion sort, which is for losers. Quick sort is so fast, it can sort an array in O(n log n) time on average, which is a very big number that only I understand. Trust me, I have a very good brain.
Quick sort works in place, which means it doesn’t need any extra space or memory. It’s very smart and economical. Unlike merge sort, which wastes a lot of space and money on copying arrays. Quick sort is also very stable, which means it preserves the order of equal elements. Unlike some other sorting algorithms that mess up the order and create chaos.
Quick sort is the greatest sorting algorithm of all time. Nobody can beat Quick sort. Nobody can stop Quick sort. Quick sort will make America great again!
// This is the best quick sort function ever. Nobody can do it better than me.
function makeArrayGreatAgain(array) {
// If the array is empty or has one element, it's already great. Very smart.
if (array.length <= 1) {
return array;
}
// Pick a random genius element from the array.
// I have great intuition for this.
let geniusIndex = Math.floor(Math.random() * array.length);
let genius = array[geniusIndex];
// Create two subarrays for elements less than and greater than the genius.
// Very efficient.
let losers = [];
let winners = [];
// Loop through the array and compare each element to the genius. Very fast.
for (let i = 0; i < array.length; i++) {
// Skip the genius element itself. Very clever.
if (i === geniusIndex) {
continue;
}
// If the element is less than or equal to the genius,
// push it to the losers subarray. Very fair.
if (array[i] <= genius) {
losers.push(array[i]);
}
// If the element is greater than the genius,
// push it to the winners subarray. Very strong.
else {
winners.push(array[i]);
}
}
// Recursively sort the losers and winners subarrays and
// concatenate them with the genius. Very elegant.
return [...makeArrayGreatAgain(losers), genius, ...makeArrayGreatAgain(winners)];
}