0

Assuming main method and main class are defined, My program is not working for {4,3,4,5,4} and {1,2,3,2,3,4}. Can someone explain me the reason. If I change the while loop, I am getting arrayindexoutofboundsexception.How can I correct my code?

public static int partition(int a[],int l,int h)

{
    int i,t;
    int k=h;
    int mid=(l+h)/2;
    int pivot=a[mid];
    i=l;
    while(i<k)
    {
     while(a[i]<pivot)
     i++;
     while(a[k]>pivot)
     k--;
     if(i<k)
    {
        t=a[i];
        a[i]=a[k];
        a[k]=t;
    }
    if(a[i]==a[k]&&a[i]==pivot)
            break;
    }  
    return i;
}
public static void quicksort(int a[],int l,int h)
{
   if(l<h)
    {
       int j=partition(a,l,h);
        quicksort(a,l,j);
        quicksort(a,j+1,h);
    }
}
public static int[] Met(int a[])
{
    int low=0;
    int len=a.length;
    int high=len-1;
    quicksort(a,low,high);
    return a;

}

0 Answers0