I have written a program to measure execution time of quick sort and am using Code Blocks to compile and run it. When I input random set of numbers my program runs well for any size of data. But when I try to input ascending/descending sorted set of numbers, my program terminates for large data set (>35000) saying program stopped working.
This same code runs well on linux. But on linux I am not able to measure time using QueryPerformanceCounter(). So I have to use Code Blocks on windows.
#include<stdio.h>
#include<windows.h>
double PCFreq = 0.0;
__int64 CounterStart = 0;
void StartCounter()
{
LARGE_INTEGER li;
if(!QueryPerformanceFrequency(&li))
printf("QueryPerformanceFrequency failed!\n");
PCFreq = (double)li.QuadPart;
QueryPerformanceCounter(&li);
CounterStart = li.QuadPart;
}
double GetCounter()
{
LARGE_INTEGER li;
QueryPerformanceCounter(&li);
return (double)(li.QuadPart-CounterStart)/PCFreq;
}
int part(int a[],int low,int high)
{
int pivot,i,j,temp;
pivot=low;
i=low+1;
j=high;
while(i<=j)
{
while(a[i]<=a[pivot])
i++;
while(a[j]>a[pivot])
j--;
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
temp=a[j];
a[j]=a[pivot];
a[pivot]=temp;
return j;
}
void QuickSort(int a[],int first,int last)
{
int q;
if(first<last)
{
q=part(a,first,last);
QuickSort(a,first,q-1);
QuickSort(a,q+1,last);
}
}
int main()
{
int n,a[100000],i;
printf("Enter the size of array:");
scanf("%d",&n);
for(i=0;i<n;i++)
//a[i]=rand()%100000; //average case (random data)
a[i]=i; //ascending sorted input
//a[i]=n-1-i; //descending sorted input
/*
printf("The UNsorted array is:\n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n\n");
*/
StartCounter();
QuickSort(a,0,n-1);
printf("Sorting time %lf micro seconds\n",GetCounter()*1000000.0);
/*
printf("\nThe sorted array is:\n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n");
*/
return 0;
}