Skip to content

Instantly share code, notes, and snippets.

@salluzziluca
Last active July 7, 2022 20:43
Show Gist options
  • Save salluzziluca/adb48c5c99c66ac18aaa81e822576163 to your computer and use it in GitHub Desktop.
Save salluzziluca/adb48c5c99c66ac18aaa81e822576163 to your computer and use it in GitHub Desktop.
sort semi-generico
#include <stdbool.h>
void swap(void **vector,int a,int b){
void *aux=vector[a];
vector[a]=vector[b];
vector[b]=aux;
}
int pivotear(void **vector,int cantidad, int (*comparador) (void*, void*)){
int pos_pivote=cantidad-1;
int pos_pivote_final=0;
for(int i=0;i<pos_pivote;i++)
if(comparador(vector[i], vector[pos_pivote]) < 0){
swap(vector,i,pos_pivote_final);
pos_pivote_final++;
}
swap(vector,pos_pivote,pos_pivote_final);
return pos_pivote_final;
}
void sort_semi_generico(void** vector, int cant_elementos, int (*comparador) (void*, void*)){
if(cant_elementos <= 1 || !comparador || !vector || cant_elementos == 0)
return;
int pos_pivote=pivotear(vector,cant_elementos, comparador);
sort_semi_generico(vector, pos_pivote, comparador);
sort_semi_generico(vector+pos_pivote+1,cant_elementos-pos_pivote-1, comparador);
}
//La función comparadora devuelve un valor positivo si el primer elemento debería ir primero o un valor negativo si el segundo debería ir primero. En caso de que sean iguales, se devuelve 0.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment