0
0
DSA Javascriptprogramming~20 mins

Heap Sort Algorithm in DSA Javascript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Heap Sort Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Heap Sort on a small array
What is the output of the following JavaScript code that uses heap sort to sort an array?
DSA Javascript
function heapify(arr, n, i) {
  let largest = i;
  let left = 2 * i + 1;
  let right = 2 * i + 2;

  if (left < n && arr[left] > arr[largest]) {
    largest = left;
  }

  if (right < n && arr[right] > arr[largest]) {
    largest = right;
  }

  if (largest !== i) {
    [arr[i], arr[largest]] = [arr[largest], arr[i]];
    heapify(arr, n, largest);
  }
}

function heapSort(arr) {
  let n = arr.length;

  for (let i = Math.floor(n / 2) - 1; i >= 0; i--) {
    heapify(arr, n, i);
  }

  for (let i = n - 1; i > 0; i--) {
    [arr[0], arr[i]] = [arr[i], arr[0]];
    heapify(arr, i, 0);
  }
}

let array = [4, 10, 3, 5, 1];
heapSort(array);
console.log(array.join(' -> ') + ' -> null');
A1 -> 4 -> 3 -> 5 -> 10 -> null
B10 -> 5 -> 4 -> 3 -> 1 -> null
C1 -> 3 -> 4 -> 5 -> 10 -> null
D5 -> 10 -> 4 -> 3 -> 1 -> null
Attempts:
2 left
💡 Hint
Heap sort sorts the array in ascending order by building a max heap first.
🧠 Conceptual
intermediate
1:30remaining
Understanding heapify in heap sort
In the heap sort algorithm, what is the main purpose of the heapify function?
ATo build a max heap by ensuring the subtree rooted at a given index satisfies the heap property
BTo swap the first and last elements of the array
CTo sort the array by comparing adjacent elements
DTo reverse the array after sorting
Attempts:
2 left
💡 Hint
Heapify fixes the heap property starting from a node downwards.
🔧 Debug
advanced
2:00remaining
Identify the error in this heap sort code snippet
What error will this JavaScript code produce when running heap sort?
DSA Javascript
function heapify(arr, n, i) {
  let largest = i;
  let left = 2 * i + 1;
  let right = 2 * i + 2;

  if (left < n && arr[left] < arr[largest]) {
    largest = left;
  }

  if (right < n && arr[right] < arr[largest]) {
    largest = right;
  }

  if (largest !== i) {
    [arr[i], arr[largest]] = [arr[largest], arr[i]];
    heapify(arr, n, largest);
  }
}

function heapSort(arr) {
  let n = arr.length;

  for (let i = Math.floor(n / 2) - 1; i >= 0; i--) {
    heapify(arr, n, i);
  }

  for (let i = n - 1; i > 0; i--) {
    [arr[0], arr[i]] = [arr[i], arr[0]];
    heapify(arr, i, 0);
  }
}

let array = [4, 10, 3, 5, 1];
heapSort(array);
console.log(array.join(' -> ') + ' -> null');
AThe array will be sorted in descending order instead of ascending
BSyntaxError due to incorrect swap syntax
CThe array will remain unsorted
DTypeError because heapify is called with wrong arguments
Attempts:
2 left
💡 Hint
Check the comparison operators in heapify function.
Predict Output
advanced
2:00remaining
Heap sort output with duplicate elements
What is the output of the following heap sort code when sorting an array with duplicates?
DSA Javascript
function heapify(arr, n, i) {
  let largest = i;
  let left = 2 * i + 1;
  let right = 2 * i + 2;

  if (left < n && arr[left] > arr[largest]) {
    largest = left;
  }

  if (right < n && arr[right] > arr[largest]) {
    largest = right;
  }

  if (largest !== i) {
    [arr[i], arr[largest]] = [arr[largest], arr[i]];
    heapify(arr, n, largest);
  }
}

function heapSort(arr) {
  let n = arr.length;

  for (let i = Math.floor(n / 2) - 1; i >= 0; i--) {
    heapify(arr, n, i);
  }

  for (let i = n - 1; i > 0; i--) {
    [arr[0], arr[i]] = [arr[i], arr[0]];
    heapify(arr, i, 0);
  }
}

let array = [4, 10, 3, 10, 5, 1];
heapSort(array);
console.log(array.join(' -> ') + ' -> null');
A10 -> 10 -> 5 -> 4 -> 3 -> 1 -> null
B1 -> 3 -> 4 -> 5 -> 10 -> 10 -> null
C1 -> 3 -> 4 -> 10 -> 5 -> 10 -> null
D1 -> 4 -> 3 -> 5 -> 10 -> 10 -> null
Attempts:
2 left
💡 Hint
Heap sort handles duplicates by sorting them in ascending order.
🚀 Application
expert
1:30remaining
Heap sort time complexity analysis
What is the time complexity of heap sort in the worst case and why?
AO(n^2) because heapify is called n times and each call takes O(n)
BO(n) because the array is sorted in a single pass
CO(log n) because heapify is a logarithmic operation
DO(n log n) because building the heap takes O(n) and each of the n removals takes O(log n)
Attempts:
2 left
💡 Hint
Consider the cost of building the heap and the cost of extracting elements.