Reputation: 26
Having a little trouble with my quicksort - I'm attempting to pass it & but keep doing something wrong. Any insight would be greatly appreciated.
The code is implementing a generic quicksort that I found online. Essentially, I'm attempting to copy it but sort by the struct's distance. Therefore if the distance is smaller - I want to move the two files and sort them appropriately.
Thanks!
typedef struct Tag
{
char classify;
float pointX;
float pointY;
float distance;
}PointClassify;
void swap(PointClassify &i, PointClassify &j)
{
PointClassify temp;
temp = i;
i = j;
j = temp;
}
void quickSort(PointClassify item[], int start, int end)
{
float pivot;
int i = start, j = end;
pivot = item[(start+end)/2].distance;
while(i <= j)
{
while(item[i].distance < pivot)
{
i = i+1;
}
while(item[j].distance > pivot)
{
j = j-1;
}
if(i <= j)
{
swap(item[i],item[j]);
i = i+1;
j = j-1;
}
}
fprintf(stderr, "This is the number %d \n", item[i].distance);
if(start < j)
{
quickSort(item, start, j);
}
if(i < end)
{
quickSort(item, i, end);
}
}
Upvotes: 1
Views: 794
Reputation: 96316
Does it not compile?
pass-by-reference is a C++ feature. Use pointers:
swap(PointClassify *i, PointClassify *j)
Upvotes: 1
Reputation: 8292
Change this:
void swap(PointClassify &i, PointClassify &j)
to this:
void swap(PointClassify *i, PointClassify *j)
In C, in a declaration, the * indicates that the variable is a pointer to something of that type. You use the & operator to take the address of a varaible. You would use it when calling the function, for example:
swap(&item[i], &item[j]);
Upvotes: 2