HackerRank: My QuickSort algorithm is too slow

Can someone please explain to me why this QuickSort algorithm has bad performance?

I followed a tutorial from Derek Banas so I thought it would optimal.

public static void quickSort(int[] arr, int left, int right){

if(left>=right) return;

int pivot = arr[right];

int index = partition(arr, left, right, pivot);

quickSort(arr, left, index-1);

quickSort(arr, index+1, right);

}

private static int partition(int[] arr, int left, int right, int pivot){

int leftPointer = left-1;

int rightPointer = right;

while(true){

while(arr[++leftPointer]
while(arr[--rightPointer]>pivot && rightPointer>leftPointer);

if(leftPointer>=rightPointer) break;

else{

//swap values

int temp = arr[leftPointer];

arr[leftPointer] = arr[rightPointer];

arr[rightPointer] = temp;

}

}

int tempP = arr[leftPointer];

arr[leftPointer] = arr[right];

arr[right] = tempP;

return leftPointer;

}...

Read More »

By: StackOverFlow - Tuesday, 6 November

Related Posts