c# - 确定最大值的有效方法(有或没有偏移)

标签 c# algorithm sorting data-structures sortedlist

我目前在循环中使用 SortedList 来按降序对一些值进行排序:

for(...)
{
    float rawValue;
    float offset;
    sortedList.Add(rawValue + offset, index);
}

如果我们按原始值对条目进行排序而不使用偏移量?

显而易见的解决方案是在同一个循环中填充另一个 sortedRawValuesList,但我认为有更快、更高效的内存实现方法吗?

谢谢!

最佳答案

您能否在迭代时简单地跟踪最高的 rawValue?如果偏移量在每次迭代中都发生变化,您可能还想保存偏移量。

float highestRawVal = float.MinVal;
float offset_ForHighestRawVal = float.MinVal;
for(...)
{
    float rawValue;
    float offset;
    sortedList.Add(rawValue + offset, index);
    if(highestRawVal < rawVal)
    {
        highestRawVal = rawValue;
        offset_ForHighestRawVal = offset;
    }
}

if (highestRawVal + offset_ForHighestRawVal == sortedList[0])
    Console.WriteLine("They Match");

然后您可以简单地检查它们是否匹配。

关于c# - 确定最大值的有效方法(有或没有偏移),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13165625/

相关文章:

c# - .NET 软件更新管理器

algorithm - 广度优先搜索的时间和空间复杂度

algorithm - 在排序列表中查找项目的索引

sorting - ElasticSearch双嵌套排序

mysql - 如何通过其中一列快速重新排序 MySQL 表?

javascript - 在 Mongoose 中对子文档进行排序

c# - 放大 Canvas 点击事件 winrt c#

c# - WPF:Datagrid - 动态应用 DataGridTemplateColumn.CellTemplate

c# - 如何开发与多个数据库管理系统兼容的 Web 应用程序

c++ - 大 O 表示法中的 next_permutation 时间复杂度