string - Sorting does not sort first item in the array -
currently 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 this:
tremblay alain 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 40.0 33.75 desjardins alex p 35.0 25.0 tardif guy 40.0 28.5 clinclin stephane o 40.0 20.75 lafleur marie 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 37.5 29.0 st-amour flavie p 40.0 25.0
i'm trying sort employe structure in alphabetical order name (char nome[25];). but, reason, dosnt sort first name , 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 have idea why, appreciate answer. thank in advance.
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: have confused selectionsort bubblesort. selectionsort goes i = 0
, j = + 1
i < 15
, j < 16
, whereas bubblesort i = 1
i < 16
, j = 0
, j < 15
. above given version has disadvantage little more swaps needed, still produces correct result @ end. see comment implementation in ideone.com environment.
because skip check first element after first iteration.
Comments
Post a Comment