Bubblesort - Lexicographic (Alphabetical Order) repeats the first name

Asked

Viewed 742 times

1

I’m doing a name-ordering exercise with Bubblesort, but when ordered, the first name repeats.

Example: I put to register 5 people:

Ana
Luiza
Gabriel
Esther
Luciana

And the ordination comes out as follows:

Ana
Ana
Esther
Gabriel
Luciana

Would someone like to explain to me 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]);      
    }
}
  • It is not necessary greetings and greetings here, I recommend you take a read on [tour] to understand how the site works.

1 answer

1


To be correct in your code do as follows:

void bubbleSort(char (*V)[30], int Fim)
{
    int i, j;
    char temp[30];
 
    for(i=0; i<Fim; i++)
    {
        for(j=0; j<Fim - 1 - i; j++)
        {
            if(strcmp(V[j], V[j+1]) > 0)
            {
                strcpy(temp, V[j]);
                strcpy(V[j], V[j+1]);
                strcpy(V[j+1], temp);
            }
        }
    }
}

The reason you weren’t doing as you intended is that your logic of bubble sort is not correct in some detail:

  • Reading the names with %s has a & the more because in a string, the string itself already works as if it were a pointer so it is not supposed to take the &, 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 Bubble Sort is supposed to start at 0 and not 1:

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

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

    Is changing with j+1, or with the front element through strcpy(V[j], V[j+1]); then when it is at last, the next one is already outside the array which is incorrect. In fact due to the elements that have already been correctly positioned need only go through the end less than the i has already been.

    Correct would be :

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

See in Ideone working

Some relevant notes:

  • Just call one setlocale once and preferably at the beginning of main. Right now it’s calling within the function of bubbleSort that doesn’t make sense, although it doesn’t make any mistake.

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

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

  • It worked! Just one more question: What is capitalizing variable names?

  • @Luizous Capitalize is to put variable names with the first letter uppercase, for example Fim that should be fim.

Browser other questions tagged

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