I'm trying to build a max heap with a static method that's not object-oriented. This is what I have but when I call the method with the following array: {"C","D","A","B","E"}, it just returns the array as is, not in max heap order. I believe the output should be {"E","D","A","B","C"}. Any help would be awesome. Thank you in advance!
public static void buildMaxHeap(String[] x, int n) {
int left = 2*n;
int right = 2*n + 1;
int max = n;
for(int i = x.length/2; i >= 0; i--) {
if(left <= x.length && x[left].compareTo(x[max]) > 0) {
max = left;
}
if(right <= x.length && x[right].compareTo(x[max]) > 0) {
max = right;
}
if(max != n) {
String temp = x[n];
x[n] = x[max];
x[max] = temp;
buildMaxHeap(x,max);
}
}
}