Heapsort is guaranteed to be O (N log N), which is much better than the worst case in Quicksort. Heapsort does not require more memory for another array to accommodate ordered data, as Mergesort requires. So why do commercial apps stick with Quicksort? What is Quicksort's feature compared to other implementations?
I checked the algorithms myself and saw that there really was something special about Quicksort. It works fast, much faster than Heap and Merge algorithms.
The secret to quick sorting is this: it almost does not make unnecessary permutations of elements. A swap is time consuming.
With Heapsort, even if all your data is already ordered, you are going to swap 100% of the elements to arrange the array.
Mergesort is even worse. You are going to write 100% of the elements to another array and write it back to the original, even if the data is already ordered.
With Quicksort, you do not change what is already ordered. If your data is completely streamlined, you won't change much! Although in the worst case there is a lot of noise due to a slight improvement in the choice of pivot, in addition to getting the first or last element of the array, it can be avoided. If you get a turn from an intermediate element between the first, last and middle element, it is enough to avoid the worst case.
What's better in quicksort is not the worst case, but the best case! At best, you make the same number of comparisons, fine, but you change almost nothing. In the average case, you change some of the elements, but not all elements, as in Heapsort and Mergesort. This is what gives Quicksort the best time. Less swap, more speed.
The following implementation in C # on my computer running in release mode is superior to Array.Sort in 3 seconds with an average rotation and 2 seconds with an improved rotation (yes, there are costs to getting a good rotation).
static void Main(string[] args) { int[] arrToSort = new int[100000000]; var r = new Random(); for (int i = 0; i < arrToSort.Length; i++) arrToSort[i] = r.Next(1, arrToSort.Length); Console.WriteLine("Press q to quick sort, s to Array.Sort"); while (true) { var k = Console.ReadKey(true); if (k.KeyChar == 'q') {