📜  使用Binary Search查找排序引文的H索引

📅  最后修改于: 2021-05-18 00:53:17             🧑  作者: Mango

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

例子:

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

时间复杂度: O(N)

高效的方法:想法是使用二进制搜索来优化上述方法。 H索引的范围可以是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)