#include <stdio.h>
#include <windows.h>
int int_cmp(const void *p1,const void *p2)
{
int a = *((int *)p1);
int b = *((int *)p2);
return a > b ? 1 : a < b ? -1 : 0;
}
void swap(void *p1,void *p2,int size)
{
int i = 0;
for (i = 0; i < size;i++)
{
char tmp = *((char *)p1+i);
*((char *)p1 + i) = *((char *)p2 + 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,2,6,7,4,2,5,3,2,8};
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");
system("pause");
return 0;
}