I have a question about heap sort. It state in an Algorithms book that A.heap-size<= A.length
I don’t understand the difference between the two. If an array represents a heap, why is there a possibility that A.heap-size
is less than A.length
. I know that A.heap-size
represents the number of elements inside a heap, so why is it not completely only equal to the number of items inside an array?
The heap size value is determined by the amount of memory available in the computer. Initial heap size is 1/64th of the computer's physical memory or reasonable minimum based on platform (whichever is larger) by default. The initial heap size can be overridden using -Xms.
1 : a collection of things thrown one on another : pile. 2 : a great number or large quantity : lot. heap. verb. heaped; heaping; heaps.
The tree is completely filled on all levels except possibly the lowest, which is filled from the left up to a point. An array A that represents a heap is an object with two attributes: length[A], which is the number of elements in the array, and heap-size[A], the number of elements in the heap stored within array A.
In computer science, a heap is a specialized tree-based data structure which is essentially an almost complete tree that satisfies the heap property: in a max heap, for any given node C, if P is a parent node of C, then the key (the value) of P is greater than or equal to the key of C.
Just to expand an answer. Read further that book.
A.heap_size
of an array, is that place where heap (max_heap or min_heap) structure elements will be placed. It makes sense in scope of sorting or queuing. You are right: this is the number of elements inside a heap, but it's equal to A.length
only at first iteration of heap sort.
At next iteration, after exchanging root of the max_heap tree (A[1]
) with A[i] = A[A.length]
(last element inside array A), the A[1]
element will be the last element of the A, and A.heap_sort
value will be decreased by 1 and max_heap structure should be max_heapified: A[Parent(i)] >= A[i]
, where Parent(i)
returns i/2 of heap tree.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With