JavaScript heap sorting algorithm-basic knowledge

Source: Internet
Author: User

Heap sorting is divided into two processes:

1. Build a heap.

The heap is essentially a complete binary tree, which must be satisfied that none of the nodes in the tree are not more than (or less than) the key word for its left and right child (if any) node.

The heap is divided into: Dagen and small Gan, ascending sort uses large root heap, descending sort uses small Gan.

If the heap is large, the node with the largest value is adjusted to the heap root by adjusting the function.

2. Save the heap root in the tail, and call the adjustment function on the remaining sequence, after the adjustment is finished, the maximum is kept in the tail-1 ( -1,-2,...,-i), then the remaining sequence is adjusted, and the process is repeated until the sorting is completed.

Copy Code code as follows:

Adjust function
function Headadjust (elements, POS, Len) {
Save the current node value
var swap = Elements[pos];
Navigate to the child node to the left of the current node
var child = pos * 2 + 1;
Recursive until no child nodes are present
while (Child < Len) {
If the current node has a child node to the right, and the right child node is larger, use the right child node
Compared to the current node
if (child + 1 < len && Elements[child] < Elements[child + 1]) {
Child = 1;
}
Compare the current node and the largest child node, and then exchange the value, and then position the current node
On the Applies node
if (Elements[pos] < Elements[child]) {
Elements[pos] = Elements[child];
pos = child;
Child = pos * 2 + 1;
}
else{
Break
}
Elements[pos] = swap;
}
}
Build heap
function Buildheap (elements) {
Compares the node with its child nodes, starting with the last node that owns the child node.
The maximum number is exchanged with the node, exchanged, and then the same Exchange process is performed in the forward node,
Until you build a large top heap (ascending to Big top, descending to small top)
for (var i=elements.length/2; i>=0; i--) {
Headadjust (elements, I, elements.length);
}
}
function sort (elements) {
Build heap
Buildheap (elements);
To adjust from the tail of a series
for (var i=elements.length-1; i>0; i--) {
The top of the heap is always the largest element, so the heap top and tail elements are exchanged to
The maximum element is saved in the tail and does not participate in the following adjustments
var swap = elements[i];
Elements[i] = elements[0];
Elements[0] = swap;
Adjust the maximum) element to the top of the heap
Headadjust (elements, 0, i);
}
}
var elements = [3, 1, 5, 7, 2, 4, 9, 6, 10, 8];
Console.log (' before: ' + elements);
Sort (elements);
Console.log (' after: ' + elements);

Efficiency:

Time complexity: Best: O (nlog2n), Worst: O (nlog2n), average: O (nlog2n).

Complexity of Space: O (1).

Stability: Unstable

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.