#include <stdio.h> void Swap(char *buf1, char *buf2, int width) { for (int i = 0; i < width; ++i) { char temp ; temp = *buf1; *buf1 = *buf2; *buf2 = temp; buf1++; buf2++; } } void bubble_sort(void *base, int sz, int width, int (*cmp)(const void * e1, const void * e2)) { for (int i = 0; i < sz - 1; i++) { for (int 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 cmp_int(const void *e1, const void *e2) { return *(int *)e1-*(int *)e2; } int main() { int arr[] = {1, 3, 4, 2, 5, 8, 7, 9, 0}; int sz = sizeof(arr) / sizeof(arr[0]); bubble_sort(arr, sz, sizeof(arr[0]), cmp_int); for (int i = 0; i < sz; ++i) { printf("%d\t",arr[i]); } return 0; }