How to rank elements of an array in C

1 Answer

0 votes
#include <stdio.h>

/*
    FUNCTION: sort_descending
    -------------------------
    Sorts an integer array in *descending* order using simple bubble sort.
    (We use bubble sort here for clarity, not performance.)
*/
void sort_descending(int arr[], int n) {
    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < n - 1 - i; j++) {
            if (arr[j] < arr[j + 1]) {
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
}

/*
    FUNCTION: getRanks
    -------------------
    Computes competition ranking for an array of numbers.

    Ranking rules (Competition Ranking):
    ------------------------------------
    - Highest value gets rank 1
    - Equal values share the same rank
    - Next rank increases by the number of equal values

    Example:
        Values: 88, 4, 19, 4, 1, 31, 14, 1, 93, 17
        Ranks:   2, 7,  4, 7, 9,  3,  6, 9,  1,  5

    Implementation notes:
    ---------------------
    - We create a sorted copy of the array (descending)
    - We assign ranks to each unique value
    - We then map each original element to its rank
*/
void getRanks(const int arr[], int ranks[], int n) {
    int sorted[128] = { 0 }; // enough for typical examples
    int rankMapValue[128] = { 0 };
    int rankMapRank[128] = { 0 };
    int mapCount = 0;

    // Copy original array into sorted[]
    for (int i = 0; i < n; i++)
        sorted[i] = arr[i];

    // Sort descending
    sort_descending(sorted, n);

    // Assign ranks to each unique value
    int rank = 1;
    for (int i = 0; i < n; i++) {
        int value = sorted[i];

        // Check if value already has a rank
        int exists = 0;
        for (int k = 0; k < mapCount; k++) {
            if (rankMapValue[k] == value) {
                exists = 1;
                break;
            }
        }

        // If not assigned yet, assign rank
        if (!exists) {
            rankMapValue[mapCount] = value;
            rankMapRank[mapCount] = rank;
            mapCount++;
        }

        rank++; // next rank position
    }

    // Build ranking array for original order
    for (int i = 0; i < n; i++) {
        int value = arr[i];

        // Find rank for this value
        for (int k = 0; k < mapCount; k++) {
            if (rankMapValue[k] == value) {
                ranks[i] = rankMapRank[k];
                break;
            }
        }
    }
}

/*
    FUNCTION: printRanking
    -----------------------
    Prints the original array and its ranking.
*/
void printRanking(const int arr[], const int ranks[], int n) {
    printf("Original array: [ ");
    for (int i = 0; i < n; i++)
        printf("%d ", arr[i]);
    printf("]\n");

    printf("Ranking array : [ ");
    for (int i = 0; i < n; i++)
        printf("%d ", ranks[i]);
    printf("]\n");
}

int main() {
    int array[] = {88, 4, 19, 4, 1, 31, 14, 1, 93, 17};
    int n = sizeof(array) / sizeof(array[0]);

    int rankingArray[128] = { 0 };

    // Compute ranks
    getRanks(array, rankingArray, n);

    printRanking(array, rankingArray, n);

    return 0;
}


/*
run:

Original array: [ 88 4 19 4 1 31 14 1 93 17 ]
Ranking array : [ 2 7 4 7 9 3 6 9 1 5 ]

*/

 



answered 2 days ago by avibootz
edited 2 days ago by avibootz
...