Advertisement
Guest User

Untitled

a guest
Jan 6th, 2014
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.25 KB | None | 0 0
  1.  
  2.     public static void heapSort(double [] a, int node, int index){
  3.         System.out.println("made it");
  4.         int finalnode = 0;
  5.         int treelength = 0;
  6.         treelength = (int)((Math.log(a.length-index))/(Math.log(2)));
  7.  
  8.         finalnode = Math.pow(2,treelength) - 1;// log base 2 of (a.length - index) - truncating to get the int value - gives the length of the tree. 2^(treelength) - 1 gives the position of the final node that will be evaluated before the top value is swapped with the bottom value
  9.        
  10.        
  11.         System.out.println("THe Finalnode is " + finalnode);
  12.         System.out.println("The index is " + index);
  13.         if(!(node > finalnode-1) && node >= 0 && index < a.length){
  14.             System.out.println("in");
  15.             boolean switchval = false;
  16.             double holder = 0;
  17.            
  18.            
  19.             if(node == finalnode){
  20.                 switchval = true;
  21.             }      
  22.             if(minNode(a,node,index) != -1){
  23.                 fixHeap(a,node, index);
  24.             }
  25.             if(switchval){
  26.                 System.out.println("Switch");
  27.                 holder = a[0];
  28.                 a[0] = a[a.length-(1+index)];
  29.                 a[a.length-(1+index)] = holder;
  30.                 index++;
  31.                 heapSort(a,0,index);       
  32.             }
  33.             else{
  34.                 heapSort(a,(2*node) +1,index);
  35.                 heapSort(a,(2*node) +2,index);
  36.                 }
  37.         }      
  38.     }//heapSort
  39.    
  40.    
  41.     public static int minNode(double [] a, int node,int index){
  42.         double min;
  43.         int pos;
  44.         if((2*node + 1) < a.length - index){
  45.             if((2*node + 2) < a.length - index){
  46.                 if(a[(2*node + 1)] > a[(2*node + 2)]){
  47.                     min = a[(2*node + 2)];
  48.                     pos = (2*node + 2);
  49.                 }
  50.                 else{
  51.                     min = a[(2*node + 1)];
  52.                     pos = (2*node + 1);
  53.  
  54.                 }
  55.                
  56.                 if(a[node] < min){
  57.                     return (-1);
  58.                 }
  59.                 else{
  60.                     return pos;
  61.                 }
  62.                
  63.             }// if 2nd child exists
  64.             min = a[(2*node + 1)];
  65.             pos = (2*node + 1);
  66.             if(a[node] < min){
  67.                     return (-1);
  68.                 }
  69.             else{
  70.                 return pos;
  71.             }
  72.         }// if 1st child exists
  73.         return -1; 
  74.     }//testNode
  75.    
  76.     public static void fixHeap(double [] a, int node, int index){
  77.         int test = node;
  78.         int minNode = 0;
  79.         double holder;
  80.         int counter = 0;
  81.         while(true){   
  82.            
  83.             counter++;
  84.             if(test >= 0){
  85.                 minNode = (minNode(a,test, index));
  86.                 if(minNode == -1){
  87.                     break;
  88.                 }
  89.                 holder = a[test];
  90.                 a[test] = a[minNode];
  91.                 a[minNode] = holder;
  92.                 test = (test-1)/2;
  93.             }
  94.             else{              
  95.                 break;
  96.             }
  97.         }      
  98.     }//fixHeap
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement