Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Generic insertion sort
- // Can be compiled using : -W -Wall -Werror -Wextra -ansi -pedantic
- void sort(void *p, int n, int size, int cmp(void *, void *))
- {
- int i;
- int j;
- void *a;
- a = __builtin_malloc(size);
- for (i=1; i<n; i++)
- {
- __builtin_memcpy(a, (char*)p+i*size, size);
- for (j=i; j>0 && cmp((char*)p+(j-1)*size, a)>0; j--)
- {
- __builtin_memcpy((char*)p+j*size, (char*)p+(j-1)*size, size);
- }
- __builtin_memcpy((char*)p+j*size, a, size);
- }
- __builtin_free(a);
- }
- int compare(void *a, void *b)
- {
- return *((int*)a) - *((int*)b);
- }
- #include <stdio.h>
- int main()
- {
- int tab[5] = {2,986,-9,0,16};
- printf("%d %d %d %d %d\n", tab[0], tab[1], tab[2], tab[3], tab[4]);
- sort(tab, 5, sizeof(int), compare);
- printf("%d %d %d %d %d\n", tab[0], tab[1], tab[2], tab[3], tab[4]);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement