The JavaScript implementation of heap ordering

Source: Internet
Author: User

Thought

Sort the array as a binary tree:

    • Index 0 is the root node of the tree;
    • Root node, the parent index of the node with index N is (N-1)/2;
    • The index of the left child node of the node with index N is 2*n+1;
    • The index of the right child node of the node with index N is 2*n+2;
Code
function Heapsort(arr){   LetHeapSize= arr.length;  Buildheap(arr);//Constructs an array of heap structures where all nodes satisfy Arr[parent[i]] > Arr[i], so that the node with the largest value is swapped to the root node   while(heapsize> 1){ //*1    //In the current tree, swap the maximum value at the root node and the value of the last node so that the maximum is ranked at the last node, so that the maximum value is    ConstTemp=arr[0];arr[0]=Arr[heapsize-1];Arr[heapsize-1]=Temp;HeapSize--;///The last node in the tree is already lined up, so you don't have to think about this node anymore, so the size of the new tree is reduced by one    if(heapsize>1){      heapify(arr,HeapSize, 0);////The exchange operation above creates a new root node, the new root node is simply swapped with the last node, so the new root node does not meet the condition Arr[parent[i]]<arr[i], so the root node is again h    }  }}/** * @descriptionconstructs an array of heap structures where all nodes satisfy Arr[parent[i]] > Arr[i] * @param {Array}arr to sort arrays */function Buildheap(arr){  ConstHeapSize= arr.length;  ConstFirstheapifyindex= Math. Floor(heapsize/2-1);//From the last node in the second level of the tree with a child node (for the last node with a full two-tree being the penultimate layer), the heapify process begins. (heapsize/2-1) is the last node index that has a child node.    for( LetI=Firstheapifyindex;I>= 0;I--){//From 0 to Firstheapifyindex to carry out heapify processing, in order to put the largest node to the root node    heapify(arr,HeapSize,I;  }}/** * @descriptiontake the first heapsize node of the array arr as a tree, and replace the node where I is indexed to the child node until it satisfies the child node from the I down Arr[parent[i]]>=arr[i] * @param {*}arr TYPE array to sort * @param {*}heapsize The number of nodes in the array to be sorted that are to be processed as the previous number in the current tree, that is, the first heapsize point in the array to be sorted as a tree * @param {*}The index of the node in the heapsize length tree that is currently being substituted for the child node in the I TYPE number arr array */function heapify(arr,HeapSize,I{  ConstLeftindex=I* 2 + 1;index of the left child node of the node//index I  ConstRightindex=I* 2 + 2;the right child node index of the node of//Index I   LetBiggestvalueindex=I;  if(Leftindex<HeapSize&&Arr[leftindex]>Arr[biggestvalueindex]){    the maximum index for the//node is heapSize-1    ///NOTE: These two comparisons are compared to arr[biggestvalueindex] and cannot be compared with arr[i] because Biggestvalueindex is updated between the left and rightBiggestvalueindex=Leftindex; if the value of the left Dial hand node is greater than the value of Biggestvalueindex (this is the value of the root node), then the update Biggestvalueindex is the left Dial Hand node index  }  if(Rightindex<HeapSize&&Arr[rightindex]>Arr[biggestvalueindex]){Biggestvalueindex=Rightindex;if the value of the right child node is greater than the value of Biggestvalueindex (which may be the value of the root node or the value of the left Dial hand node), then the update biggestvalueindex is the right child node index  }  if(Biggestvalueindex!==I{ //If Biggestvalueindex is the left Dial Hand node index or right child node index, then the value of the Exchange root node and the Biggestvalueindex node    ConstTemp=Arr[i];Arr[i]=Arr[biggestvalueindex];Arr[biggestvalueindex]=Temp;    //After swapping, the child node being swapped (left child node or right child node) may no longer meet [parent[i]]>=arr[i], so continue to heaify processing of Biggestvalueindex, The Biggestvalueindex may need to exchange values with the child nodes until this branch of the tree meets the leaf node Arr[parent[i]]>=arr[i]    heapify(arr,HeapSize,Biggestvalueindex);//To  }}
Work process

Pending drawing

Performance analysis
    • Time complexity: Best, average, Worst is O (Nlogn)
    • Space complexity: O (1), unstable

      Extension: If the number of arrays is greater than 10, find only the maximum of 10 values?

      The lines of the above code are replaced by:

while>arr.length-10)
Resources

-"Learning JavaScript data structure and algorithms" 10.1.6
-"Data structure (c language version)" 9.4.2

The JavaScript implementation of heap ordering

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.