Ordering With a certain condition

Asked

Viewed 42 times

1

I made the point Who’s Going to Fail? that had the rule Students would be ordered according to the number of problems solved, with draws solved according to the alphabetical order of the names (there are no homonyms in the class), Well I managed to solve only that I wanted another way because in that needed to order twice My code

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct pessoas
{
 char nome[100];
 int nota;

};

struct pessoas pessoa[102];
void ordena(int teste);
void ordena2(int teste);

int main()
{
  int teste, i, cont = 0;
  while(scanf("%d", &teste) == 1)
  {
      for(i = 0; i < teste; i++)
      {
         scanf("%s %d", pessoa[i].nome, &pessoa[i].nota);
      }

      ordena(teste);
      ordena2(teste);
      printf("Instancia %d\n", ++cont);
      puts(pessoa[teste - 1].nome);
      putchar('\n');
  }
   system("pause");
   return 0;
}

void ordena(int teste)
{
  int i, j , aux;
  char a[100];
  for(i = 0; i < teste; i++)
  {
     for(j = 0; j < teste; j++)
     {
         if(pessoa[i].nota > pessoa[j].nota)
         {
             aux = pessoa[i].nota;
             pessoa[i].nota = pessoa[j].nota;
             pessoa[j].nota = aux;
             strcpy(a, pessoa[i].nome);
             strcpy(pessoa[i].nome, pessoa[j].nome);
             strcpy(pessoa[j].nome, a);
         }
     }
 }
}

 void ordena2(int teste)
 {
    int i, j, aux;
    char a[100];
    for(i = 0; i < teste; i++)
    {
        for(j = i + 1; j < teste; j++)
        {
           if(pessoa[i].nota == pessoa[j].nota)
           {
              if(strcmp(pessoa[i].nome, pessoa[j].nome) > 0)
              {
                strcpy(a, pessoa[i].nome);
                strcpy(pessoa[i].nome, pessoa[j].nome);
                strcpy(pessoa[j].nome, a);
                aux = pessoa[i].nota;
                pessoa[i].nota = pessoa[j].nota;
                pessoa[j].nota = aux;
              }
           }
       }
   }
  }
  • I didn’t understand very well, want other ways to sort? Like for example Quicksort?

  • Could be, the problem I can’t sort out with the rules as the question asks

  • I don’t quite understand, but instead of doing the separate functions you can put them together

  • It would have a more summarized way of doing this, because it has several if and slows down time

  • Of course, but this has to be seen in other ways, its ordering method is O(N 2), I will refer in my answer to other ordering methods

1 answer

1

     if(pessoa[i].nota > pessoa[j].nota)
     {
         aux = pessoa[i].nota;
         pessoa[i].nota = pessoa[j].nota;
         pessoa[j].nota = aux;
         strcpy(a, pessoa[i].nome);
         strcpy(pessoa[i].nome, pessoa[j].nome);
         strcpy(pessoa[j].nome, a);
     }
     else
        if(pessoa[i].nota == pessoa[j].nota)
       {
          if(strcmp(pessoa[i].nome, pessoa[j].nome) > 0)
          {
            strcpy(a, pessoa[i].nome);
            strcpy(pessoa[i].nome, pessoa[j].nome);
            strcpy(pessoa[j].nome, a);
            aux = pessoa[i].nota;
            pessoa[i].nota = pessoa[j].nota;
            pessoa[j].nota = aux;
          }
       }

That way I would first see if the note of i was greater than that of the j, If it were the same I would pass to that condition.

His method of ordination is the Selection Sort that has complexity of N 2, one of the quickest methods, but a little difficult is the Quick Sort which has complexity in the best case of Nlog(N). The Insertion Sort is already a little easier than Quicksort and has complexity of N in the best case.

You can see these methods intuitively on the site Visualgo

Giving a ready-made Quicksort code to your code is quite laborious and so it indicated that you try to do it yourself.

ORDINATION METHODS

Browser other questions tagged

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