BubbleSort - Lexicographic (Alphabetical Order) repeats the first name

0

I'm doing a name sorting exercise with BubbleSort, but when sorted, the first name repeats itself.

Example: I register to register 5 people:

  

Ana
  Luiza
  Gabriel   Ester
  Luciana

And the ordering comes out as follows:

  

Ana
  Ana
  Ester
  Gabriel   Luciana

Could anyone explain why this happens?

Here is my code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <conio.h>
#include <locale.h>

//Função do Bubble Sort
void bubbleSort(char (*V)[30], int Fim)
{
    setlocale(LC_ALL, "PORTUGUESE");
    int i, j;
    char temp[30];

    for(i=1; i<Fim; i++)
    {
        for(j=0; j<Fim; j++)
        {
            if(strcmp(V[j], V[j+1]) > 0)
            {
                strcpy(temp, V[j]);
                strcpy(V[j], V[j+1]);
                strcpy(V[j+1], temp);
            }
        }
    }
}

//Código
int main()
{
    setlocale(LC_ALL, "PORTUGUESE");
    int Fim=0, i=0;

    printf("\n Quantas pessoas deseja cadastrar? ");
    scanf("%i", &Fim);
    char V[Fim][30];

    for(i=0; i<Fim; i++)
    {
        printf("\n ---------------------------------------");
        printf("\n   Digite o nome da %iº pessoa: ", i+1);
        scanf("%s", &V[i]);
    }
    system("cls");


    printf("\n ############### ORDENAÇÃO POR NOME ###############");
    for(i=0; i<Fim; i++)
    {
        bubbleSort(V, Fim);
        printf("\n %s", V[i]);      
    }
}
    
asked by anonymous 02.12.2018 / 22:20

1 answer

1

The reason why it does not work out is that its bubble sort logic is not correct in some details:

  • Reading names with %s has & more because in a string, the string itself functions as if it were a pointer, so it is not supposed to take & , here:

    for(i=0; i<Fim; i++)
    {
         printf("\n ---------------------------------------");
         printf("\n   Digite o nome da %iº pessoa: ", i+1);
         scanf("%s", &V[i]);
         //          ^--- está a mais
    
  • The first for of bubble sort is supposed to start in 0 and not 1 :

    for(i=1; i<Fim; i++)
    //    ^--- 0 em vez de 1
    
  • The end of the second for is also not correct, and even goes out of the array, which basically generates indefinite behavior:

    for(i=1; i<Fim; i++)
        for(j=0; j<Fim; j++)
        //          ^--- este
    

    If you are swapping with j+1 , ie with the front element through strcpy(V[j], V[j+1]); then when it is in the last, the next one is already outside the array which is incorrect. In fact, because of the elements that have already been correctly positioned, they only need to go to the end, except the ones that i has already walked.

    Correct would be:

    for(j=0; j<Fim-1-i; j++)
    //         ^^^^^^^
    

See Ideone working

Some relevant notes:

  • Only call a setlocale once and preferably at the beginning of main . You are currently calling within the bubbleSort function which does not make sense, although it does not cause you any error.

  • Avoid including libraries you are not using as time.h or math.h

  • Avoid capitalizing variable names, as this clearly goes against the naming pattern used in C.

03.12.2018 / 00:45