PHP Searching and Sorting Algorithm: Heap sort
Write a PHP program to sort a list of elements using Heap sort.
In computer science, heapsort (invented by J. W. J. Williams in 1964) is a comparison-based sorting algorithm. Heapsort can be thought of as an improved selection sort: like that algorithm, it divides its input into a sorted and an unsorted region, and it interactively shrinks the unsorted region by extracting the largest element and moving that to the sorted region. The improvement consists of the use of a heap data structure rather than a linear-time search to find the maximum. Although somewhat slower in practice on most machines than a well-implemented quicksort, it has the advantage of a more favorable worst-case O(n log n) runtime. Heapsort is an in-place algorithm, but it is not a stable sort.
A run of the heapsort algorithm sorting an array of randomly permuted values. In the first stage of the algorithm, the array elements are reordered to satisfy the heap property. Before the actual sorting takes place, the heap tree structure is shown briefly for illustration.
Animation credits : RolandH
Sample Solution :
PHP Code :
Output:
Original Array : 3, 0, 2, 5, -1, 4, 1 Sorted Array : -1, 0, 1, 2, 3, 4, 5
Explanation:
In the exercise above,
- Node Class: This class represents an element in the heap. It has a private property _i to store the key value of the node. The constructor initializes the node with a key value, and the "getKey()" method retrieves the key value of the node.
- Heap Class: This class implements the heap data structure. It has private properties 'heap_Array' to store heap elements and 'currentSize' to keep track of the current size of the heap. The constructor initializes the heap by initializing the heap array and setting the current size to 0. It also contains methods for removing the item with the maximum key from the heap ("remove()"), restoring the heap property by shifting down ("bubbleDown()"), inserting a node at a specific index ("insertAt()"), getting the current size of the heap ("getSize()"), and returning heap elements as an array ("asArray()").
- heapsort Function: This function performs heap sort on a given heap object. It first sifts all nodes in the heap except the lowest level to restore the heap property. Then, it sorts the heap by repeatedly removing the maximum element from the heap and inserting it into the sorted array. Finally, it returns a sorted array.
- Input Array and Sorting: An input array of integers is provided, and each integer is inserted into a heap as a node. The heap is then sorted using the heapsort function, and the sorted array is printed.
Flowchart :


PHP Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a PHP program to sort a list of elements using Quick sort.
Next: Write a PHP program to sort a list of elements using Insertion sort.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics