void swap(void * pLeft, void * pRight , int width)
{
void * tmp = malloc(width);
memcpy( tmp , pLeft , width );
memcpy( pLeft , pRight , width );
memcpy( pRight, tmp , width );
free(tmp);
tmp = NULL;
}
void qs( void *pHead ,int begin, int end , int width, int (*fcmp)(const void *,const void *))
{
void * compare = malloc(width);
memcpy( compare , &((char *)pHead)[begin*width], width);
int left = begin;
int right = end;
if (left > right) {
return;
}
while (left < right) {
while ( (left < right) && (fcmp( &((char *)pHead)[right*width] , compare )) >= 0 ) {
right--;
}
swap( &((char *)pHead)[left*width], &((char *)pHead)[right*width], width);
while ( (left < right) && (fcmp( &((char *)pHead)[left*width] , compare )) < 0 ) {
left++;
}
swap(&((char *)pHead)[right*width], &((char *)pHead)[left*width], width );
}
memcpy( &((char *)pHead)[right*width] , &((char *)pHead)[left*width], width);
qs( pHead , begin , right - 1 , width , fcmp);
qs( pHead , right + 1 , end , width , fcmp);
free(compare);
compare = NULL;
}
void mryQsort(void *base,int nelem,int width, int (*fcmp)(const void *,const void *))
{
qs( base , 0, nelem - 1 , width , fcmp);
}
int cmp (const void * a,const void * b)
{
return -(*(int*)a - *(int*)b);
}
int main(int argc, char *argv[])
{
int a[9] = {4,2,3,1,6,5,7,9,8};
ccBsort( a, 10, 4, funp);
mryQsort( a , 9, 4, funp);
}