Implementing Heapsort Algorithm in JavaScript

JavaScriptJavaScriptBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will explore the implementation of the heapsort algorithm in JavaScript. Heapsort is a comparison-based sorting algorithm that works by dividing an array into a sorted and an unsorted region, and iteratively shrinking the unsorted region by extracting the largest element and moving that to the sorted region. Through this lab, you will gain a deeper understanding of how the heapsort algorithm works and how to implement it using recursion and closures in JavaScript.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL javascript(("`JavaScript`")) -.-> javascript/BasicConceptsGroup(["`Basic Concepts`"]) javascript(("`JavaScript`")) -.-> javascript/AdvancedConceptsGroup(["`Advanced Concepts`"]) javascript/BasicConceptsGroup -.-> javascript/variables("`Variables`") javascript/BasicConceptsGroup -.-> javascript/data_types("`Data Types`") javascript/BasicConceptsGroup -.-> javascript/arith_ops("`Arithmetic Operators`") javascript/BasicConceptsGroup -.-> javascript/comp_ops("`Comparison Operators`") javascript/BasicConceptsGroup -.-> javascript/cond_stmts("`Conditional Statements`") javascript/BasicConceptsGroup -.-> javascript/loops("`Loops`") javascript/BasicConceptsGroup -.-> javascript/array_methods("`Array Methods`") javascript/AdvancedConceptsGroup -.-> javascript/spread_rest("`Spread and Rest Operators`") subgraph Lab Skills javascript/variables -.-> lab-28374{{"`Implementing Heapsort Algorithm in JavaScript`"}} javascript/data_types -.-> lab-28374{{"`Implementing Heapsort Algorithm in JavaScript`"}} javascript/arith_ops -.-> lab-28374{{"`Implementing Heapsort Algorithm in JavaScript`"}} javascript/comp_ops -.-> lab-28374{{"`Implementing Heapsort Algorithm in JavaScript`"}} javascript/cond_stmts -.-> lab-28374{{"`Implementing Heapsort Algorithm in JavaScript`"}} javascript/loops -.-> lab-28374{{"`Implementing Heapsort Algorithm in JavaScript`"}} javascript/array_methods -.-> lab-28374{{"`Implementing Heapsort Algorithm in JavaScript`"}} javascript/spread_rest -.-> lab-28374{{"`Implementing Heapsort Algorithm in JavaScript`"}} end

Heap Sort Algorithm

To practice coding, open the Terminal/SSH and type 'node'. The following algorithm sorts an array of numbers using the heapsort algorithm. Follow these steps:

  • Use recursion in the function.
  • Use the spread operator (...) to clone the original array, arr.
  • Use closures to declare a variable, l, and a function heapify.
  • Use a for loop and Math.floor() in combination with heapify to create a max heap from the array.
  • Use a for loop to repeatedly narrow down the considered range, using heapify and swapping values as necessary to sort the cloned array.
const heapsort = (arr) => {
  const a = [...arr];
  let l = a.length;
  const heapify = (a, i) => {
    const left = 2 * i + 1;
    const right = 2 * i + 2;
    let max = i;
    if (left < l && a[left] > a[max]) max = left;
    if (right < l && a[right] > a[max]) max = right;
    if (max !== i) {
      [a[max], a[i]] = [a[i], a[max]];
      heapify(a, max);
    }
  };
  for (let i = Math.floor(l / 2); i >= 0; i -= 1) heapify(a, i);
  for (let i = a.length - 1; i > 0; i--) {
    [a[0], a[i]] = [a[i], a[0]];
    l--;
    heapify(a, 0);
  }
  return a;
};

For example:

heapsort([6, 3, 4, 1]); // [1, 3, 4, 6]

Summary

Congratulations! You have completed the Heap Sort lab. You can practice more labs in LabEx to improve your skills.

Other JavaScript Tutorials you may like