Hi everyone,
I am having trouble with a function that mergesorts a generic array (using void *); it seems that I can't swap two elements in it
Here is the code
void mysort( int n, // Number of elements
int elementSize, // Size of each element
void * array, // Pointer to an array
int ascending, // 0 -> descending; 1 -> ascending
CompareFunction compFunc ) // Comparison function.
{
int i, j;
bool flag = true;
void * temp;
int arrayLength = n;
for(i = 1; (i <= arrayLength) && flag; i++)
{
flag = false;
for (j=0; j < (arrayLength -1); j=j+1)
{
void *arrayItem = (void*)((char*)array + j*elementSize );
void *arrayMove = (void*)((char*)array + (j+1)*elementSize );
if(ascending==0)
{
if (compFunc( arrayItem, arrayMove )<=0) // descending
{
temp = arrayItem; // swap elements
arrayItem = arrayMove;
arrayMove = temp;
temp = NULL;
flag = true; // indicates that a swap occurred.
}
}
else if(ascending!=0)
{
if (compFunc(arrayItem, arrayMove)>=0) // ascending
{
temp = arrayItem; // swap elements
arrayItem = arrayMove;
arrayMove = temp;
temp = NULL;
flag = true; // indicates that a swap occurred.
}
}
arrayItem = NULL;
arrayMove = NULL;
}
}
}
return;
}
The swapping parts does not affect the actual array. I have some guesses, but I couldn't figure out how to deal with it. Can you help?