模仿qsort的功能实现一个通用的冒泡排序。


int int_cmp(const void*p1,const void *p2){
Return (*(int *)p1>*(int *)p2);
}
void  _swap(void *p1,void *p2,int size){
Int i=0;
for(i=0;i<size;i++){
char tmp=*((char *)p1+i);
*((char *)p2+i)=tmp;
}
}
void bubble(void *base,int count ,int size,int (*cmp)(void *,void *)){
Int i=0;
Int j=0;
For(i=0;i<count-1;i++)
{
For (j=0;j<count-1-i;j++)
{
If(cmp((char * ) base+j*size,(char *)base +(j+1)*size)>0)
{
_swap((char*)base+j*size,(char *)base+(j+1)*size,size);
}
}
}


}


Int main(){
Int arr[]={1,3,5,7,9,3,4};
Int i=0;
Bubble(arr,sizeof(arr)/sizeof(arr[0]),sizeof(int),int_cmp);
For(i=0;i<sizeof(arr)/sizeof(arr[0];i++)
{
Printf(“%d”,arr[i];
}
Printf(“\n”);
Return 0;
}

猜你喜欢

转载自blog.csdn.net/sd116460/article/details/80443577