JavaScript sorting algorithm--heap sort

Source: Internet
Author: User

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">$ (document). Ready (function() {                         //var array_1 = [9,8,7,6,5,4,3,2,1];            varArray_1 = [4,5,3,1,2,0];            alert (array_1); /*Heapsort*/alert (Heapsort (array_1)); }    ); </script><style type= "Text/css" > * {padding:0; margin:0; }        body {padding:100px; Font-size:15px; }    </style><script type= "Text/javascript" >functionHeapsort (elements) {//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 compared 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 on the child node after swapping            if(Elements[pos] <Elements[child]) {Elements[pos]=Elements[child]; POS=Child ; Child= pos * 2 + 1; }            Else{               Break; } Elements[pos]=swap; }        }        //Build Heap        functionBuildheap (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 adjustmentsalert (elements); varSwap =Elements[i]; Elements[i]= Elements[0]; elements[0] =swap;            alert (elements); //Adjust the largest element to the top of the heapHeadadjust (Elements, 0, i);          alert (elements);        }} sort (elements); returnelements; }</script>

JavaScript sorting algorithm--heap sort

Related Article

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.