I am trying to calculate the execution time of quicksort algorithm in three cases "Mid pivot, first pivot, random pivot" of an array was already sorted.
The mid pivot and random works fine with any size, but the first pivot case gives stackoverflow if the size was greater than 25000.
Heres the code:
static void q_sort( int left, int right)
{
int pivot;
int l_hold, r_hold;
l_hold = left;
r_hold = right;
pivot = array[left];
while (left < right)
{
while ((array[right] >= pivot) && (left < right))
right--;
if (left != right)
{
array[left] = array[right];
left++;
}
while ((array[left] <= pivot) && (left < right))
left++;
if (left != right)
{
array[right] = array[left];
right--;
}
}
array[left] = pivot;
pivot = left;
left = l_hold;
right = r_hold;
if (left < pivot)
q_sort(left,(int) pivot-1);
if (right > pivot)
q_sort( (int)pivot+1, right);
}
and heres the calling code:
double startTime1 = System.currentTimeMillis();
q_sort(0,size -1);
double stopTime1 = System.currentTimeMillis();
double elapsedTime1 = stopTime1 - startTime1;
System.out.println(elapsedTime1);