Ordering with certain condition

I asked the question who will be reproached? that had the rule the students would be ordered according to the number of problems solved, with ties 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 why in this I had 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;
              }
           }
       }
   }
  }
Author: Fábio Morais, 2018-08-21

1 answers

     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;
          }
       }

In this way I would first see if the note of i was greater than that of j, if it was equal I would pass to that condition.

Your sorting method is selection sort which has complexity of N^2, one of the fastest methods, but a little difficult is quick sort which has complexity in the best case of Nlog(N). The insertion sort is already a bit easier than QuickSort and has complexity of N in the best case.

Can see these methods of intuitive way on the site VisuAlgo

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

SORTING METHODS

 1
Author: Fábio Morais, 2018-08-22 16:58:19