void quick_sort(struct student *students, int low, int high) { int index_low, index_high; if (low < high) { for (index_low = low, index_high = high; index_low <= index_high;) { if (students[index_low].score >= students[low].score) index_low++; else if (students[index_high].score < students[low].score) index_high--; else { swapstr(students, index_low, index_high); swap_score(students, index_low, index_high); } } if (students[index_high].score > students[low].score) { swapstr(students, index_high, low); swap_score(students, index_high, low); } quick_sort(students, low, index_high - 1); quick_sort(students, index_high + 1, high); } }
void sort_leaderboard(Leaderboard *leaderboard) { /* The statistics for the clients should be displayed in ascending order of games won. If two or more players have the same number of games won then the player with the highest percentage of games won should be displayed last (percentage is the number of games won divided by the number of games played). If two or more players have the same number of games won and the same percentage of games won then display those players in alphabetical order. */ int num_scores = leaderboard->num_scores; bool swap; for (int i = 0; i < (num_scores - 1); i++) { Score *s1 = get_score_at_index(leaderboard,i); Score *s2 = get_score_at_index(leaderboard,i+1); swap = false; if (s1->games_won > s2->games_won) { swap = true; } else if (s1->games_won == s2->games_won) { if ((s1->games_won / s1->games_played) > (s2->games_won / s2->games_played)) { swap = true; } else if ((s1->games_won / s1->games_played) == (s2->games_won / s2->games_played)) { if (!alphabetical_order(s1->username, s2->username)) { swap = true; } } } if (swap) { swap_score(leaderboard,i,i+1); i = -1; } } }