Tutorialspoint
Problem
Solution
Submissions

Binary Heap and Heap Sort Algorithm

Certification: Advanced Level Accuracy: 100% Submissions: 1 Points: 15

Write a Python program to implement a Binary Max Heap data structure and use it to perform Heap Sort. A binary heap is a complete binary tree where each parent node is greater than or equal to its children (max heap property). The program should include methods to insert elements, extract maximum, heapify, and perform heap sort on an array. The heap should be implemented using an array representation.

Example 1
  • Input: arr = [4, 10, 3, 5, 1]
  • Output: Sorted array: [1, 3, 4, 5, 10]
  • Explanation:
    Step 1: Build a max heap from the input array [4, 10, 3, 5, 1].
    Step 2: The max heap structure becomes [10, 5, 3, 4, 1] after heapification.
    Step 3: Extract maximum (10) and place it at the end, then heapify the remaining elements.
    Step 4: Repeat the process until all elements are in sorted order.
    Step 5: Final sorted array is [1, 3, 4, 5, 10] in ascending order.
Example 2
  • Input: arr = [12, 11, 13, 5, 6, 7]
  • Output: Sorted array: [5, 6, 7, 11, 12, 13]
  • Explanation:
    Step 1: The input array [12, 11, 13, 5, 6, 7] needs to be converted into a max heap.
    Step 2: After building max heap, the array becomes [13, 11, 12, 5, 6, 7].
    Step 3: The largest element (13) is at the root and gets swapped with the last element.
    Step 4: The heap size is reduced and heapify is called on the reduced heap.
    Step 5: This process continues until the entire array is sorted in ascending order.
Constraints
  • 1 ≤ arr.length ≤ 10^5
  • -10^9 ≤ arr[i] ≤ 10^9
  • The heap should be implemented using array representation
  • Time Complexity: O(n log n) for heap sort
  • Space Complexity: O(1) for heap sort (in-place sorting)
  • Building heap should take O(n) time complexity
ArraysHeapWiproeBay
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Use array representation where for index i, left child is at 2*i+1 and right child is at 2*i+2
  • Implement heapify function to maintain max heap property starting from a given node
  • Build heap by calling heapify on all non-leaf nodes from bottom to top
  • For heap sort, repeatedly extract maximum element and place it at the end of array
  • After each extraction, reduce heap size and call heapify on root to maintain heap property
  • Parent of node at index i is at index (i-1)//2
  • Use swap operations to maintain heap structure during heapify process

Steps to solve by this approach:

 Step 1: Implement helper methods to find parent, left child, and right child indices in the array representation of heap.
 Step 2: Create heapify method that maintains max heap property by comparing a node with its children and swapping if necessary.
 Step 3: Implement build_heap method that converts an arbitrary array into a max heap by calling heapify on all non-leaf nodes.
 Step 4: Start heapification from the last non-leaf node (at index n//2-1) and move upwards to the root.
 Step 5: For heap sort, first build the max heap from the input array using the build_heap method.
 Step 6: Repeatedly extract the maximum element (root) by swapping it with the last element of the current heap.
 Step 7: After each extraction, reduce the heap size and call heapify on the root to maintain heap property, continuing until array is sorted.

Submitted Code :