Heap Sort in Java

Here you will get program for heap sort in java.

Heap sort is a sorting algorithm that uses heap data structure. Its best, worst and average time complexity is O (n log n).

 

How heap sort algorithm works?

  • First we make max heap from given set of elements. In max heap each parent node is greater than or equal to its left and right child.
  • Now first node is swapped by last node and size of heap is reduced by 1. After each swap we check the heap satisfies max heap property or not.
  • The above step is repeated until the size of heap is more than 1.

Heap Sort in Java

Program for Heap Sort in Java

 

Output

Enter number of elements:
6

Enter the elements:
12 56 34 2 10 10

Sorted array is:
2 10 10 12 34 56

 

You can watch below video to learn about heap sort algorithm.

 

Comment below if you have any doubts related to above program for heap sort in java.

Leave a Reply

Your email address will not be published. Required fields are marked *