I repeatedly get a StackOverflow error at line marked with ** The stack overflows if I try to sort more than 3 numbers but works for arrays of 3 or less, therefore I do not think there is an infinite recursion. Can someone explain to me why line 246 seems to be the source of the stack overflow?
Thanks
public static void heapSort(double [] a,int node, int index, boolean upcheck){
if(node < 0){
}
else if(node > a.length-index){
}
else if(upcheck){
if(!testHeap(a,node,(2*node)+1) || !testHeap(a,node,(2*node)+2) ){
int min = 0;
if(a[(2*node)+1] > a[(2*node)+2]){
min = (2*node)+2;
}
else{
min = (2*node)+1;
}
switchHeap(a,node,min);
****************************heapSort(a,(node-1)/2,index,true);*********************
}
}
else if(node == (a.length-index-1)/2){
if((2*node)+1 <= a.length-index){
if(testHeap(a,node,(2*node)+1)){
}
else{
heapSort(a,(node-1)/2,index,true);
}
if((2*node)+2 <= a.length-index){
if(testHeap(a,node,(2*node)+2)){
}
else{
heapSort(a,(node-1)/2,index,true);
}
}
}
switchHeap(a,0,a.length-index);
index++;
heapSort(a,0,index,false);
}
else{
if((2*node)+1 <= a.length-index){
if(testHeap(a,node,(2*node)+1)){
}
else{
heapSort(a,(node-1)/2,index,true);
}
heapSort(a,(2*node)+1,index,false);
if((2*node)+2 <= a.length-index){
if(testHeap(a,node,(2*node)+2)){
}
else{
heapSort(a,(node-1)/2,index,true);
}
heapSort(a,(2*node)+2,index,false);
}
}
}
}//heapSort - method