#include<stdio.h>
#include<string.h>
void Swap(char* buf1,char* buf2,int width)
{
int i=0;
for(i=0;i<width;i++)
{
char temp=*buf1;
*buf1=*buf2;
*buf2=temp;
buf1++;
buf2++;
}
}
int cmp_int( void*e1, void* e2)
{
return *(int*)e1-*(int*)e2;
}
void bubble_sort(void* base,int sz,int width,int(*cmp)(void*e1,void*e2))
{
int i=0;
for(i=0;i<sz-1;i++)
{
int j=0;
for(j=0;j<sz-1-i;j++)
{
if(cmp((char*)base+j*width,(char*)base+(j+i)*width)>0)
{
Swap((char*)base+j*width,(char*)base+(j+1)*width,width);
}
}
}
}
void test1()
{
int arr[10]={9,8,7,6,5,4,3,2,1,0};
int sz=sizeof(arr)/sizeof(arr[0]);
bubble_sort(arr,sz,sizeof(arr[0]),cmp_int);
int i=0;
for(i=0;i<sz;i++)
{
printf("%d ",arr[i]);
}
}
int main()
{
//test();
test1();
return 0;
}
错误的输出结果:2 4 5 6 7 3 8 1 9 0