heap sort
A heap is a complete binary tree with the following properties: the value of each node is greater than or equal to the value of its left and right child nodes, which is called a big top heap; or the value of each node is less than or equal to the value of its left and right child nodes value, called the small top heap. As shown below:
function heapSort(arr) { for(var i=Math.floor(arr.length/2)-1;i>=0;i--){ //Generate a large top heap from top to bottom heapAdjust(arr,i,arr.length); } for(var j=arr.length-1;j>0;j--){ swap(arr,0,j); //Swap the push element and the end element, so that the largest element is swapped to the end of the queue heapAdjust(arr,0,j); //Re-adjust the heap } return arr; } function heapAdjust(arr,i,len) { //Top down var temp=arr[i]; var largest=i; var left=2*i+1; var right = 2 * i + 2; if(left<len && arr[left]>arr[largest]){ larget=left; } if(right<len && arr[right]>arr[largest]){ largest=right; } if(largest!=i){ swap(arr,larget,i); heapAdjust(arr,largest,len); } }