hey
i am still a beginner at programming but i am trying to write a program that counts the number of comparisons in a quicksort program
this is my code
package algo_quicksort;
public class Algo_quicksort {
public static int partition(int[]A,int p,int r){
int x=A[p];
int i=p+1;
int temp;
for(int j=p+1;j<r;j++){
if(A[j]<x){
temp=A[j];
A[j]=A[i];
A[i]=temp;
i++;
}
}
temp=A[p];
A[p]=A[i-1];
A[i-1]=temp;
return i-1;
}
public static long quickSort(int[]A,int startPos,int length){
if(length==1){
return 0;
}
else{
if(startPos<length){
int pivot= partition(A,0,length);
quickSort(A,startPos,pivot+1);
quickSort(A, pivot+2,length);
return length-startPos-1;
}
else{
return 0;
}
}
}
public static void main(String[] args) {
int a[]={3,2,4};
System.out.println("# of comparisons is: " +quickSort(a,0,a.length));
System.out.println("A[] after quicksort is: ");
for(int i=0;i<a.length;i++){
System.out.print(a[i]+" ");
}
}
}
it works perfectly for any array of size 3 or less
but if it is any bigger than that it gives me a stackoverflow exception at the recursive call
i tried debugging my code but could not figure out where it's going wrong
i would appreciate any help i get ^_^