Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.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_Sorting_Graphics_Quicksort - Fatal编程技术网

C# 整数排序对

C# 整数排序对,c#,.net,sorting,graphics,quicksort,C#,.net,Sorting,Graphics,Quicksort,我有一个约100k个整数对的列表,如下所示: 0, 12 0, 14 0, 1 0, 8 0, 2 0, 4 0, 3 1, 5 1, 11 1, 8 1, 2 2, 7 2, 9 2, 4 2, 5 2, 13 3, 12 3, 10 3, 4 3, 6 ... 我需要把它们分类 0, 1 0, 2 0, 3 0, 4 0, 8 0, 12 0, 14 1, 2 1, 5 1, 8 1, 11 2, 4 2, 5 2, 7 2, 9 2, 13 3, 4 3, 6 ... 目前我正在做:

我有一个约100k个整数对的列表,如下所示:

0, 12
0, 14
0, 1
0, 8
0, 2
0, 4
0, 3
1, 5
1, 11
1, 8
1, 2
2, 7
2, 9
2, 4
2, 5
2, 13
3, 12
3, 10
3, 4
3, 6
...
我需要把它们分类

0, 1
0, 2
0, 3
0, 4
0, 8
0, 12
0, 14
1, 2
1, 5
1, 8
1, 11
2, 4
2, 5
2, 7
2, 9
2, 13
3, 4
3, 6
...
目前我正在做:

myList.Sort(comparer);
当比较器定义为:

class EdgeIntersectComparer : IComparer<EdgeIntersect>
{
   public int Compare(EdgeIntersect l1, EdgeIntersect l2)
   {
       if (l1.V1 < l2.V1)
          return -1;
       if (l1.V1 > l2.V1)
          return 1;

       if (l1.V2 < l2.V2)
          return -1;
       if (l1.V2 > l2.V2)
          return 1;

       return 0;
   }
}
class EdgeIntersectComparer:IComparer
{
公共整数比较(边接口l1、边接口l2)
{
如果(l1.V1l2.V1)
返回1;
如果(l1.V2l2.V2)
返回1;
返回0;
}
}

如何提高执行速度?有没有更聪明的方法来解决这个问题

谢谢

编辑:


测试了myList.OrderBy(e=>e.V1)。然后是By(e=>e.V2),速度较慢。

您在一篇已删除的帖子中评论说,
V1
已排序

此外,根据V1,列表已排序

我使用V1已经排序的数据做了一个测试,但是V2用随机数初始化。我发现这比你的方法快:

myList = myList.GroupBy(x => x.V1).SelectMany(x => x.OrderBy(y => y.V2)).ToList();

这仅在
V1
已排序的情况下有效。

您可以尝试使用数组而不是列表。(这取决于你的背景)。如果你不能:

Asuming:

 public class Pair
{
    public int First { get; private set; }
    public int Second { get; private set; }
    public Pair(int first, int second)
    {
        this.First = first;
        this.Second = second;
    }
}
列表是如何按第一项排序的,可能是这样的?不确定这是否会更快:

    public static List<Pair> FullOrderedList(List<Pair> SemiOrderedList)
    {
        List<Pair> FList = new List<Pair>();
        List<Pair> demi = new List<Pair>();
        int MaxNumber = SemiOrderedList.Count;
        int compared = 0;
        for (int i = 0; i < MaxNumber; i++)
        {
            int first = SemiOrderedList[i].First;
            if (compared == first)
            {
                demi.Add(SemiOrderedList[i]);
            }
            else
            {
                compared++;
                FList.AddRange(demi.OrderBy(x => x.Second));
                demi.Clear();
            }
        }
        return FList;
    }
公共静态列表FullOrderedList(列表SemiOrderedList)
{
List FList=新列表();
List demi=新列表();
int MaxNumber=SemiOrderedList.Count;
int=0;
对于(int i=0;ix.Second));
半透明();
}
}
回传;
}

通过优化比较器,可以获得较小的速度提升:

if (l1.V1 == l2.V1)
{
   if (l1.V2 > l2.V2) return 1;
   else return -1;
}
else if (l1.V1 < l2.V1)
          return -1;
else return 1;
if(l1.V1==l2.V1)
{
如果(l1.V2>l2.V2)返回1;
否则返回-1;
}
else if(l1.V1

最多要检查2条语句,而不是4条。

当前列表按对的第一个整数排序…@Chris:Edges被定义为一对整数…可能的重复我不知道它是否会更快(可能不会),但您可以尝试使用Linq:
var sorted=myList.OrderBy(e=>e.V1)。然后使用(e=>e.V2)“我能做些什么来提高执行速度?有没有更聪明的方法来解决这个问题?”。。。不确定是否有人在读那部分。