Advertisement
juaniisuar

sorts.c

Jun 29th, 2017
360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. #include "sorts.h"
  2.  
  3. #define min(a,b) (*a<*b) ? a : b
  4.  
  5. static void swap(carta *p, carta *q) {
  6. carta t = *p;
  7. *p = *q;
  8. *q = t;
  9. }
  10.  
  11. void bsortg(void *base, int tam, size_t size, CmpFunc cmp) {
  12. int ordenado = 0, i;
  13. while (!ordenado) {
  14. ordenado = 1;
  15. for (i = 0; i < tam - 1; ++i){
  16. if (cmp( base+(i*size), base+(i+1)*size )) { //cmp representa >
  17. ordenado = 0;
  18. swap(base+(i*size), base+((i+1)*size));
  19. }
  20. }
  21. }
  22. }
  23.  
  24. void ssortg(void *base, int tam, size_t size, CmpFunc cmp) {
  25. int i,j;
  26. carta *mini;
  27. for(i = 0; i < tam-1 ; i++) {
  28. mini = (carta*) base+(i*size);
  29. for (j = i+1 ; j < tam ; j++){
  30. if (cmp(mini, base+(j*size))) //cmp representa >
  31. mini = base+(j*size);
  32. }
  33. if (mini != base+(i*size)) swap(mini,base+(i*size));
  34. }
  35. }
  36.  
  37. void isortg(void *base, int tam, size_t size, CmpFunc cmp) {
  38. int k=0,j;
  39. void *t=malloc(size);
  40. while (k<tam){
  41. memcpy(t,base+(k*size),size);
  42. for ( j = k ; j > 0 && cmp(base+((j-1)*size),t) ; j-- ){ //cmp representa >
  43. memcpy(base+(j*size),base+((j-1)*size),size);
  44. }
  45. memcpy(base+(j*size),t,size);
  46. k++;
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement