1
public static int partitionsimple_hoare(int[] arr,int l , int h){
    int pivot = arr[l];
    int i = l-1;
    int j = h+1;
    while(true){
        do{
            i++;
        }while(arr[i]<pivot);

        do{
            j--;
        }while(arr[j]>pivot);

        if(i<j){
            swap(arr,i,j);
        }
        else break; 
    }
    return j;
}

This is the basic implementation of Quicksort partition which I employed.

but when I replace the first line (choosing pivot), with :

int pivot = arr[randomPivot(l,h)];

where, the implementation is this:

public static int randomPivot(int l, int r){
    int x =(int)( Math.random()*(r-l+1));
    return x+l;
}

It surprisingly takes a lot more time. (i measured time between the sort call with System.nanoTime(); This isn't supposed to happen. Is it due to Java's Math.random() possibly taking more time than it should've ideally taken?

The worst case performance will not be O(n^2) with the randomized Pivot selection, it'll be O(n logn), thus taking much less time.

  • ```taking more time than it should've ideally taken?``` - How do you know how much time it should have taken? – iceman Sep 21 '17 at 10:26
  • without the randomization call, the worst case running time (eg. already sorted array) can degenerate to O(n^2) performance. The randomized pivot selection is commonly done as a measure to NOT let this happen. Thus it should return a much better performance time (O (n log n ) ). – Akarsh Rastogi Sep 21 '17 at 12:22

1 Answers1

1

The Quick Sort Algorithm Whenever we choose the first element or the last element as the pivot element then the Best Case Time complexity is O(nlogn) and the worst case time complexity is O(n^2) and has an Average Time Complexity of O(nlogn) also

However when the Pivot element is chosen at Random which is mainly done to avoid the Worst Case, the Time Complexity will be O(nlogn) EXCEPT in one case where this also lead to the Worst Case Time Complexity of O(n^2) WHEN all the elements in the INPUT are IDENTICAL. Also we can note here that Since Quick Sort is an Unstable Sorting Algorithm(Refer to the Link just in case) therefore if in the input we have two or more elements which are identical then the time taken for Sorting will also Increase gradually. So, Please check the Input taken once.

Now coming to the code, Please Refer here for the code of the Quick Sort algorithm when the First element is chosen as the pivot element just in case you have made some mistake.

And finally, Here is the code for Randomized Quick Sort in C but there is not much difference in the Code for C and JAVA in this case. Also I would Suggest that you use System.nanoTime(); for measuring the Time when the inputs are identical in the case when the pivot element is the first element and when the pivot element is chosen randomly.

I Hope this helped. Thank you

Radhesh Khanna
  • 141
  • 3
  • 11
  • I used System.nanoTime() only. I used 2 kinds of input: already sorted array, and randomized elements. I took the average over several runs( 1000s) of quicksort through a for loop. The random pivot selection always gives 10 times more running time than normal. My implementations of quicksort and partitioning are standard. – Akarsh Rastogi Sep 21 '17 at 13:12
  • Then i would Recommend to implement the code once in C just to be sure that there is not some inbuilt function in Java that is causing this extended time since i could not figure out any mistake in the code – Radhesh Khanna Sep 21 '17 at 15:35