简体   繁体   English

如何在 Java 中通过 MergeSort 对点数组进行排序?

[英]How to sort an Array of Points by MergeSort in Java?

我创建了 Point 类,并且有一个 Point 对象的数组列表,我想按归并排序对其进行排序。

Assuming you are talking about 2D or 3D points, you would have to think about defining a metric such that they are orderable.假设您在谈论 2D 或 3D 点,您将不得不考虑定义一个指标,以便它们可以排序。

Consider the exampe points (1,0) and (0,1) in 2D space.考虑二维空间中的示例点(1,0)(0,1) Is (1,0) > (0,1) ?(1,0) > (0,1)吗? Or (1,0) < (0,1) ?还是(1,0) < (0,1) It really depends on the application.这真的取决于应用程序。

You could eg take just the first component as metric, then it would be (1,0) > (0,1) .例如,您可以仅将第一个组件作为度量标准,那么它将是(1,0) > (0,1) Or you could take the distance from a specific point, eg the origin (0,0) , in which case they would be equal.或者您可以从特定点(例如原点(0,0)获取距离,在这种情况下它们将相等。

Anyways, after you have decided on that, you can look up existing implementations of sorting algorithms (you seem to be determined about MergeSort though), on the internet.无论如何,在您决定之后,您可以在互联网上查找排序算法的现有实现(尽管您似乎确定了 MergeSort)。 Here is one example. 是一个例子。

Most interfaces to sort function in various programming languages require either a key function (eg "get first component" or "distance from origin"), or a custom comparison operator.大多数在各种编程语言中排序函数的接口都需要一个关键函数(例如“获取第一个组件”或“与原点的距离”),或者一个自定义的比较运算符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM