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