📜  使用二分搜索查找排序引文的 H 索引

📅  最后修改于: 2021-09-03 04:04:29             🧑  作者: Mango

给定一个数组arr[]由 N 个以非递增顺序表示的整数组成,表示引用,任务是找到 H-index。

例子:

朴素的方法:一个简单的解决方案是从左到右遍历论文,并在引用i大于或等于索引时增加 H-index。

时间复杂度: O(N)

Efficient Approach:想法是使用二分搜索来优化上述方法。 H-index 可以在0N的范围内。要检查给定值是否可能,请检查citations[value]是否大于或等于value

  • 将二元搜索的搜索范围初始化为0 到 N
  • 找到范围的中间元素。
  • 检查引用的中间元素是否小于索引。如果是,则将左侧范围更新为中间元素。
  • 否则,检查引用的中间元素是否大于索引。如果是,则将正确的范围更新到中间元素。
  • 否则,给定的索引是引文的 H 索引。

下面是上述方法的实现:

C++
// C++ implementation of the
// above approach
 
#include 
using namespace std;
 
// Function to find the H-index
int hIndex(vector citations,
           int n)
{
 
    int hindex = 0;
 
    // Set the range for binary search
    int low = 0, high = n - 1;
 
    while (low <= high) {
        int mid = (low + high) / 2;
 
        // Check if current citations is
        // possible
        if (citations[mid] >= (mid + 1)) {
 
            // Check to the right of mid
            low = mid + 1;
 
            // Update h-index
            hindex = mid + 1;
        }
        else {
 
            // Since current value is not
            // possible, check to the left
            // of mid
            high = mid - 1;
        }
    }
 
    // Print the h-index
    cout << hindex << endl;
 
    return hindex;
}
 
// Driver Code
int main()
{
 
    // citations
    int n = 5;
    vector citations = { 5, 3, 3, 2, 2 };
 
    hIndex(citations, n);
}


Java
// Java implementation of the
// above approach
import java.io.*;
 
class GFG{
 
// Function to find the H-index
static int hIndex(int[] citations, int n)
{
    int hindex = 0;
 
    // Set the range for binary search
    int low = 0, high = n - 1;
 
    while (low <= high)
    {
        int mid = (low + high) / 2;
 
        // Check if current citations is
        // possible
        if (citations[mid] >= (mid + 1))
        {
 
            // Check to the right of mid
            low = mid + 1;
 
            // Update h-index
            hindex = mid + 1;
        }
        else
        {
 
            // Since current value is not
            // possible, check to the left
            // of mid
            high = mid - 1;
        }
    }
 
    // Print the h-index
    System.out.println(hindex);
 
    return hindex;
}
 
// Driver Code
public static void main (String[] args)
{
 
    // citations
    int n = 5;
    int[] citations = { 5, 3, 3, 2, 2 };
 
    hIndex(citations, n);
}
}
 
// This code is contributed by sanjoy_62


Python3
# Python3 implementation of the
# above approach
 
# Function to find the H-index
def hIndex(citations, n):
 
    hindex = 0
 
    # Set the range for binary search
    low = 0
    high = n - 1
 
    while (low <= high):
        mid = (low + high) // 2
 
        # Check if current citations is
        # possible
        if (citations[mid] >= (mid + 1)):
 
            # Check to the right of mid
            low = mid + 1
 
            # Update h-index
            hindex = mid + 1
 
        else:
             
            # Since current value is not
            # possible, check to the left
            # of mid
            high = mid - 1
 
    # Print the h-index
    print(hindex)
 
    return hindex
 
# Driver Code
 
# citations
n = 5
citations = [ 5, 3, 3, 2, 2 ]
 
# Function Call
hIndex(citations, n)
 
# This code is contributed by Shivam Singh


C#
// C# implementation of the
// above approach
using System;
 
class GFG{
 
// Function to find the H-index
static int hIndex(int[] citations, int n)
{
    int hindex = 0;
 
    // Set the range for binary search
    int low = 0, high = n - 1;
 
    while (low <= high)
    {
        int mid = (low + high) / 2;
 
        // Check if current citations is
        // possible
        if (citations[mid] >= (mid + 1))
        {
             
            // Check to the right of mid
            low = mid + 1;
 
            // Update h-index
            hindex = mid + 1;
        }
        else
        {
             
            // Since current value is not
            // possible, check to the left
            // of mid
            high = mid - 1;
        }
    }
 
    // Print the h-index
    Console.WriteLine(hindex);
 
    return hindex;
}
 
// Driver Code
public static void Main ()
{
 
    // citations
    int n = 5;
    int[] citations = { 5, 3, 3, 2, 2 };
 
    hIndex(citations, n);
}
}
 
// This code is contributed by sanjoy_62


Javascript


输出:
3

时间复杂度: O(logN)
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live