0
/*
* to change this template, choose tools | templates
* and open the template in the editor.
*/
package maxheapifyhomework;
import java. util.random;
/**
*
* @author ***
*/
public class maxheapifyhomework {
/**
* @param args the command line arguments
*/
public static void main(string[] args) {
//i have created random numbers for array to sort
random generator = new random();
int[] array=new int[10];
for (int i=0;i<=array. length;i++){
array[i]=generator. nextint(i);
}
// call maxheapify
//for height logn, n=10 which is log10=1
//root node is array[i]
maxheapify(array, array[1],1);
}
public static void maxheapify(int[] heaparray, int root, int height) {
int left = root + 1;
int right = root + 2;
int largest = root;
//check nodes are greater than parent and choose largest
if (left < height && heaparray[left] > heaparray[root]) {
largest = left;
}
if (right < height && heaparray[right] > heaparray[largest]) {
largest = right;
}
//in the case of largest is not equal to root
//find out a number that will give a hand to recurrence
if (largest != root) {
int number = heaparray[largest];
heaparray[largest] = heaparray[root];
heaparray[root] = number;
maxheapify(heaparray, largest, height);
}
}
}