Search code examples
arraysstringsortingstructurestrcmp

Sorting does not sort first item in the array


Currently i got this:

    struct employe{
    char nomE[25];
    char posteE;
    float nbHeureE;
    float tauxE;
} employes[16];
int nbPers = 0;

void readFile(){
    int i=0;

    FILE * entree;
    if(entree = fopen("employes.dat", "r"))
    {
        fscanf(entree, "%24c %c %f %f", &employes[i].nomE, &employes[i].posteE, &employes[i].nbHeureE, &employes[i].tauxE);
        nbPers++;
        while(!feof(entree)) 
        {
            i++;
            fscanf(entree, "%24c %c %f %f", &employes[i].nomE, &employes[i].posteE, &employes[i].nbHeureE, &employes[i].tauxE);
            nbPers++;
        }
        fclose(entree);
    }
    else printf("Impossible d'ouvrir le fichier!\n");
}

void trier(struct employe employes[], int nbPers){
    int j,i,k;
    struct employe temp;
    for(i=0;i<16;i++)
    {
        for(j=1;j<15;j++)
        {
            if(strcmp(employes[i].nomE, employes[j].nomE) < 0)
            {
                temp = employes[i];
                employes[i] =employes[j];
                employes[j] = temp;
            }
        }
    }
}

int main() {
    int p=0;

    readFile();
    trier(employes, nbPers);
    for(p=0; p<nbPers; p++)
    printf("%s", employes[p].nomE);

    return 0;
}

employes.dat looks like this:

Tremblay Alain           A 35.0 35.5
Vachon Jean              P 40.0 22.75
Lapalme Justin           O 40.0 15.75
Deschenes Sylvie         P 35.0 25.0
Lachance Carl            O 37.5 18.0
Labonte Chantal          P 40.0 20.0
Doucet Michel            A 40.0 33.75
Desjardins Alex          P 35.0 25.0
Tardif Guy               A 40.0 28.5
Clinclin Stephane        O 40.0 20.75
Lafleur Marie            A 37.5 32.75
Desbiens Robert          P 35.0 25.0
Desautels Maryse         P 35.0 26.0
St-germain guy           O 37.5 15.0
Bourgeois Louis          A 37.5 29.0
St-amour Flavie          P 40.0 25.0

I'm trying to sort my employe structure in alphabetical order from the name (char nomE[25];). But, for some reason, it dosnt sort the first name and outputs this:

Tremblay Alain
Bourgeois Louis
Clinclin Stephane
Desautels Maryse
Desbiens Robert
Deschenes Sylvie
Desjardins Alex
Doucet Michel
Labonte Chantal
Lachance Carl
Lafleur Marie
Lapalme Justin
St-amour Flavie
St-germain guy
Tardif Guy
Vachon Jean

If someone have any idea why, I would REALLY appreciate the answer. Thank you in advance.


Solution

  • Change:

    for(i=0;i<16;i++) {
        for(j=1;j<15;j++){ 
    

    to:

    for(i=0;i<16;i++) {
        for(j=0;j<15;j++){
    

    EDIT: You have confused SelectionSort with BubbleSort. SelectionSort goes with i = 0 and j = i + 1 but also with i < 15 and j < 16, whereas BubbleSort is from i = 1 to i < 16 and j = 0 and j < 15. The above given version has the disadvantage that it does little more swaps than needed, but still produces the correct result at the end. See my comment for its implementation in ideone.com environment.

    Because you skip to check the first element after the first iteration.