Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function heapSort(t){
- _buildHeap(t);
- var len = t.length-2;
- while(len>=1){
- _heapSwap2(t, 0, len);
- temp = t[0];
- t[0] = t[len];
- t[len] = temp;
- len--;
- }
- }
- function _heapSwap2(t, i, l){
- var i1 = 2*i+1, i2 =2*i+2, maxi;
- if( i2>l ){
- if( i1>l )
- return;
- else
- maxi = i1;
- }else{
- if( t[i1]<t[i2] )
- maxi = i2;
- else
- maxi = i1;
- }
- if( t[i]<t[maxi] ){
- var temp = t[i];
- t[i] = t[maxi];
- t[maxi] = temp;
- _heapSwap2(t, maxi, l);
- }
- }
- function _heapSwap(t, i){
- while( i!=0 ){
- j = Math.ceil(i/2)-1;
- if( t[j]<t[i] ){
- var temp = t[i];
- t[i] = t[j];
- t[j] = temp;
- i = j;
- }else
- i=0;
- }
- }
- function _buildHeap(t){
- len = t.length;
- i = 0;
- while( i<len ){
- _heapSwap(t, i);
- i++;
- }
- var temp = t[0];
- t[0] = t[len-1];
- t[len-1] = temp;
- }
Advertisement
Add Comment
Please, Sign In to add comment