


Detailed graphic explanation of Heap Sort heap sorting algorithm and JavaScript code implementation_Basic knowledge
1. I have to talk about binary trees
To understand the heap, you must first understand the binary tree. In computer science, a binary tree is a tree structure in which each node has at most two subtrees. Usually subtrees are called "left subtree" and "right subtree". Binary trees are often used to implement binary search trees and binary heaps.
Each node of a binary tree has at most two subtrees (there are no nodes with degree greater than 2). The subtrees of a binary tree are divided into left and right subtrees, and the order cannot be reversed. The i-th level of a binary tree has at most 2i - 1 nodes; a binary tree with depth k has at most 2k - 1 nodes; for any binary tree T, if the number of terminal nodes is n0, the number of nodes with degree 2 is n2, then n0 = n2 + 1.
Three main differences between trees and binary trees:
The number of nodes of a tree must be at least 1, while the number of nodes of a binary tree can be 0
There is no limit to the maximum degree of a node in a tree, while the maximum degree of a node in a binary tree is 2
The nodes of the tree are not divided into left and right, while the nodes of the binary tree are divided into left and right
Binary trees are divided into complete binary trees and full binary trees
Full binary tree: A tree with depth k and 2k - 1 nodes is called a full binary tree
(full binary tree with depth 3)
Complete binary tree: A binary tree with depth k and n nodes. If and only if each of its nodes corresponds to the nodes numbered 1 to n in the full binary tree with depth k, it is called a complete binary tree
(complete binary tree with depth 3)
2. What is a heap?
A heap (binary heap) can be regarded as a complete binary tree. An "excellent" property of a complete binary tree is that every level except the lowest level is full, which allows the heap to use arrays to Represented (ordinary binary trees are usually represented by linked lists as basic containers), each node corresponds to an element in the array.
As shown below, it is the relationship between a heap and an array
(Interrelationship between heap and array)
For a given subscript i of a node, the subscripts of the parent node and child node of this node can be easily calculated:
Parent(i) = floor(i/2), i’s parent node subscript
Left(i) = 2i, the subscript of the left child node of i
Right(i) = 2i + 1, i’s right child node subscript
Binary heaps are generally divided into two types: maximum heap and minimum heap.
Maximum heap:
The maximum element value in the max heap appears at the root node (top of the heap)
The element value of each parent node in the heap is greater than or equal to its child node (if it exists)
(maximum heap)
Minimum heap:
The smallest element value in the min-heap appears at the root node (top of the heap)
The element value of each parent node in the heap is less than or equal to its child node (if it exists)
(min heap)
3. Heap sorting principle
Heap sorting is to take out the maximum number at the top of the maximum heap, continue to adjust the remaining heap to the maximum heap, and take out the maximum number at the top of the heap again. This process continues until there is only one remaining number. Define the following operations on the heap:
Max-Heapify: Adjust the end child nodes of the heap so that the child nodes are always smaller than the parent node
Create a maximum heap (Build-Max-Heap): Reorder all data in the heap to make it a maximum heap
Heap-Sort: Remove the root node of the first data and perform a recursive operation of maximum heap adjustment
Before continuing with the following discussion, one thing that needs to be noted is that arrays are all Zero-Based, which means that our heap data structure model will change
(Zero-Based)
Correspondingly, several calculation formulas must also be adjusted accordingly:
Parent(i) = floor((i-1)/2), i’s parent node subscript
Left(i) = 2i + 1, i’s left child node subscript
Right(i) = 2(i + 1), i’s right child node subscript
The function of maximum heap adjustment (MAX-HEAPIFY) is to maintain the properties of the maximum heap and is the core subroutine for creating the maximum heap. The function process is as shown in the figure:
(Max-Heapify)
Since after one adjustment, the heap still violates the heap properties, recursive testing is required to make the entire heap satisfy the heap properties. This can be expressed in JavaScript as follows:
/** * 从 index 开始检查并保持最大堆性质 * * @array * * @index 检查的起始下标 * * @heapSize 堆大小 * **/ function maxHeapify(array, index, heapSize) { var iMax = index, iLeft = 2 * index + 1, iRight = 2 * (index + 1); if (iLeft < heapSize && array[index] < array[iLeft]) { iMax = iLeft; } if (iRight < heapSize && array[iMax] < array[iRight]) { iMax = iRight; } if (iMax != index) { swap(array, iMax, index); maxHeapify(array, iMax, heapSize); // 递归调整 } } function swap(array, i, j) { var temp = array[i]; array[i] = array[j]; array[j] = temp; }
Generally speaking, recursion is mainly used in the divide and conquer method, but divide and conquer is not required here. Moreover, recursive calls require pushing/clearing the stack, which has a slight performance disadvantage compared with iteration. Of course, following the 20/80 rule, this can be ignored. But if you feel that using recursion will make you feel uncomfortable, you can also use iteration, such as the following:
/** * 从 index 开始检查并保持最大堆性质 * * @array * * @index 检查的起始下标 * * @heapSize 堆大小 * **/ function maxHeapify(array, index, heapSize) { var iMax, iLeft, iRight; while (true) { iMax = index; iLeft = 2 * index + 1; iRight = 2 * (index + 1); if (iLeft < heapSize && array[index] < array[iLeft]) { iMax = iLeft; } if (iRight < heapSize && array[iMax] < array[iRight]) { iMax = iRight; } if (iMax != index) { swap(array, iMax, index); index = iMax; } else { break; } } } function swap(array, i, j) { var temp = array[i]; array[i] = array[j]; array[j] = temp; }
The function of creating a maximum heap (Build-Max-Heap) is to transform an array into a maximum heap. It accepts two parameters: array and heap size. Build-Max-Heap will call Max-Heapify from bottom to top. Transform the array and create a maximum heap. Because Max-Heapify can ensure that the nodes after the node with subscript i satisfy the maximum heap property, bottom-up calling Max-Heapify can maintain this property during the transformation process. If the number of elements in the maximum heap is n, then Build-Max-Heap starts from Parent(n) and calls Max-Heapify upwards. The process is as follows:
Described in JavaScript as follows:
function buildMaxHeap(array, heapSize) { var i, iParent = Math.floor((heapSize - 1) / 2); for (i = iParent; i >= 0; i--) { maxHeapify(array, i, heapSize); } }
Heap-Sort is the interface algorithm of heap sort. Heap-Sort first calls Build-Max-Heap to transform the array into a maximum heap, then exchanges the top and bottom elements of the heap, then raises the bottom, and finally Recall Max-Heapify to maintain the maximum heap properties. Since the top element of the heap must be the largest element in the heap, after one operation, the largest element existing in the heap is separated from the heap. After repeating n-1 times, the array is arranged. The whole process is as follows:
Described in JavaScript as follows:
function heapSort(array, heapSize) { buildMaxHeap(array, heapSize); for (int i = heapSize - 1; i > 0; i--) { swap(array, 0, i); maxHeapify(array, 0, i); } }
4.JavaScript language implementation
Finally, organize the above into the complete javascript code as follows:
function heapSort(array) { function swap(array, i, j) { var temp = array[i]; array[i] = array[j]; array[j] = temp; } function maxHeapify(array, index, heapSize) { var iMax, iLeft, iRight; while (true) { iMax = index; iLeft = 2 * index + 1; iRight = 2 * (index + 1); if (iLeft < heapSize && array[index] < array[iLeft]) { iMax = iLeft; } if (iRight < heapSize && array[iMax] < array[iRight]) { iMax = iRight; } if (iMax != index) { swap(array, iMax, index); index = iMax; } else { break; } } } function buildMaxHeap(array) { var i, iParent = Math.floor(array.length / 2) - 1; for (i = iParent; i >= 0; i--) { maxHeapify(array, i, array.length); } } function sort(array) { buildMaxHeap(array); for (var i = array.length - 1; i > 0; i--) { swap(array, 0, i); maxHeapify(array, 0, i); } return array; } return sort(array); }
5. Application of heap sort algorithm
(1) Algorithm performance/complexity
The time complexity of heap sort is very stable (we can see that it is not sensitive to the input data), and is O(n㏒n) complexity. The best case is the same as the worst case.
However, its space complexity varies from implementation to implementation. Two common complexities have been discussed above: O(n) and O(1). In line with the principle of saving space, I recommend the O(1) complexity method.
(2) Algorithm stability
Heap sorting involves a large number of screening and moving processes and is an unstable sorting algorithm.
(3) Algorithm applicable scenarios
Heap sorting will cause relatively large overhead in the process of establishing and adjusting the heap, and is not suitable when there are few elements. However, it is still a good choice when there are many elements. Especially when solving problems such as "the first n largest numbers", it is almost the preferred algorithm.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

Essential tools for stock analysis: Learn the steps to draw candle charts in PHP and JS. Specific code examples are required. With the rapid development of the Internet and technology, stock trading has become one of the important ways for many investors. Stock analysis is an important part of investor decision-making, and candle charts are widely used in technical analysis. Learning how to draw candle charts using PHP and JS will provide investors with more intuitive information to help them make better decisions. A candlestick chart is a technical chart that displays stock prices in the form of candlesticks. It shows the stock price

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest
