Trouble sorting code with Shellsort

Asked

Viewed 226 times

0

#include <stdio.h>

typedef struct {
 int id_submissao;
 int tempo;
 int id_equipe;
 int id_problema;
 char status;
} Linha;

void shellSort(Linha *v, int n) {
 int i = (n - 1) / 2;
  int chave, k, aux;

  while(i != 0) {
   do {
     chave = 1;
     for(k = 0; k < n; k++){
        if(v[k] > v[k + i]){
           aux = v[k];
           v[k] = v[k + i];
           v[k + i] = aux;
           chave = 0;
        }
     }
  }while(chave == 0);
      i = i / 2;
  }
}

void imprimeLinhas (Linha* vet, int N) {
int i;

for (i = 0; i < N; i++)
    printf("%d %d %c %d %d\n",
           vet[i].id_equipe,
           vet[i].id_problema,
           vet[i].status,
           vet[i].tempo,
           vet[i].id_submissao);

printf("\n");
}

int main () {

FILE* arq;
Linha vet[105];
int i;

arq = fopen("EDI-1S2018-Aula11-runs.txt", "r");

for (i = 0; !feof(arq); i++)
    fscanf(arq, "%d %d %d %d %c\n",
           &vet[i].id_submissao,
           &vet[i].tempo,
           &vet[i].id_equipe,
           &vet[i].id_problema,
           &vet[i].status);

printf("**********************************\n");
printf("**   Dados antes da ordenacao   **\n");
printf("**********************************\n");
imprimeLinhas (vet, 105);

shellSort(vet, 105);

printf("\n");
printf("**********************************\n");
printf("**    Dados apos a ordenacao    **\n");
printf("**********************************\n");
imprimeLinhas (vet, 105);

return 0;
}

Erro

1 answer

1


After reading a little about Sort shell I found some problems in your code, like in most sorting algorithms, the examples are given with integer vectors, but note that you have a struct vector, so the comparison of v[i] > v[j], like any other comparison (==, <=, >=, !=) logic would not work that way since we can only compare non-priminal types in C, so the first thing I did was arrange its sort function, so that it compares the vetor[k].tempo That way he can check the major times before the minor ones so he can order them. Also note that your auxiliary variable cannot be an integer since it is used to move the structures within your vector, we will then have to use an auxiliary variable of type Linha

void shellSort(Linha *v, int n) {
 int i = (n - 1) / 2;
 int chave, k;
 Linha aux;


 while(i != 0) {   do {
 chave = 1;
 for(k = 0; k < n; k++){
    if(v[k].tempo > v[k + i].tempo){
       aux = v[k];
       v[k] = v[k + i];
       v[k + i] = aux;
       chave = 0;
    }
 }
  }while(chave == 0);
    i = i / 2;
  }
}

Also Realize that your function imprimeLinhas(Linha vet,int N) is incorrect, so it expects to receive a single struct of type Line as parameter, but you pass a vector, as we know you want to print the vector so we have to modify its scope to: imprimeLinhas(Linha *vet,int N) this way you have the function waiting for a vector.

Notice the differences between implementation when you use a primitive type vector, or a struc that contains various information.

I also realized that you do not treat the files correctly, I advise you to do the following to open your file:

arq = fopen("EDI-1S2018-Aula11-runs.txt", "r");
if(arq == NULL) {
    perror("Erro ao abrir arquivo");
    return(-1);
}

This way you can ensure that the file has been opened correctly, otherwise your program will end.Also once you finish using the file it is ideal to close it, in your case after reading the whole file you can close the file, this way:

fclose(arq);

See that function feof() returns a value não zero if the file has not reached the end, and otherwise, valor zero, note that this is not a good way to ensure that you have read your entire file since the function realizes this according to the latest I/O operation (You can read more about this in the documentation here), and also does not make sense since its vector has size limits so the most practical solution would be to leave your for as follows:

for (i = 0; i<105 i++)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.