Select Sort---Heap sorting algorithm (JavaScript edition)

Source: Internet
Author: User

Heap sorting is divided into two processes:

1. Build the heap.

The heap is essentially a fully binary tree, and must be satisfied that the keywords of any non-leaf node in the tree are not greater than (or not less than) the key words of the child (if any) of the node.

Heap is divided into: Dagen and small Gan, ascending sort by large root heap, descending sort using small Gan.

If it is a large heap, adjust the function to resize the node with the largest value to the heap root.

2. Save the heap root at the tail, and call the adjustment function on the remaining sequence, after the adjustment is complete, then the maximum heel is saved at tail-1 ( -1,-2,...,-i), then the remaining sequence is adjusted, and the process is repeated until the sorting is complete.

The following code is executed in Nodejs

//Adjustment function
functionheadadjust (Elements, POS, len) {//Save the current node value varSwap =Elements[pos]; //navigates to the left child node of the current node varChild = pos * 2 + 1; //recursive until there are no child nodes while(Child <Len) { //If the current node has the right child node and the right child node is large, the right child node is used //compare with the current node if(Child + 1 < len && Elements[child] < Elements[child + 1]) { child+ = 1; } //compares the current node and the largest child node, is less than the value exchange, and then positions the current node after swapping //on the child node if(Elements[pos] <Elements[child]) {Elements[pos]=Elements[child]; POS=Child ; Child= pos * 2 + 1; } Else{ Break; } Elements[pos]=swap; }}
Build HeapfunctionBuildheap (elements) {//starting with the last node that owns the child node, compare the node with its child nodes, //The largest number is exchanged with the node, swapped, then the forward node in turn to the same interchange processing, //until a large top heap is built (ascending to a large top, descending to a small top) for(varI=ELEMENTS.LENGTH/2; i>=0; i--) {Headadjust (elements, I, elements.length); }}functionsort (elements) {//Build Heapbuildheap (elements); //adjust from the tail of the series for(varI=elements.length-1; i>0; i--){ //The top of the heap is always the largest element, so the stack top and tail elements are exchanged to //The largest element is saved at the tail and does not participate in the subsequent adjustments varSwap =Elements[i]; Elements[i]= Elements[0]; elements[0] =swap; //Adjust the maximum) element to the top of the heapHeadadjust (Elements, 0, i); }}varelements = [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).

Space complexity: O (1).

Stability: Unstable

Select Sort---heap sorting algorithm (JavaScript edition)

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.