如何按价格和速度对二维 100 张汽车图像的 10 x 10 网格进行排序?
这是场景。
我有一百个汽车物品。每辆车都有速度属性和价格属性。我想将汽车的图像排列在网格中,以便最快和最昂贵的汽车位于右上角,最慢和最便宜的汽车位于左下角,所有其他汽车都位于网格中的适当位置。
我需要为此使用什么样的排序算法,您有什么建议吗?
编辑:结果不需要精确 - 实际上我正在处理一个更大的网格,所以如果汽车大致聚集在正确的位置就足够了。
Here's the scenario.
I have one hundred car objects. Each car has a property for speed, and a property for price. I want to arrange images of the cars in a grid so that the fastest and most expensive car is at the top right, and the slowest and cheapest car is at the bottom left, and all other cars are in an appropriate spot in the grid.
What kind of sorting algorithm do I need to use for this, and do you have any tips?
EDIT: the results don't need to be exact - in reality I'm dealing with a much bigger grid, so it would be sufficient if the cars were clustered roughly in the right place.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
只是一个受坎托先生启发的想法:
,距离可能类似于
应用加权(视觉上)必要的
结果(镜像,左上角是最好的):
Just an idea inspired by Mr Cantor:
based on a²+b²=c², distance could be something like
apply weighting as (visually) necessary
Result (mirrored, top left is best):
将其视为两个问题:
1:生成排序列表
2:将排序列表的成员放入网格中
排序只是您更精确地定义规则的问题。 “先最快、最贵”是行不通的。我的 100,000 英镑劳斯莱斯,最高时速 120 英里,还是我的强化版 Mini,价值 50,000 英镑,最高时速 180 英里,哪一个先出现?
拿到清单后,您将如何填写?第一个和最后一个很容易,但是第二个去哪里呢?沿着顶部还是向下?那么下一步,沿着行,沿着列,之字形?你必须做出决定。之后编码应该很容易。
Treat this as two problems:
1: Produce a sorted list
2: Place members of the sorted list into the grid
The sorting is just a matter of you defining your rules more precisely. "Fastest and most expensive first" doesn't work. Which comes first my £100,000 Rolls Royce, top speed 120, or my souped-up Mini, cost £50,000, top speed 180?
Having got your list how will you fill it? First and last is easy, but where does number two go? Along the top or down? Then where next, along rows, along the columns, zig-zag? You've got to decide. After that coding should be easy.
我想你想要的是让具有“相似”特征的汽车聚集在附近,此外,成本总体上向右增加,速度总体上向上增加。
我会尝试遵循以下方法。假设您有 N 辆车,并且您想将它们放入 X * Y 网格中。假设 N == X * Y。
这是优化问题的标准“本地搜索”方法。这里基本上是一个简单的组合优化问题。另一种可以尝试的方法可能是使用自组织映射 (SOM),在矩阵中预置速度和成本梯度。
I guess what you want is to have cars that have "similar" characteristics to be clustered nearby, and additionally that the cost in general increases rightwards, and speed in general increases upwards.
I would try to following approach. Suppose you have N cars and you want to put them in an X * Y grid. Assume N == X * Y.
This is a standard "local search" approach to an optimization problem. What you have here is basically a simple combinatorial optimization problem. Another approaches to try might be using a self-organizing map (SOM) with preseeded gradient of speed and cost in the matrix.
基本上,您必须将速度或价格之一作为主要值,然后获取具有与该主要值相同的值的汽车,并按升序/降序对这些值进行排序,并且主要值也根据需要按升序/降序排列。
示例:
c1(20,1000) c2(30,5000) c3(20, 500) c4(10, 3000) c5(35, 1000)
让我们假设 Car(speed,price) 作为上面列表中的度量和主要度量是速度。
1 获取速度最小的汽车
2 然后获取所有具有相同速度值的汽车
3 将这些值按照汽车价格升序排列
4 获取下一个具有下一个最小速度值的汽车,并重复上述过程
c4(10, 3000)
c3(20, 500)
c1(20, 1000)
c2(30, 5000)
c5(35, 1000)
如果您发布您使用的语言,我们会很有帮助,因为某些语言结构使这更容易实现。例如,在这种情况下,LINQ 使您的生活变得非常轻松。
编辑:
现在你得到了列表,根据将这些汽车项目放入网格中,除非你知道会有这么多数量的具有这些值的预定汽车,否则你不能做任何期望的事情就像您现在所做的那样,具有一些固定的网格大小。
一种选择是使用不均匀的网格,如果您愿意,每行都有特定速度的汽车项目,但这仅适用于当您知道将有相当数量的汽车具有相同的速度值时。
因此,每一行的网格中都会显示速度相同的汽车。
谢谢
Basically you have to take one of speed or price as primary and then get the cars with the same value of this primary and sort those values in ascending/descending order and primaries are also taken in the ascending/descending order as needed.
Example:
c1(20,1000) c2(30,5000) c3(20, 500) c4(10, 3000) c5(35, 1000)
Lets Assume Car(speed, price) as the measure in the above list and the primary is speed.
1 Get the car with minimum speed
2 Then get all the cars with the same speed value
3 Arrange these values in ascending order of car price
4 Get the next car with the next minimum speed value and repeat the above process
c4(10, 3000)
c3(20, 500)
c1(20, 1000)
c2(30, 5000)
c5(35, 1000)
If you post what language you are using them it would we helpful as some language constructs make this easier to implement. For example LINQ makes your life very easy in this situation.
Edit:
Now you got the list, as per placing this cars items into the grid unless you know that there will be this many number of predetermined cars with these values, you can't do anything expect for going with some fixed grid size as you are doing now.
One option would be to go with a nonuniform grid, If you prefer, with each row having car items of a specific speed, but this is only applicable when you know that there will be considerable number of cars which has same speed value.
So each row will have cars of same speed shown in the grid.
Thanks
10x10 约束是否必要?如果是的话,您必须有十种速度和十种价格,否则该图就没有多大意义。例如,如果最快的汽车不是最贵的,会发生什么?
我宁愿建议您使网格大小等于,
那么这将是按两个轴排序的(相当)简单的情况。
Is the 10x10 constraint necessary? If it is, you must have ten speeds and ten prices, or else the diagram won't make very much sense. For instance, what happens if the fastest car isn't the most expensive?
I would rather recommend you make the grid size equal to
then it would be a (rather) simple case of ordering by two axes.
如果数据源自数据库,那么您应该在从数据库中获取数据时对它们进行排序。这应该只意味着在查询末尾附近、
LIMIT
部分之前添加ORDER BY speed,price
(其中“speed”和“price”是查询的名称)适当的字段)。正如其他人所说,“最快且最昂贵”是一件很难做到的事情,您应该首先选择一个进行排序。但是,可以使用此算法进行近似:
这确保了 A 车比 B 车更快、更贵,因此它会排在列表的前面。一个值较高而另一个值较低的汽车会被粗略地排序。我建议将这些值存储在数据库中并根据您的选择进行排序。
将它们放入 10x10 网格中很容易。开始输出项目,当达到 10 的倍数时,开始一个新行。
If the data originates in a database, then you should order them as you fetch them from the database. This should only mean adding
ORDER BY speed, price
near the end of your query, but before theLIMIT
part (where 'speed' and 'price' are the names of the appropriate fields).As others have said, "fastest and most expensive" is a difficult thing to do, you ought to just pick one to sort by first. However, it would be possible to make an approximation using this algorithm:
This ensures that is car A is faster and more expensive than car B, it gets put ahead on the list. Cars where one value is higher but the other is lower get roughly sorted. I'd recommend storing these values in the database and sorting as you select.
Putting them in a 10x10 grid is easy. Start outputting items, and when you get to a multiple of 10, start a new row.
另一种选择是对每辆车应用分数
0 .. 200%
,并按该分数排序。例子:
Another option is to apply a score
0 .. 200%
to each car, and sort by that score.Example:
嗯……冒泡排序在这里可能是简单的算法。
在以下情况下,两个相邻元素的顺序“错误”:
a)它们是水平邻居,左边的比右边的慢,
b) 它们是垂直邻居,顶部的一个比底部的便宜。
但我实际上不确定这个算法是否会针对每个数据停止。我几乎可以肯定它非常慢:-)。它应该很容易实现,并且经过一定次数的迭代后,部分结果可能足以满足您的目的。您还可以首先使用此处提到的其他方法之一生成数组。它还会保持您的阵列形状状况。
编辑:现在证明任何事情都为时已晚,但我在 python 中做了一些实验。看起来 100x100 的随机数组可以在几秒钟内以这种方式排序,而且我总是设法获得完整的二维排序(也就是说:最后我得到了错误排序的邻居)。假设 OP 可以预先计算这个数组,他可以将任何合理数量的汽车放入数组中并获得合理的结果。实验代码:http://pastebin.com/f2bae9a79(你需要matplotlib,我也推荐ipython)。
iterchange
是那里的排序方法。Hmmm... kind of bubble sort could be simple algorithm here.
Two neighbour elements are in "wrong order" when:
a) they're horizontal neighbours and left one is slower than right one,
b) they're vertical neighbours and top one is cheaper than bottom one.
But I'm not actually sure if this algorithm stops for every data. I'm almost sure it is very slow :-). It should be easy to implement and after some finite number of iterations the partial result might be good enough for your purposes though. You can also start by generating the array using one of other methods mentioned here. Also it will maintain your condition on array shape.
Edit: It is too late here to prove anything, but I made some experiments in python. It looks like a random array of 100x100 can be sorted this way in few seconds and I always managed to get full 2d ordering (that is: at the end I got wrongly-ordered neighbours). Assuming that OP can precalculate this array, he can put any reasonable number of cars into the array and get sensible results. Experimental code: http://pastebin.com/f2bae9a79 (you need matplotlib, and I recommend ipython too).
iterchange
is the sorting method there.