Hello, I compared time of execution of different sort algorithm.
I cam accross on interesting thing about quick sort algorithm.
Consider this code pay attention on function partition:
If I execute code that use function median I get average time of execution (when array is already sorted - worst case)this algorithm 100 ms.
#define N 10000
void quick_sort(int*, int, int);
int partition(int*, int, int);
clock_t start, end;
void quick_sort(int* array,int l,int r)
int partition(int* array, int l, int r)
/*here is important point*/
while((array[i] <= pivot) && (i <= r))
while((array[j] > pivot) && (j > l))
if(i < j)
int median (int* arr, int l, int r)
if ((arr[l] > arr[c] && arr[c] > arr[r]) || (arr[r] > arr[c] && arr[c] > arr[l]))
if ((arr[c] > arr[r] && arr[r] > arr[l]) || (arr[l] > arr[r] && arr[r] > arr[c]))
But if I choose not to use median of three function and choose pivot as simply first array element I get exception stack overflow. I'm using Visual Studio .Net. I conclude this is because of recursion in quick_sort. Because choosing first element I'm basically dividing array in two nonequal size arrays, bigger array will have 9999 elements to be sorted using recursion.
I know I might overcome this problem using median function and/or explicit stack with nonrecursive implementation of quick sort.
Is there any possibility that stack size is limited in visual studio to some fixed value and will this code have the same problem on using some other compiler. Can you execute it so we can compare results.
I wonder if this way of measuring time of execution correct. For example if I get result 150 milliseconds what is fault tolerance when using clock() function?
I was learning a lot about different type of sorting algorithms and found good infos in Prelude's corner but I think job isn't finished. I'm willing to write a small tutorial about bubble, selection, insertion and quick sort with algorithm analysis and measuring time of execution, but not sure if there are people interested to read that. And secondly, if there is need for that I'll need someone to examine and correct tutorial because English is not my native language.
Please tell me your opinion about this idea.