Detailed explanation of the Javascript heap Sorting Algorithm and javascript heap sorting
Heap sorting is divided into two processes:
1. Create a heap.
A heap is essentially a Complete Binary Tree. It must satisfy the following requirements: the keywords of any non-leaf node in the tree are not greater than (or less than) the keywords of its left and right children (if any) nodes.
The heap is divided into two types: large and small heap. The large heap is used in ascending order, and the small heap is used in descending order.
If it is a large root heap, adjust the node with the maximum value to the heap root through the adjustment function.
2. save the heap root to the end, and call the adjustment function for the remaining sequence. After the adjustment, save the maximum heel to the end-1 (-1,-2 ,..., -I), then adjust the remaining sequence, and repeat the process until the sorting is completed.
Copy codeThe Code is as follows:
// Adjust the Function
Function headAdjust (elements, pos, len ){
// Save the current node Value
Var swap = elements [pos];
// Locate the child node on the left of the current node
Var child = pos * 2 + 1;
// Recursion until no subnode exists
While (child <len ){
// If the current node has a child node on the right and the right child node is large, use the right child node
// Compare with the current node
If (child + 1 <len & elements [child] <elements [child + 1]) {
Child + = 1;
}
// Compare the current node with the largest subnode. If the value is smaller than the value, the value is exchanged. After the switch, the current node is located.
// On the subnode
If (elements [pos] <elements [child]) {
Elements [pos] = elements [child];
Pos = child;
Child = pos * 2 + 1;
}
Else {
Break;
}
Elements [pos] = swap;
}
}
// Build the heap
Function buildHeap (elements ){
// Start from the last node with a subnode and compare the node with its subnode,
// Swap the largest number with this node, and then perform the same exchange processing on the forward node in sequence,
// Until a large top heap is built (the top is in ascending order and the top is in descending order)
For (var I = elements. length/2; I> = 0; I --){
HeadAdjust (elements, I, elements. length );
}
}
Function sort (elements ){
// Build the heap
BuildHeap (elements );
// Start from the end of the sequence.
For (var I = elements. length-1; I> 0; I --){
// The heap top is always the largest element. Therefore, the heap top and tail elements are exchanged
// The maximum element is stored at the end and is not involved in subsequent adjustments.
Var swap = elements [I];
Elements [I] = elements [0];
Elements [0] = swap;
// Adjust the 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: preferably: O (nlog2n), worst: O (nlog2n), average: O (nlog2n ).
Space complexity: O (1 ).
Stability: unstable