简体   繁体   English

确定数组中的最高和最低编号

[英]Determining Highest and Lowest Numbers in an Array

I'm trying to solve a problem where I need to write java code to find the two highest and the smallest number in an array given the below conditions: 我正在尝试解决一个问题,在给定以下条件的情况下,我需要编写Java代码以在数组中找到两个最大和最小的数字:

-Every element is a real number -每个元素都是一个实数

-Every element is random -每个元素都是随机的

Any ideas on the best approach? 关于最佳方法有什么想法吗?

You have to examine every number, so your best algorithm is linear in the length of the array. 您必须检查每个数字,因此最佳算法在数组长度上是线性的。

The standard approach is to just scan the array, keeping track of the two smallest and largest numbers that you've seen so far. 标准方法是仅扫描阵列,并跟踪您到目前为止所看到的两个最小和最大的数字。

So, given that firstMin , secondMin , firstMax , secondMax are respectively the smallest, second smallest, largest and second largest values that you've seen so far, on the next iteration of the loop: 因此,鉴于firstMinsecondMinfirstMaxsecondMax分别是到目前为止在循环的下一次迭代中看到的最小,第二最小,最大和第二最大值:

if (value > firstMax) {
    secondMax = firstMax;
    firstMax = value;
} 
else if (value > secondMax) {
    secondMax = value;
}

if (value < firstMin) {
    secondMin = firstMin; 
    firstMin = value;
}
else if (value < secondMin) {
    secondMin = value;
}

At the end of this block, we maintain the invariant that firstMin , secondMin , firstMax , secondMax are respectively the smallest, second smallest, largest and second largest values that you've seen so far. 在该块的最后,我们保持不变,即firstMinsecondMinfirstMaxsecondMax分别是到目前为止您所看到的最小,第二最小,最大和第二最大值。 This proves correctness. 这证明是正确的。

This algorithm is linear in the length of the array and examines each value exactly once and makes the minimum number of comparisons. 该算法在数组的长度上是线性的,并且只检查一次每个值,并进行最少的比较。 It is also O(1) in space, and is optimal in that it uses only four extra memory locations for the top and bottom two values. 它在空间上也是O(1) ,并且最佳方式是,它仅对顶部和底部两个值使用四个额外的存储位置。

Have a variable to keep track of the min, second_min, max, and second_max values that you have seen so far. 有一个变量来跟踪到目前为止您已经看到的min,second_min,max和second_max值。

You can go through the elements of the array one by one, and update your min/max variables accordingly. 您可以一个接一个地遍历数组的元素,并相应地更新最小/最大变量。 Here are some cases to consider: 以下是一些需要考虑的情况:

  • If current element is smaller than your min, save your min to second_min and update your min. 如果当前元素小于您的最小值,则将您的最小值保存为second_min并更新您的最小值。
  • If current element is larger than your max, save your max to second_max and update your max 如果当前元素大于最大值,则将最大值保存为second_max并更新最大值
  • If current element is smaller then second_min, but larger than min, update second_min only 如果当前元素小于second_min但大于min,则仅更新second_min
  • If current element is larger then second_max, but smaller than max, update second_max only 如果当前元素大于second_max,但小于max,则仅更新second_max

Is super-optimized performance necessary? 是否需要超级优化的性能? If not, 如果不,

Arrays.sort( array );

Look at the first and last two elements. 看一下前两个元素。

I think the best way is use the array as a heap or a special data structure if you can. 我认为最好的方法是将数组用作堆或特殊的数据结构。 Keeping the structure balance and sorted will be most efficient for you. 保持结构的平衡和分类对您来说是最有效的。 This implies keeping some elements empty at some indixes in your array since the index of the array will be information about your ADT. 这意味着将数组中某些索引处的某些元素保留为空,因为数组的索引将是有关ADT的信息。 http://en.wikipedia.org/wiki/Heap_(data_structure) http://zh.wikipedia.org/wiki/堆(数据结构)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM