Quick sort algorithm troubles
May 5, 2010 at 8:03am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
void quicksort(vector<int > &data, int start, int end)
{
int i=start,j=end;
int l;
int tmp,pivot,closest;
int sum=0;
double finalsum,average,next,potential;
for (int k=0;k<data.size();k++)
{
sum=sum+data[k];
}
finalsum=sum+0.0;
average=finalsum/data.size();
closest=0;
for (l=0;l<data.size();l++)
{
next=abs(data[l]-average);
potential=abs(data[closest]-average);
if (next<potential)
{
closest=l;
}
}
pivot=l;
while (i<=j)
{
while (data[i]<pivot)
{
i++;
}
while (data[j]>pivot)
{
j--;
}
if (i<=j)
{
tmp=data[i];
data[i]=data[j];
data[j]=tmp;
i++;
j--;
}
}
if (start<j)
{
quicksort(data,start,j);
}
if (i<end)
{
quicksort(data,i,end);
}
}
This is a quicksort algorithm I wrote. I am getting REALLY WEIRD results. Somehow stuck in an infinite loop. Please help.
May 6, 2010 at 1:23am UTC
What do you want for someone to debug it for you? Have you tried to debug it by stepping line by line? Also if you do want help debugging you need to show us a complete, compilable function so that we can see what set of inputs you are using.
Topic archived. No new replies allowed.