c - Selection Sort troubles with keeping pointers in correct spots post sort -
c - Selection Sort troubles with keeping pointers in correct spots post sort -
i'm trying execute selection sort sort goals scored. have 3 categories; goals, assists, names. can correctly sort goals , maintain players goal's , assists in right spots after sort, when seek move names right spot after sort moves first letter of name. here's code. help!
void sortplayersbygoals(int* goals, int* assists, char** names, int size) { int lh, rh, i, tempg, tempa, tempn; for(lh = 0; lh < size; lh++) { rh = lh; for(i = lh; < size; i++) { if(goals[i] > goals[rh]) { rh = i; } tempg = goals[lh]; tempa = assists[lh]; tempn = *names[lh]; goals[lh] = goals[rh]; *names[lh] = *names[rh]; assists[lh] = assists[rh]; goals[rh] = tempg; *names[rh] = tempn; assists[rh] = tempa; } } }
here's output if helps show problem..
pre-sort name goals assists redden 2 0 berglund 5 2 jackman 2 0 stewart 4 0 oshie 3 5 mcdonald 2 4 pietrangelo 2 7 perron 2 6 tarasenko 5 5 post-sort name goals assists tedden 5 5 berglund 5 2 sackman 4 0 otewart 3 5 rshie 2 0 mcdonald 2 4 pietrangelo 2 7 perron 2 6 jarasenko 2 0
void sortplayersbygoals(int* goals, int* assists, char** names, int size) { /* names array of pointers char */ int lh, rh, i, tempg, tempa; char *tempn; /* pointer 1 name */ for(lh = 0; lh < size; lh++) { rh = lh; for(i = lh; < size; i++) { if(goals[i] > goals[rh]) { rh = i; } tempg = goals[lh]; tempa = assists[lh]; tempn = names[lh]; /* names[lh] pointer name in pos lh */ goals[lh] = goals[rh]; names[lh] = names[rh]; /* swap pointers */ assists[lh] = assists[rh]; goals[rh] = tempg; names[rh] = tempn; /* , not first letter */ assists[rh] = tempa; } } }
c string sorting pointers selection
Comments
Post a Comment