2 * bubble-sort: there's no reason to this for anything.
4 * Luiz Fernando N. Capitulino <lcapitulino@mandriva.com.br>
10 int compare(const void *key1
, const void *key2
)
12 if (*((const long *) key1
) > *((const long *) key2
))
14 else if (*((const long *) key1
) < *((const long *) key2
))
20 int bubble_sort(void *data
, int size
, int esize
,
21 int (*compare
) (const void *key1
, const void *key2
))
26 tmp
= (char *) malloc(esize
);
31 int i
, has_swapped
= 0;
33 for (i
= 0; i
< size
-1; i
++) {
34 if (compare(&a
[i
* esize
], &a
[(i
+ 1) * esize
]) > 0) {
35 memcpy(tmp
, &a
[i
* esize
], esize
);
36 memcpy(&a
[i
* esize
], &a
[(i
+ 1) * esize
],
38 memcpy(&a
[(i
+ 1) * esize
], tmp
, esize
);
51 int main(int argc
, char *argv
[])
59 printf("Usage: bubble-sort <num1> <num2> ... <numN>\n");
64 nums
= calloc(len
, sizeof(long));
70 for (i
= 0; i
< len
; i
++) {
71 nums
[i
] = strtol(argv
[i
+ 1], (char **)NULL
, 10);
72 printf("%ld ", nums
[i
]);
76 ret
= bubble_sort((void *) nums
, len
, sizeof(long), compare
);
78 perror("bubble_sort()");
82 for (i
= 0; i
< len
; i
++)
83 printf("%ld ", nums
[i
]);