H-Index - C Solution

1. Introduction

This post explores a problem frequently encountered in bibliometrics and academic research analysis - calculating the H-Index. The H-Index is a measure that combines the productivity and citation impact of a researcher's publications. It's a useful metric for evaluating the significance of a researcher's scholarly output.

Problem

Given an array of integers citations, where citations[i] is the number of citations a researcher received for their ith paper, the task is to return the researcher's h-index. The h-index is defined as the highest number h such that the researcher has published at least h papers that have each been cited at least h times.

2. Solution Steps

1. Sort the citations array in descending order.

2. Iterate through the sorted array.

3. For each index i, check if the number of citations is greater than or equal to i+1 (since array index starts at 0).

4. The maximum i+1 satisfying this condition is the h-index.

5. If no such condition is met, return 0.

3. Code Program

#include <stdio.h>
#include <stdlib.h>

// Comparator function for descending order sorting
int compare(const void *a, const void *b) {
    return *(int*)b - *(int*)a;
}

// Function to calculate the h-index
int hIndex(int* citations, int citationsSize) {
    // Sort the array in descending order
    qsort(citations, citationsSize, sizeof(int), compare);

    // Iterate through the array to find the h-index
    for (int i = 0; i < citationsSize; i++) {
        if (citations[i] < i + 1) {
            return i;
        }
    }
    return citationsSize; // If all papers have citations >= their rank
}

// Main function to test the hIndex function
int main() {
    int citations1[] = {3, 0, 6, 1, 5};
    int citationsSize1 = sizeof(citations1) / sizeof(citations1[0]);
    printf("H-Index (Example 1): %d\n", hIndex(citations1, citationsSize1));

    int citations2[] = {1, 3, 1};
    int citationsSize2 = sizeof(citations2) / sizeof(citations2[0]);
    printf("H-Index (Example 2): %d\n", hIndex(citations2, citationsSize2));

    return 0;
}

Output:

H-Index (Example 1): 3
H-Index (Example 2): 1

Explanation:

1. qsort(citations, citationsSize, sizeof(int), compare); - Sort the citations array in descending order.

2. for (int i = 0; i < citationsSize; i++) - Iterate through the sorted array.

3. if (citations[i] < i + 1) - Check if the number of citations is less than the paper count (i + 1).

4. return i; - If the condition is met, return the current h-index.

5. return citationsSize; - If all papers have citations greater than or equal to their rank, return the size of the array as the h-index.

6. The main function tests the hIndex function with two examples and prints the calculated h-index.


Comments