
public class My5Heap {
	private double[] priorityArray;
	private String[] valueArray;
	private int next;
	int size;

	public My5Heap(int size) {
		this.size=size;
		priorityArray = new double[size];
		valueArray = new String[size];
		next = 0;
	}

	public void insert(double priority, String value) {
		priorityArray[next] = priority;
		valueArray[next] = value;
		int pos = next;
		while(pos > 0) {
			int parent = (pos-1)/5;
			if(priorityArray[pos] > priorityArray[parent]) {
				swap(pos, parent);
			} else {
				break;
			}
			pos = parent;
		}
		next = next + 1;
	}
	
	public void swap(int pos1, int pos2) {
		double tempPriority = priorityArray[pos1];
		String tempValue = valueArray[pos1];
		priorityArray[pos1] = priorityArray[pos2];
		valueArray[pos1] = valueArray[pos2];
		priorityArray[pos2] = tempPriority;
		valueArray[pos2] = tempValue;
	}
	
	public boolean isFull() {
		return (next >= priorityArray.length-1);
	}
	
	public String deleteMax() {
		double max=0;//the root value
		String str="";
		
			//the last hole
			double lastVal=priorityArray[next - 1];
			String lastStr=valueArray[next - 1];
		
			//the max node 
			max=priorityArray[0];
			str=valueArray[0];
			
			//delete the max
			priorityArray[0]=lastVal;
			valueArray[0]=lastStr;

			per_Down(0);
		
			next = next - 1;
			
		return max+"\t"+str;
	}
	
	public void per_Down(int hole){
		int child;		
		int cindex=0;
		double cmax=0;
		
		while(hole*5+1<next){
			child=hole*5+1;
			//find the max in the children
			//if the last onde's children number less than 5
			if(hole*5+5>next-1){
				for(int j=child;j<next-1;j++){
					if(priorityArray[j]<priorityArray[j+1]){
						cmax= (cmax>priorityArray[j+1]?cmax:priorityArray[j+1]);
							if(cmax==priorityArray[j+1])
								cindex=j+1;
					}
					else if(priorityArray[j]>priorityArray[j+1]){
						cmax=cmax>priorityArray[j]?cmax:priorityArray[j];
							if(cmax==priorityArray[j])
								cindex=j;
					}
				}
			}
			else{
				//find max between child, and its index
				for(int i=child;i<child+4;i++){
					if(priorityArray[i]<priorityArray[i+1]){
						cmax=cmax>priorityArray[i+1]?cmax:priorityArray[i+1];
							if(cmax==priorityArray[i+1])
								cindex=i+1;
					}
					else if(priorityArray[i]>priorityArray[i+1]){
						cmax=cmax>priorityArray[i]?cmax:priorityArray[i];
							if(cmax==priorityArray[i])
								cindex=i;
					}
				}
			} 
			//swap nodes
			if(priorityArray[cindex]>priorityArray[hole]){
				swap(cindex, hole);
			}
			else break;
			hole=cindex;
		}
	}

}

@@ -1575,95 +1575,32 @@
 %09%09%09%09
-for(int j=child;j%3Cnext-1;j++)%7B%0A%09%09%09%09%09if(priorityArray%5Bj%5D%3CpriorityArray%5Bj+1%5D)%7B%0A%09%09
+cindex = child;%0A
 %09%09%09%09cmax
 = (c
@@ -1599,16 +1599,11 @@
 cmax
+ 
 = 
-(cmax%3E
 prio
@@ -1616,245 +1616,87 @@
 ray%5B
-j+1%5D?cmax:priorityArray%5Bj+1%5D);%0A%09%09%09%09%09%09%09if(cmax==priorityArray%5Bj+1%5D)%0A%09%09%09%09%09%09%09%09cindex=j+1;%0A%09%09%09%09%09%7D%0A%09%09%09%09%09else if(priorityArray%5Bj%5D%3EpriorityArray%5Bj+1%5D)%7B%0A%09%09%09%09%09%09cmax=cmax%3EpriorityArray%5Bj%5D?cmax:priorityArray%5Bj%5D;%0A%09%09%09%09%09%09%09if(cmax==priorityArray%5Bj%5D)%0A%09%09
+cindex%5D;%0A%09%09%09%09for(int i=child+1;i%3C=next-1;i++)%7B%0A%09%09%09%09%09if(priorityArray%5Bi%5D%3Ecmax)%7B%0A
 %09%09%09%09
@@ -1707,10 +1707,12 @@
 ndex
-=j
+ = i
 ;%0A%09%09
@@ -1788,352 +1788,128 @@
 %09%09%09%09
-for(int i=child;i%3Cchild+4;i++)%7B%0A%09%09%09%09%09if(priorityArray%5Bi%5D%3CpriorityArray%5Bi+1%5D)%7B%0A%09%09%09%09%09%09cmax=cmax%3EpriorityArray%5Bi+1%5D?cmax:priorityArray%5Bi+1
+cindex = child;%0A%09%09%09%09cmax = priorityArray%5Bchild
 %5D;%0A%09%09%09%09
-%09%09
-%09if(cmax==priorityArray%5Bi+1%5D)%0A%09%09%09%09%09%09%09%09cindex=i+1;%0A%09%09%09%09%09%7D%0A%09%09%09%09%09else if(priorityArray%5Bi%5D%3EpriorityArray%5Bi+1%5D)%7B%0A%09%09%09%09%09%09cmax=cmax%3EpriorityArray%5Bi%5D?cmax:priorityArray%5Bi%5D;%0A%09%09%09%09%09%09%09if(cmax==priorityArray%5Bi%5D)%0A%09%09
+for(int i=child+1;i%3C=child+4;i++)%7B%0A%09%09%09%09%09if(priorityArray%5Bi%5D%3Ecmax)%7B%0A
 %09%09%09%09
@@ -1916,17 +1916,19 @@
 %09%09cindex
-=
+ = 
 i;%0A%09%09%09%09%09

Update My5Heap Fix the bug that deleteMax does not alway returns the highest priority. Refactor to reduce redundant code.