我有一个正整数名单,我想了3个最大的值存储在变量h1,h2和h3.其余值无关紧要.
我认为与一个对其进行管理int*,并realloc为它充满,然后通过合适的排序算法阿婷的内存,但它真的值得吗?因为我不需要对整个数组进行排序,所以我就是这样做的:
if (currentVal > h3) {
h3 = currentVal;
if (currentVal > h2) {
h3 = h2;
h2 = currentVal;
if (currentVal > h1) {
h2 = h1;
h1 = currentVal;
}
}
}
Run Code Online (Sandbox Code Playgroud)
感觉就像一种愚蠢而静态的做法,但它有效.我应该实现一个排序算法,如果还有,任何建议可能适合吗?