I am programming a genetic algortimo so that this solves problems of linear programming, I am using C language, when calculating the limit of the variables I keep the values in a float type array, I need to order that array but it erases a data What I need at the time of ordering: I have used a shell_sort pogramado by me and the qsort that is implemented in the standard librarian and with both gives me the same result, annex the code of the algortimo that I use to order and the comparator function that I use for the qsort ():
void shell_sort(float *A, int n){
int gap = n/2; //Se obtiene el gap dividiendo el tamaño de arreglo entre dos
int inner, outer, swap; //Variables auxiliares
while (gap > 0) { //Mientras gap sea mayor que zero entonces:
for(outer = gap; outer < n; outer++){ // Para outer igual a gap, siempre que outer sea menor a n, outer aumentara su valor en uno
inner = outer; // inner se iguala al valor de outer
swap = A[inner]; // Swap se iguala a la posiscion inner de A
while (inner > gap - 1 && A[inner - gap] > swap ) { // Mientras inner sea mayor que gap menos 1 y que A en su posicion inner menos gap sea mayor a Swap
A[inner] = A[inner - gap]; //La posicion inner de A tomara como nuevo valor la posicion inner menos gap de A
inner -= gap; //inner decrementa su valor en gap veces
}
A[inner] = swap; //La posicion inner de A tomo como nuevo valor swap
}
gap /=2; // se divide a gap entre dos
}
}
int comp(const void * a, const void * b){
if(*(float*)a < *(float*)b) return -1;
if(*(float*)a == *(float*)b) return 0;
if(*(float*)a > *(float*)b) return 1;
}
In the image you can see the data before ordering them and after ordering them, as you can see the 0.000 is missing Function where the sorting algorithms are used:
Limites obtenerValoresLimites(lista *l,char var){
Limites lim;
restriccion r;
int i,j;
float *aux = (float*)malloc(sizeof(float));
for (i = 0; i < Size(l); i++)
{
r = Element(l,i+1);
for (j = 0; j < strlen(r.variables); j++)
{
if(r.variables[j] == var){
aux[i] = (r.limite/r.coeficientes[j]);
}
}
}
//for (i = 0; i < sizeof(aux)/sizeof(*aux) ;i++)
//printf("%f\n",aux[i]);
//qsort(aux,sizeof(aux)/sizeof(*aux)+1,sizeof(float),comp);
shell_sort(aux,sizeof(aux)/sizeof(*aux));
//printf("\n");
//for (i = 0; i < sizeof(aux)/sizeof(*aux) ;i++)
//{
// printf("%f\n",aux[i]);
//}
lim.inferior = 0;
lim.superior = aux[(sizeof(aux)/sizeof(*aux))-1];
lim.variable = var;
return lim;
}