A Beginner's Guide to Understanding Heap Sort: An Example in JavaScript.
Heap Sort is a comparison-based sorting algorithm that uses a heap data structure to sort an array. A heap is a complete binary tree, where the value of each parent node is greater than or equal to the values of its children (for a max-heap) or less than or equal to the values of its children (for a min-heap).
The basic idea behind Heap Sort is to first build a heap out of the input array, and then repeatedly remove the largest (or smallest, depending on whether it is a max-heap or min-heap) element from the heap and place it at the end of the array. This process continues until the heap is empty, resulting in a sorted array.
The example I provided earlier is an implementation of Heap Sort in JavaScript. The heapSort
function is the main sorting function, which takes an array as input. The first thing it does is build a heap out of the input array using the heapify
function. The heapify
function is called recursively to ensure that the heap property is maintained: that is, that the value of each parent node is greater than or equal to the values of its children.
Once the heap is built, the heapSort
function repeatedly removes the largest element (the root) from the heap and places it at the end of the array, reducing the size of the heap by one. This process continues until the heap is empty, resulting in a sorted array in descending order.
The time complexity of Heap Sort is O(n*log(n)), which is the same as that of Quick Sort. However, Heap Sort is not a stable sorting algorithm, meaning that equal elements may not retain their relative order in the sorted array. Additionally, Heap Sort is not an in-place sorting algorithm, as it requires additional memory to store the heap data structure.
Here is an example of Heap Sort implemented in JavaScript:
function heapSort(arr) {
// Build heap (rearrange array)
for (let i = Math.floor(arr.length / 2) - 1; i >= 0; i--) {
heapify(arr, arr.length, i);
}
// One by one extract an element from heap
for (let i = arr.length - 1; i > 0; i--) {
// Move current root to end
[arr[0], arr[i]] = [arr[i], arr[0]];
// call max heapify on the reduced heap
heapify(arr, i, 0);
}
return arr;
}
function heapify(arr, n, i) {
let largest = i; // Initialize largest as root
let l = 2 * i + 1; // left = 2*i + 1
let r = 2 * i + 2; // right = 2*i + 2
// If left child is larger than root
if (l < n && arr[l] > arr[largest]) {
largest = l;
}
// If right child is larger than largest so far
if (r < n && arr[r] > arr[largest]) {
largest = r;
}
// If largest is not root
if (largest !== i) {
[arr[i], arr[largest]] = [arr[largest], arr[i]];
// Recursively heapify the affected sub-tree
heapify(arr, n, largest);
}
}
console.log(heapSort([4, 10, 3, 5, 1])); // [1, 3, 4, 5, 10]
In this example, the heapSort
function first builds a heap out of the input array using the heapify
function. The heapify
function is called recursively to ensure that the heap property is maintained: that is, that the value of each parent node is greater than or equal to the values of its children.
Once the heap is built, the function repeatedly removes the largest element (the root) from the heap and places it at the end of the array, reducing the size of the heap by one. This process continues until the heap is empty, resulting in a sorted array in descending order.
Note: The above example is a basic implementation of Heap Sort. The time complexity of Heap Sort is O(n*log(n)) which is same as of Quick sort but it is not a stable sort algorithm and also it is not in place sort algorithm
Comments
Post a Comment