Choosing pivot from left most value
public void quickSort(int[] arr)
{
quickSortHelper(arr, 0, arr.length - 1);
}
public void quickSortHelper(int[] arr, int left, int right)
{
if(left < right)
{
int pivot = partition(arr,left,right);
quickSortHelper(arr, left, pivot - 1);
quickSortHelper(arr, pivot + 1, right);
}
}
public int partition(int[] arr, int left, int right)
{
int pivot = left;
int leftBound = left + 1;
int rightBound = right;
while(leftBound <= rightBound){
while(leftBound <= rightBound && arr[leftBound] <= arr[pivot]) //move leftBound
{
leftBound++;
}
while(leftBound <= rightBound && arr[rightBound] > arr[pivot]) //move RightBound
{
rightBound--;
}
if(rightBound < leftBound)
{
int temp = arr[pivot];
arr[pivot] = arr[rightBound];
arr[rightBound] = temp;
break;
}else{
int temp = arr[leftBound];
arr[leftBound] = arr[rightBound];
arr[rightBound] = temp;
}
}
return rightBound;
}