Java:从数组切片中查找最大值的索引
我有一个大数组。我有一些 Java 代码,用于识别该大数组的子集/切片的起点和终点的索引。我需要从数组的选定子部分检索的唯一信息项是局部最大值和最小值的索引和值。我可以在指定范围内找到最大值和最小值的最快(且占用内存最少)的方法是什么?
这是我需要的代码的开始:
// Step One: declare new array and populate it
pts = new double[5000];
for (int i = 0; i < 5000; i++){ pts[i] = (value assigned by extraneous process);}
// Step Two: slice out section between indices 3600 and 3750
// what code do I write here?
// Step Three: find max value in the sliced section and return its index
// what code to I write here?
I have a large array. I have some Java code for identifying indices for start and end points for a subset/slice of that large array. The only information items that I need to retrieve from the selected subsection of the array are the indices and values of the local maximum and minimum. What is the fastest (and least memory intensive) way that I can find a max and min within the specified range?
Here is a start of what I need in terms of code:
// Step One: declare new array and populate it
pts = new double[5000];
for (int i = 0; i < 5000; i++){ pts[i] = (value assigned by extraneous process);}
// Step Two: slice out section between indices 3600 and 3750
// what code do I write here?
// Step Three: find max value in the sliced section and return its index
// what code to I write here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只需迭代所需的范围并记录最大和最小可见值:
Just iterate over the desired range and record maximum and minimum seen values:
如果不需要为切片创建数组的副本,则基本上可以一口气执行步骤 2 和 3:(
对于任何语法错误,我深表歉意,我一直在使用 Scala,语法有点不同)
If it's not necessary to create a copy of the array for your slice, you can essentially do steps 2 and 3 in one fell swoop:
(sorry for any syntax errors, I've been messing around with Scala and the grammar is a little different)
有一个循环遍历选定的小节一次。
在循环中,当找到新的最大值时,调整四个变量的值
maxValue
、maxIndex
、minValue
、minIndex
或最小值。循环结束后,您将获得最大值和最小值以及它们的位置。
不需要额外的内存,线性性能(只需一次遍历阵列的选定部分)。
Have a loop that goes over the selected subsection once.
In the loop, adjust the values of four variables
maxValue
,maxIndex
,minValue
,minIndex
as you find new maxima or minima.After the loop, you will have the maximum and minimum and their positions.
No extra memory needed, linear performance (just one pass over the selected part of the array).
如果您要经常这样做,您可以通过跟踪不同尺度的最大值/最小值来提高性能。
例如,如果您每 20 行保留一个列表,并且想要检查范围 55 - 184,则只需检查 5 个值 (55-59),然后检查 60-179 中的 6 个值,然后检查 60-179 中的 4 个值。 180 - 184,所以这是 15 次检查,而不是 130 次,速度提高了 20 倍。
当然,当数组发生变化时,您需要将存储桶标记为已更改,并定期更新它们。
If you're going to do this a lot, you could increase performance by keeping track of the maxima / minima at different scales.
For instance if you keep a list for every 20 rows, and you want to check the range 55 - 184, you'd only need to check 5 values (55-59), then 6 values from 60-179, then 4 values from 180 - 184, so that's 15 checks rather than 130, a 20x speed increase.
Of course, you'd need to mark your buckets as changed when the array changes and periodically update them.