Name: Anonymous 2008-07-09 1:39
inline void swap_bytes(void *a, void *b, size_t size)
{
char *a_char = a, *b_char = b, temp;
while(size--) {
temp = *a_char;
*a_char++ = *b_char;
*b_char++ = temp;
}
}
void bubble_sort(void *base, size_t num, size_t size, int (*comparator)(const void *, const void *))
{
void *first, *second;
size_t pass, passes = num - 1, i;
int result;
if(num > 1) {
for(pass = 0; pass < passes; ++pass) {
second = base + size * num - size;
first = second - size;
for(i = num; i > pass + 1; --i) {
result = comparator(first, second);
if(result == 1)
swap_bytes(first, second, size);
first -= size;
second -= size;
}
}
}
}Here you go, /prog/, a generic implementation of bubble sort that can replace qsort().