
本文介绍一种简洁、准确且符合定义的 php 实现方式,用于根据论文引用数组计算学者的 h 指数,核心逻辑基于排序后的位置比较,时间复杂度为 o(n log n),适用于各类科研数据场景。
本文介绍一种简洁、准确且符合定义的 php 实现方式,用于根据论文引用数组计算学者的 h 指数,核心逻辑基于排序后的位置比较,时间复杂度为 o(n log n),适用于各类科研数据场景。
H 指数(h-index)是衡量科研人员学术影响力的重要指标,其定义为:最大的整数 h,使得该作者至少有 h 篇论文每篇被引用不少于 h 次。例如,引用数组 [9, 7, 6, 2, 1] 经降序排列后,第 0 位(索引 0)对应值 9 ≥ 1?不参与判断;关键在于:当遍历到索引 i(从 0 开始)时,若当前论文引用数 $citations[i] ,则最大满足条件的 <code>h 即为 i(因为前 i 篇已满足“每篇 ≥ i 次”)。但更直观的实现是:将数组降序排列后,找到首个满足 $index ≥ $citation 的位置,该 $index 即为 h 值(注意:此处 $index 从 0 开始,而 h 是“篇数”,即自然计数,故直接返回 $index 即可——因前 $index 篇论文(索引 0 到 $index−1)共 $index 篇,且它们的引用均 ≥ $index)。
以下是推荐的 PHP 实现(匿名函数形式,可轻松转为普通函数或方法):
$hIndex = function(array $citations): int {
rsort($citations); // 降序排列:高引用在前
foreach ($citations as $index => $count) {
// 当前位置(0-indexed)表示「已有 index+1 篇论文」,
// 但定义要求「h 篇论文各 ≥ h 次」→ 等价于:第 index 篇(0-indexed)的引用数必须 ≥ index+1
// 因此,若 count = count → 此时 h = index 满足定义(前 index 篇均 ≥ index)
if ($index >= $count) {
return $index;
}
}
// 全部满足:如 [5,5,5,5,5] → index=4, count=5 → 4<p>✅ <strong>验证示例:</strong> </p><div class="aritcle_card flexRow artxards">
<div class="artcardd flexRow">
<a class="aritcle_card_img" rel="nofollow" href="/xiazai/skill5233" title="btpanel phpsite 宝塔面板PHP网站"><img
src="https://img.php.cn/upload/skill/000/000/081/179040786932301.jpg" alt="btpanel phpsite 宝塔面板PHP网站" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a>
<div class="aritcle_card_info flexColumn">
<a rel="nofollow" href="/xiazai/skill5233" title="btpanel phpsite 宝塔面板PHP网站" class="overflowclass">btpanel phpsite 宝塔面板PHP网站</a>
<p class="overflowclass">宝塔面板 PHP 网站管理:站点创建、删除、启停、PHP 版本切换、域名管理、SSL证书管理、伪静态管理、数据库管理</p>
</div>
<a rel="nofollow" href="/xiazai/skill5233" title="btpanel phpsite 宝塔面板PHP网站" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span>
</a>
</div>
</div><pre class="brush:php;toolbar:false;">echo $hIndex([9, 7, 6, 2, 1]) . "\n"; // 输出: 3
echo $hIndex([51, 27, 14, 14, 6, 2, 1, 0, 0, 0]) . "\n"; // 输出: 5
echo $hIndex([10, 9, 8, 7, 6, 5, 4, 3, 2, 1]) . "\n"; // 输出: 5
echo $hIndex([100, 100, 2, 2, 2, 2, 2, 2, 2, 2]) . "\n"; // 输出: 2
echo $hIndex([100, 100, 9, 8, 3, 2, 2, 1, 1, 0]) . "\n"; // 输出: 4 ⚠️ 注意事项:
- 输入数组无需预排序,函数内部自动
rsort()处理; - 空数组返回
0(count()为 0,循环不执行,最终return 0); - 包含零引用或负值(如有)需提前过滤,因 H 指数仅统计有效引用(Wikipedia 定义中引用数为非负整数);
- 该算法简洁高效,适合中小型数据集;若需处理百万级论文引用,可考虑二分搜索优化(先排序再二分查找最大 h),但日常科研分析中本实现已足够。
总结:H 指数的本质是排序后的“位置-数值”平衡点。PHP 中一行 rsort() 加一次线性扫描,即可精准捕获该临界值,兼顾可读性与实用性。
php免费学习视频:立即使用
踏上前端学习之旅,开启通往精通之路!从前端基础到项目实战,循序渐进,一步一个脚印,迈向巅峰!










