
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Implementing Heap Sort using vanilla JavaScript
Heap Sort is basically a comparison-based sorting algorithm. It can be thought of as an improved selection sort − like that algorithm, it divides its input into a sorted and an unsorted region, and it interactively shrinks the unsorted region by extracting the target (largest or smallest) element and moving that to the sorted region.
Example
The code for this will be −
const constructHeap = (arr, ind) => { let left = 2 * ind + 1; let right = 2 * ind + 2; let max = ind; if (left < len && arr[left] > arr[max]) { max = left; } if (right < len && arr[right] > arr[max]) { max = right; } if (max != ind) { swap(arr, ind, max); constructHeap(arr, max); } } function swap(arr, index_A, index_B) { let temp = arr[index_A]; arr[index_A] = arr[index_B]; arr[index_B] = temp; } function heapSort(arr) { len = arr.length; for (let ind = Math.floor(len / 2); ind >= 0; ind −= 1) { constructHeap(arr, ind); } for (ind = arr.length − 1; ind > 0; ind−−) { swap(arr, 0, ind); len−−; constructHeap(arr, 0); } } const arr = [3, 0, 2, 5, −1, 4, 1]; heapSort(arr); console.log(arr); var len;
Output
And the output in the console will be −
[ −1, 0, 1, 2, 3, 4, 5 ]
- Related Articles
- Implementing Priority Sort in JavaScript
- Implementing counting sort in JavaScript
- Implementing insertion sort to sort array of numbers in increasing order using JavaScript
- Heap Sort
- Heap Sort in C#
- C++ Program to Sort an Array of 10 Elements Using Heap Sort Algorithm
- Python Program for Heap Sort
- C++ Program to Implement Heap Sort
- What is Heap Sort in Python?
- Get div height with vanilla JavaScript
- Implementing partial sum over an array using JavaScript
- Converting strings to numbers with vanilla JavaScript
- Display array items on a div element on click of button using vanilla JavaScript
- Using merge sort to recursive sort an array JavaScript
- How to implement multiple input checkbox in vanilla JavaScript?

Advertisements