#include<stdio.h>
#include<string.h>
#pragma warning(disable:4996)
int cmp(const void *x, const void *y)
{
return (*(char*)x) - (*(char*)y);
}
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)(const void *x, const void *y))
{
int i = 0;
for (i = 0; i < sz - 1; i++){
int j = 0;
for (j = 0; j < sz - i - 1; j++){
int ret = cmp(((char*)base + (j*width)), ((char*)base + (j + 1)*width));
if (ret>0)
{
swap(((char*)base + (j*width)), ((char*)base + (j + 1)*width), width);
}
}
}
}
int main()
{
int arr1[] = { 10, 8, 9, 6, 7 };
bubble_sort(arr1, sizeof(arr1) / sizeof(arr1[0]), sizeof(arr1[0]), cmp);
int i = 0;
for (; i < sizeof(arr1) / sizeof(arr1[0]); i++){
printf("%d", arr1[i]);
}
printf("\n");
char arr2[] = { 'h', 'g', 's', 't', 'o' };
bubble_sort(arr2, sizeof(arr2) / sizeof(arr2[0]), sizeof(arr2[0]),cmp);
for (; i < sizeof(arr2) / sizeof(arr2[0]); i++){
printf("%c", arr2[i]);
}
printf("\n");
system("pause");
return 0;
}