#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int cmp_int(const int *e1,const int *e2)
{
return *(int*)e1 - *(int*)e2;
}
void swap(char* buf1,char* buf2,int width)
{
int i = 0;
for (i = 0; i < width; i++)
{
char tmp = *buf1;
*buf1 = *buf2;
*buf2 = tmp;
buf1++;
buf2++;
}
}
void bubble_sort(void* base,int sz,int width,int(*cmp)(void*e1,void*e2))
{
int i = 0;
for (i = 0; i <= sz; i++)
{
int j = 0;
for (j = 0; j < sz-1-i; j++)
{
if (cmp((char*)base + j*width, (char*)base + (j + 1)*width)>0)
{
swap((char*)base + j*width, (char*)base + (j + 1)*width, width);
}
}
}
}
int main()
{
int arr[] = { 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 a = 0;
for (a = 0; a < sz; a++)
{
printf("%d ", arr[a]);
}
return 0;
}
以回调函数实现冒牌排序
最新推荐文章于 2023-04-27 21:29:57 发布