I've implemented the quicksort algorithm in Visual Studio 6.0 SP5 in a console project .
It finishes in 0.01 seconds for an array of 1700 elements .For 2000 elements it crashes .
It's probably a stack overflow due to recurrence but I don't know what to do .
Don't tell me to make the variables global .
I give the code below .
Code:
void quickSort(long *array,long iL,long iR){
long pivot,center,left,right,i,j;
if(iL<iR){
//cautam pivotul si-l punem pe ultima pozitie
center=array[(iL+iR)/2];
left=array[iL];
right=array[iR];
if( (center<left && left<right) || (center>left && left>right) ) //left la mijloc
swap(array,iL,iR);
else
if( (left<center && center<right) || (left>center && center>right) ) //center la mijloc
swap(array,(iL+iR)/2,iR);
pivot=array[iR];
//punem elementele mai mici ca pivotul in stanga si cele mai mari in dreapta
for(;;){
i=iL-1;j=iR;
while(array[++i]<pivot);
while(j>iL && array[--j]>pivot);
if(i<j)
swap(array,i,j);
else
break;
};
swap(array,i,iR);
//apelam recursiv algoritmul asupra celor doua subsiruri
quickSort(array,iL,i-1);
quickSort(array,i+1,iR);
}
}