SHARE
TWEET

Untitled

a guest May 19th, 2019 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "UNIVERS_SORT.h"
  2. #include <stdio.h>
  3. #include <stddef.h>
  4.  
  5. void swap(void *a, void *b, size_t size) {
  6.     char tmp;
  7.     char* ac = (char*)(a);
  8.     char* bc = (char*)(b);
  9.     for( int i = 0; i < size; i++){
  10.         tmp = ac[i];
  11.         ac[i] = bc[i];
  12.         bc[i] = tmp;
  13.     }
  14. }
  15.  
  16. void sort (void* arr, size_t n, size_t size, int (*cmp)(const void* a, const void* b)){
  17.     size_t i, j, min;
  18.     char *pMin, *pi, *pj;
  19.     pi = (char*)arr;
  20.     for( i = 0; i < n-1; ++i, pi += size ){
  21.         pMin = pi;
  22.         min = i;
  23.         for( pj = pi+size, j = i+1; j < n; ++j, pj += size  ){
  24.             if( cmp( pj, pMin ) < 0 ){
  25.                 pMin = pj;
  26.                 min = j;
  27.             }
  28.         }
  29.         if( min != i ){
  30.             for( j = 0; j < size; ++j ){
  31.                 swap(&pi[j],&pMin[j], sizeof(*pi));
  32.             }
  33.         }
  34.     }
  35. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top