Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "sorts.h"
- #define min(a,b) (*a<*b) ? a : b
- static void swap(carta *p, carta *q) {
- carta t = *p;
- *p = *q;
- *q = t;
- }
- void bsortg(void *base, int tam, size_t size, CmpFunc cmp) {
- int ordenado = 0, i;
- while (!ordenado) {
- ordenado = 1;
- for (i = 0; i < tam - 1; ++i){
- if (cmp( base+(i*size), base+(i+1)*size )) { //cmp representa >
- ordenado = 0;
- swap(base+(i*size), base+((i+1)*size));
- }
- }
- }
- }
- void ssortg(void *base, int tam, size_t size, CmpFunc cmp) {
- int i,j;
- carta *mini;
- for(i = 0; i < tam-1 ; i++) {
- mini = (carta*) base+(i*size);
- for (j = i+1 ; j < tam ; j++){
- if (cmp(mini, base+(j*size))) //cmp representa >
- mini = base+(j*size);
- }
- if (mini != base+(i*size)) swap(mini,base+(i*size));
- }
- }
- void isortg(void *base, int tam, size_t size, CmpFunc cmp) {
- int k=0,j;
- void *t=malloc(size);
- while (k<tam){
- memcpy(t,base+(k*size),size);
- for ( j = k ; j > 0 && cmp(base+((j-1)*size),t) ; j-- ){ //cmp representa >
- memcpy(base+(j*size),base+((j-1)*size),size);
- }
- memcpy(base+(j*size),t,size);
- k++;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement