Heap sort by selection

One of the most unusual array sorting algorithms is HeapSort, which is based on a selection sort algorithm, uses a heap data structure to quickly find the maximum element, and a way to store a binary tree in a linear array. Let's deal with everything in order.





Sort by selection

The operation of the synchrophasotron of the sorting algorithm is based on the principle of finding the maximum element in the unsorted part of the array and transferring it to the sorted part. 





, . - . 





, , 1. 





, N . , :





(2*N  + 1) + (2*(N - 1) + 1) + … + (2*K + 1) + … + 2*1 + 1 = 2(N*N - N)/2 + N = N*N







, - O(N^2).





.





. - ? , , «» «». , . , - 1 .





, :

















, . , , , . .





P = (x - 1) / 2







L = 2x + 1







R = 2x + 2







: . 





 





void heapify(int root, int size)



 





, , . - .





?





, , «» . -- .





- , , .





, . heapify , , - , , .





heapify()



. . 





HeapSort





? - . N/2 . , , log 2 N. 





, - (N log N) / 2





. . , . 1, , .





«» -- , , . log 2 K , K - .





N , , !





O(N log 2 N).





- - «» , .





" ". 20 - , () . . . - .





.








All Articles