Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 固定排序列表/数据结构_C#_.net - Fatal编程技术网

C# 固定排序列表/数据结构

C# 固定排序列表/数据结构,c#,.net,C#,.net,我正在写一些东西来记录对象间各种方法的性能。我想找出最慢的10次。因此,我想要一个固定的排序列表,例如在我的例子10中。所以,每当我有一个新的时间,我只是插入它,它命令它。它将是固定的,所以在我插入第5次后(假设下面的示例将其限制为5次),列表将不会增长,但它只会将其插入列表中,并删除最小的值 例如 var-topTen=新的XXX(5); 三十、 插入(1); 三十、 插入(3); 三十、 插入(2); 三十、 插入(6); 三十、 插入(4); 三十、 插入(5); /* 前十名[0]为6

我正在写一些东西来记录对象间各种方法的性能。我想找出最慢的10次。因此,我想要一个固定的排序列表,例如在我的例子10中。所以,每当我有一个新的时间,我只是插入它,它命令它。它将是固定的,所以在我插入第5次后(假设下面的示例将其限制为5次),列表将不会增长,但它只会将其插入列表中,并删除最小的值

例如

var-topTen=新的XXX(5);
三十、 插入(1);
三十、 插入(3);
三十、 插入(2);
三十、 插入(6);
三十、 插入(4);
三十、 插入(5);
/*
前十名[0]为6
前十名[1]是5名
前十名[2]是第四名
前十名[3]是第三名
前十名[4]是2
*/

我本来打算为它写一些东西,但我只是想知道.net中是否已经有了任何东西。

通常,您可以使用堆来执行类似的操作。例如:

var heap = new BinaryHeap<int>();

for (int i = 0; i < 1000; ++i)
{
    var time = GetTimeSomehow();
    if (heap.Count < 5)
    {
        heap.Insert(time);
    }
    else if (time > heap.Peek())
    {
        // the new value is larger than the smallest value on the heap.
        // remove the smallest value and add this one.
        heap.RemoveRoot();
        heap.Insert(time);
    }
}
.NET Framework中没有可用的堆数据结构。不久前我发表了一篇简单的文章。请参阅。

尝试以下方法(未经测试):

int listLength=5

List<int> list = new List<int>(listLength+1);

void VerifyTime(int time) {
  list[listLength] = time;
  var i = listLength;
  while (listLength>0  &&  list[listLength] < list[listLength-1])
    swap(list, listLength, listLength-1);
}

void swap (List<int> l, int a, int b) {
  var temp = l[a];
  l[a] = l[b];
  l[b] = temp;
}
List List=新列表(listLength+1);
无效验证时间(整数时间){
列表[listLength]=时间;
var i=列表长度;
而(listLength>0&&list[listLength]

对于ListLength的任何小值,它都应该可以正常工作。

不是内置类。但是您可能会发现
MyPriorityQueue
实现很有用。它正是你想要做的。
while (heap.Count > 0)
{
    var time = heap.RemoveRoot();
    Console.WriteLine(time);
}
List<int> list = new List<int>(listLength+1);

void VerifyTime(int time) {
  list[listLength] = time;
  var i = listLength;
  while (listLength>0  &&  list[listLength] < list[listLength-1])
    swap(list, listLength, listLength-1);
}

void swap (List<int> l, int a, int b) {
  var temp = l[a];
  l[a] = l[b];
  l[b] = temp;
}