Summary
/* This function takes mid element as pivot, places the pivot element at its
correct position in sorted array, and places all smaller (smaller than pivot)
to left of pivot and all greater elements to right of pivot */
public static int partition(int[] arr,int left,int right)
{
int i=left;
int j=right;
int temp;
int pivot= arr[(left+right)/2];
while(i<=j)
{
while(arr[i]<pivot)
i++;
while(arr[j]>pivot)
j--;
if(i<=j)
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
i++;
j--;
}
}
return i;
}
public static void quickSort(int[] arr,int left,int right)
{
int index=partition(arr,left,right);
if(left<index-1)
{
quickSort(arr,left,index-1);
}
if(right>index)
{
quickSort(arr,index,right);
}
}Last updated