为什么我得到两个不同的结果相同的设计? [英] Why I am getting two different results for the same design?

查看:164
本文介绍了为什么我得到两个不同的结果相同的设计?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

快速的问题:

我有坐标的一堆(云)我倾向于找一大堆的四个角坐标。和角落我的意思是:

I have a bunch (cloud) of coordinates and I tend to find the four corner coordinates of the whole bunch. And by corner I mean:

MyDesiredResult = {SmallestX,BiggestY,BiggestX,SmallestY}

MyDesiredResult = { SmallestX, BiggestY, BiggestX, SmallestY }

我用这个的老套路让我的价值和它的是正确的

I use this Old Routine to get my values and it is the CORRECT ONE:

double smallestX = MyCloud[0].X;       // assing X of first element to my result controller
var tempCoordPoint = MyCloud[0];       // assign first element to my result controller

for (int i = 0; i < MyCloud.Count; i++)
{
    if (smallestX > MyCloud[i].X)      // find minimum X
    {
        smallestX = MyCloud[i].X;
        tempCoordPoint = MyCloud[i];
    }
}

MyResult.Add(tempCoordPoint);           // add to my list



不过,我需要做的四次(四个结果)。所以我想,改成这个新常规,我只使用一次,以优化我的代码:

However, I would need to do that Four Times (for the Four results). So I am trying to optimize my code by changing it to this New Routine which I use only once:

List<CoordPoint> MySortedList = MyCloud.Select(c => new CoordPoint { X = c.X, Y = c.Y, Z = c.Z, Color = c.Color }).ToList();

MySortedList.Sort((c1, c2) => c1.X.CompareTo(c2.X));     // sort on X
var temp = MySortedList[MySortedList.Count - 1];         // hold biggest X in a temp variable
MyResult.Add(MySortedList[0]);                           // add smallest X to my result

MySortedList.Sort((c1, c2) => c1.Y.CompareTo(c2.Y)); ;   // sort on Y
MyResult.Add(MySortedList[MySortedList.Count - 1]);      // add biggest Y to my result 
MyResult.Add(temp);                                      // add biggest X to my result
MyResult.Add(MySortedList[0]);                           // add smallest Y to my result



但它给了不同的结果。我会想展示了一个输入,电流输出和期望的输出。我可以跳过样本输入(巨大的负荷),并显示结果。 ?任何人都可以点我对我在做什么错

But it gives different results. I would want to show a sample input, current output and a desired output. I can skip the sample input (huge load) and show the results. Could anyone point me towards what I am doing wrong?

有关同一输入:

结果从老套路

(0,4),(15,12),(23,6),(19 ,0)

(0, 4), (15, 12), (23, 6), (19, 0)

导致新的常规

(0,4),(18,12),(23,6),(18,0)

(0, 4), (18, 12), (23, 6), (18, 0)

推荐答案

我会回答你的问题与另一个问题

I will answer your question with another question:

如果两个点具有相同的Y坐标和Y坐标恰好是最小或最大,会发生什么?同样对于X坐标?

What happens if two points have the same Y coordinate and that Y coordinate happens to be the minimum or maximum? Likewise for the X coordinate?

让我说明一个例子。比方说,你有以下4点:

Let me illustrate with an example. Let's say you had these 4 points:

(0,0),(1,0),(0,1),(1 ,1)

(0, 0), (1, 0), (0, 1), (1, 1)

您独创的算法将返回:

(0,0),(0,1),(1,0),(0,0)

(0, 0), (0, 1), (1, 0), (0, 0)

现在让我们说我们把那些原来的4分,打乱他们:

Now let's say we took those original 4 points and shuffled them:

(1,1),(0,1),(1, 0)(0,0)

(1, 1), (0, 1), (1, 0), (0, 0)

如果您在运行原来的算法,你会得到这样的:

If you run your original algorithm on that, you'll get this:

(0,1),(1,1),(1,1),(1,0)

(0, 1), (1, 1), (1, 1), (1, 0)

根据你原来的算法是正确的,但我刚刚给它相同的点的集合在两个不同的订单,并得到了两个不同的答案。因此,这问题的答案是正确的?什么是实际的预期结果?

According to you, the original algorithm is correct, but I've just given it the same set of points in two different orders and gotten two different answers. So which of the answers is correct? What is the actual expected result?

现在,还有一个原因,我没有提供新的算法的结果,那是因为我不知道你的新算法会产生。为什么我不知道的原因是,列表< T>的.sort 执行不稳定排序。这意味着,比较平等两个元素不一定会为了留下。所以,如果我们的投入是(0,0),(1,0),(0,1),(1,1)以下全部是经过有效的可能性试图用X排序坐标:

Now, there's a reason I didn't provide the results of your new algorithm and that's because I don't know what your new algorithm would produce. The reason why I don't know is that List<T>.Sort performs an unstable sort. That means that two elements that compare "equal" will not necessarily be left in order. So, if our input was (0, 0), (1, 0), (0, 1), (1, 1) all of the following are valid possibilities after trying to sort by X coordinate:

(0,0),(0,1),(1,0),(1, 1)

(0, 0), (0, 1), (1, 0), (1, 1)

(0,1),(0,0),(1,0),(1,1)

(0, 1), (0, 0), (1, 0), (1, 1)

(0,1),(0,0),(1,1),(1,0)

(0, 1), (0, 0), (1, 1), (1, 0)

(0,0),( 0,1),(1,1),(1,0)

(0, 0), (0, 1), (1, 1), (1, 0)

列表< T&取代。排序可能产生的那些中的任何一个。如果您有更多的复制点¯x坐标,你将有更多的可能的顺序。这就是所谓的不稳定的原因是因为两个相等元素的相对顺序(如(0,0)(0,1))排序后不保留。排序算法可能会交换自己的立场。

List<T>.Sort could produce any one of those. If you have more duplicate X coordinates, you'll have even more possible orderings. The reason this is called unstable is because the relative order of two equal elements (e.g. (0, 0) and (0, 1)) is not preserved after sorting. The sorting algorithm might swap their positions.

这篇关于为什么我得到两个不同的结果相同的设计?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆