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.
– user28595