I have a task:
I need to swap an elements in array, by "void pointers based" swap function. Its a simple bubble sort algorithm.
But my void SwapInt(void *x, void *y)
function doesnt work! I mean it called correctly, but did nothing. My pre-sorting array doesnt change. What could be wrong here and how it fix?
void SwapInt(void *x, void *y)
{
void *buffer = x;
x = y;
y = buffer;
}
bool CmpInt(void *x, void *y)
{
int *intPtrX = static_cast<int*>(x);
int *intPtrY = static_cast<int*>(y);
if(*intPtrX > *intPtrY)
return true;
else
return false;
}
void Sort(int array[], int nTotal, size_t size, void (*ptrSwapInt)(void *x, void *y), bool (*ptrCmpInt)(void *x, void *y))
{
for (int i = 0; i < nTotal; i++)
{
for (int j = 0; j < nTotal - 1; j++)
{
if (ptrCmpInt(&array[j] , &array[j + 1]))
{
ptrSwapInt(&array[j], &array[j + 1]);
}
}
}
}
P.S I have already visit StackOverflow_1 and StackOverflow_2, and I still dont have a clue, what is wrong.