Advertisement
Guest User

Untitled

a guest
Sep 21st, 2014
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.82 KB | None | 0 0
  1. #include "header.h"
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5.  
  6. void sort(void *base, size_t nmemb, size_t size,
  7.                   int (*compare)(const void *, const void *))
  8. {
  9.         if(!compare) return;
  10.         if(base == NULL) return;
  11.         if(size < 2) return;
  12.         for(int i = 0; i < nmemb - 1; i++)
  13.         for(int j = 0; j < nmemb -1 - i; j++)
  14.         {
  15.                         char *a = (char*)base + size*j,
  16.             *b = (char*)base + size*(j+1);
  17.             if(compare(a, b) > 0)
  18.                         {
  19.                 for(int k = 0; k < size; k++)
  20.                                 {
  21.                                     char temp = *(a+k);
  22.                                         *(a+k) = *(b+k);
  23.                                         *(b+k) = temp;
  24.                                 }  
  25.                         }
  26.         }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement