arrays - 4 元素数组中的两个最大数字

标签 arrays algorithm assembly

我正在尝试找到一种方法来找到 4 个元素的数组中最大的两个数字,我现在使用的方法是这样的:
开始于

x = array[1]  
y = array[2]  
compare x,y  
if y > x then switch the values "because i want x to have the greatest value"  
read array[3] if it greater than x then make it the new x and give x value to y  
else if check if it greater than y if yes then make it the new y  

但是对比太多了,想减少。你们有更好的算法吗?

最佳答案

是的,实际上您会对每个指标进行大量比较。 为什么根本找不到最大值,将其从数组中删除然后找到最大值?

你的算法将是 O(2n) 所以 O(n)

关于arrays - 4 元素数组中的两个最大数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26805449/

相关文章:

java - 如何检查数组中的元素是否按照出现次数排序?

php - 查找数组值之间的值

java 。对象实例发生奇怪的变化

algorithm - 回文检测效率

windows - x86 程序参数位置

delphi - 如何从物理内存中读取数据?

java - 以编程方式区分最终参数数组和最终参数省略号的任何方法?

javascript - 生成多数组的排列

java - 二进制字符串到整数转换器中的错误

assembly - 为什么 x86 nopl 指令需要一个操作数?