From 0189fdaa9744c0aeddcf72ca0f3f7fa3981e53a2 Mon Sep 17 00:00:00 2001 From: Ricardo Markiewicz Date: Sun, 20 Jun 2004 19:10:50 +0000 Subject: [PATCH] BUGFIX : Se sacan cosasq ue no se necesitan mas y se arregla la funcion de comparacion para que el qsort ordene bien. --- otros/blocksorting/bs.c | 33 +++++++++++++-------------------- otros/blocksorting/bs.h | 2 -- otros/blocksorting/main.c | 13 ++++++++++--- 3 files changed, 23 insertions(+), 25 deletions(-) diff --git a/otros/blocksorting/bs.c b/otros/blocksorting/bs.c index b7caa54..21e1ff6 100644 --- a/otros/blocksorting/bs.c +++ b/otros/blocksorting/bs.c @@ -36,10 +36,11 @@ char es_menor(char *data, t_BlockSort *bs, int i, int j) pi = (i+k)%bs->len; pj = (j+k)%bs->len; - if (data[pi] > data[pj]) return 0; - if (data[pi] < data[pj]) return 1; + if (data[pi] > data[pj]) return 1; /* El primero es mayor */ + if (data[pi] < data[pj]) return -1; /* El primero es menor */ } + /* Son iguales! */ return 0; } @@ -49,30 +50,23 @@ void generar_array(char *data, t_BlockSort *bs) for(i=0; ilen; i++) { bs->array[i].pos_inicial = i; bs->array[i].pos_final = (i+bs->len-1)%bs->len; - bs->array[i].pos_orden = 0; - bs->array[i].ord = (i==1)?1:0; + bs->array[i].ord = (i==0)?1:0; bs->array[i].bs = bs; } } void ordenar_array(char *data, t_BlockSort *bs) { - /*unsigned long int i, j, min;*/ - -/* for(i=0; ilen; i++) { - min = -1; - for(j=0; jlen; j++) { - if (bs->array[j].ord) continue; - if ((min==-1) || (es_menor(data, bs, j, min))) - min = j; - } + qsort(bs->array, bs->len, sizeof(t_BlockSortData), __compare); +} - bs->array[min].ord = 1; - bs->array[min].pos_orden = i; +void print_(char *data, unsigned long int pos, unsigned long int len) +{ + unsigned long int i; - bs->ord[i] = min; - }*/ - qsort(bs->array, bs->len, sizeof(t_BlockSortData), __compare); + for(i=0; ilen; i++) { + /* print_(data, bs->array[i].pos_inicial, bs->len); */ salida[i] = data[bs->array[i].pos_final]; if (bs->array[i].ord == 1) k = i; } @@ -130,7 +125,6 @@ t_BlockSort *bs_create(unsigned long int len) tmp = malloc(sizeof(t_BlockSort)); tmp->array = malloc(sizeof(t_BlockSortData)*len); - tmp->ord = malloc(sizeof(unsigned long int)*len); tmp->len = len; return tmp; @@ -140,7 +134,6 @@ t_BlockSort *bs_create(unsigned long int len) void bs_destroy(t_BlockSort *bs) { free(bs->array); - free(bs->ord); free(bs); } diff --git a/otros/blocksorting/bs.h b/otros/blocksorting/bs.h index 099acb2..5097d6c 100644 --- a/otros/blocksorting/bs.h +++ b/otros/blocksorting/bs.h @@ -11,7 +11,6 @@ typedef struct _bs_t_ t_BlockSort; typedef struct _bs_data_t_ { unsigned long int pos_inicial; unsigned long int pos_final; - unsigned long int pos_orden; char ord; /* indica si esta ordenada */ /* Guardo el puntero al padre */ @@ -21,7 +20,6 @@ typedef struct _bs_data_t_ { struct _bs_t_ { char *data; t_BlockSortData *array; - unsigned long int *ord; unsigned long int len; }; diff --git a/otros/blocksorting/main.c b/otros/blocksorting/main.c index 7028017..2c8abb9 100644 --- a/otros/blocksorting/main.c +++ b/otros/blocksorting/main.c @@ -5,6 +5,7 @@ int main(int argc, char *argv[]) { char *data; char *salida; + char *orig; unsigned long int len, i, k, total; FILE *fp; char c; @@ -23,8 +24,11 @@ int main(int argc, char *argv[]) data = malloc(sizeof(char)*len); salida = malloc(sizeof(char)*(len+1)); + orig = malloc(sizeof(char)*(len+1)); salida[len] = '\0'; + orig[len] = '\0'; + bs = bs_create(len); c = fgetc(fp); @@ -39,9 +43,11 @@ int main(int argc, char *argv[]) bs_solve(data, salida, bs, &k, i); /* XXX ACA SALIDA DEBERIA PASAR A LA SIGUIENTE ETAPA XXX */ - if (argc == 3) - printf("%s -> %ld\n", salida, k); - else + if (argc == 3) { + printf("BS : %s -> %ld\n", salida, k); + bs_restore(orig, salida, k, i); + printf("Orig : %s\n", orig); + } else printf("%s", salida); } fclose(fp); @@ -50,6 +56,7 @@ int main(int argc, char *argv[]) printf("Total bytes : %ld\n", total); free(data); free(salida); + free(orig); return 0; } -- 2.43.0