Miquel_Fuster

seleccion.c

Jan 1st, 2022 (edited)
571
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.77 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include "seleccion.h"
  4.  
  5. void ordenar(void *base, size_t n_elem, size_t tam_elem, int (*compara)(const void* elem_1, const void* elem_2)) {
  6.     unsigned char aux[sizeof(unsigned char) * tam_elem];
  7.     size_t index;
  8.  
  9.     for(size_t i=0; i < n_elem-1; ++i) {
  10.         index = i;
  11.         for(size_t j=i+1; j < n_elem; ++j) {
  12.             if(compara(base+index*tam_elem, base+j*tam_elem) > 0) {
  13.                 index = j;
  14.             }
  15.         }
  16.         if(index != i) {
  17.             memcpy(aux, base+i*tam_elem, sizeof(unsigned char)*tam_elem);
  18.             memcpy(base+i*tam_elem, base+index*tam_elem, sizeof(unsigned char)*tam_elem);
  19.             memcpy(base+index*tam_elem, aux, sizeof(unsigned char)*tam_elem);
  20.         }
  21.     }
  22. }
Add Comment
Please, Sign In to add comment