排序对象使用$ P $排序的值的pdefined列表对象、列表、pdefined

2023-09-11 02:20:46 作者:将往事深埋

我想知道什么是对象的数组排序以相同的顺序为不同阵列的最快方式。

I was wondering what would be the fastest way to sort an array of objects in the same order as a different array.

下面是在C#中的一个例子:

Here is an example in C#:

class MyClass
{
    public MyClass(int value)
    {
        this.value = value;
    }
    int value;
    public int Value
    {
        get { return value; }
        set { this.value = value; }
    }
}


    static List<int> sortedValuesList;
    static List<MyClass> objectList;

什么是相同的顺序sortedValues​​List排序链表的最快的方法? 有可能是具有相同值的多个对象

What is the fastest way of sorting objectList in the same order as sortedValuesList? There might be multiple objects with the same value.

我已经有简单的算法,可以做到这一点,但它是为O(n ^ 2),并且需要额外的内存。

I already have simple algorithm that can do it, but it's O(n^2) and requires extra memory.

编辑: 我想这是不是清楚我想要做的。 比方说,一个用户看到销售人员的数据网格在屏幕上。他可以用他想要的任何列进行排序。现在正在显示的用户点击一个按钮和客户的表。每一个客户参考的销售人员之一。我想要的客户名单,总部设在previous数据网格销售人员的顺序进行排序。

I guess it's not clear what I'm trying to do. Let's say a user sees a data grid of salespeople on the screen. He can sort them by any column he wants. Now the user clicks on a button and a table of customers is being shown. Every customer references one of the salespeople. I want to sort the customer list, based on the order of salespeople in previous data grid.

这只是一个理论性的问题,因为我并不需要更多的性能。我只是想知道,如果当你需要使用一个查找表来比较的对象有一些不错的排序算法。

It's only a theoretical question as I don't need more performance. I was just wondering if there is some nice sorting algorithm when you need to use a lookup table to compare objects.

推荐答案

它分解成步骤:

穿过你的sortedValues​​List和构建值的地图 - >索引位置;这是O(n log n)的 穿过你的对象,并添加索引在临时区域(再次,为O(n log n)的) 排序你的临时字段列表(也为O(n log n)的)

总的算法,为O(n log n)的。

Total algorithm, O(n log n).

另外,如果你不希望有划痕场,你可以在每次查询通过地图的排序键整体为O(n(log n)的^ 2)

Alternatively, if you don't want to have the scratch field, you can look up the sort key via the map each time for an overall O(n (log n)^2)