Topk算法(胡思乱想)
1 $a = array(2,13,800,42,8001,34,56,23,67,365,87665,54,68,3); 2 3 $k = 5; 4 5 $ok = []; 6 7 for($i=$k;$i>0 && count($a)>0;$i--) 8 { 9 $len = count($a); 10 $temp = ''; 11 $count=0;//统计过掉数 12 foreach ($a as $key => $value) { 13 $temp?"":$temp=$key; 14 if($a[$temp]<$value) 15 { 16 $temp = $key; 17 } 18 24 if(++$count>=$len-$i-1) 25 {
break; 28 } 29 } 30 31 $ok[] = $a[$temp]; 32 unset($a[$temp]); 33 } 34 35 print_r($ok);