Output :

NOTE: I have shown two similar methods here maxHeapify(int[] array, int position)
and maxHeapifyForSort(int[] array, int position, int maxPos) but for sorting you will only need the second one. If your goal is to only create a max heap method one will do just fine. When we go for sort in each iteration we need to decouple indexes from the end so they are the maximum ones and hence we need a limit position to inform heapify function till where it should operate. What heapify does is that each element should have children which are smaller then itself.

NOTE : Heap sort is an in place algorithm (in-place algorithm is an algorithm which transforms input using no auxiliary data structure).

Applications : Heaps are generally used in priority queues ( min or max heap) so that you can retrieve the min or max element in O(1). Insertion will be log(N). It can be used to store realtime data. Lets say to keep score of top 3 people in a game given their scores are changing in realtime.