简体   繁体   English

C# 插入排序列表(升序+降序)

[英]C# Insertion sort List (ascending + descending)

I am developing a game in Unity, c#.我正在 Unity 中开发一款游戏,c#。 I have a large Vector3 list of 16k elements that has holds x position, y for dic key and z position.我有一个包含 16k 个元素的大型 Vector3 列表,其中包含 x position、y 用于 dic 键和 z position。

The list gets sorted very often but only 2750 of that list gets sorted and the rest remains unsorted.该列表经常被排序,但该列表中只有 2750 个被排序,并且 rest 仍未排序。

Right now I am using linq for a quickstort with orderby: list = list.OrderBy(x => x).ToList现在我正在使用 linq 与 orderby 进行快速排序: list = list.OrderBy(x => x).ToList
But it is not the right sort algorithm for my case.但这不是我的情况的正确排序算法。

I have no idea how to implement an Insertion sort for a Cector3 list.我不知道如何为 Cector3 列表实现插入排序。 Just did it with simple arrays.只需使用简单的 arrays 即可。 Is there maybe an Insertion sort (ascending and descending) for Vector3 list already in linq? linq 中是否已经存在 Vector3 列表的插入排序(升序和降序)?

Well turns out insertion sort is worse.事实证明,插入排序更糟。

Thats how I wrote it:我就是这样写的:

    static void sortVec3Array(Vector3[] arrayToSort, int startAt, int stopAt)
{
    int i, j;
    for (i = startAt+1; i < stopAt; i++)
    {
        float item = arrayToSort[i].x;
        int ins = 0;
        for (j = i - 1; j >= 0 && ins != 1;)
        {
            if (item < arrayToSort[j].x)
            {
                arrayToSort[j + 1].x = arrayToSort[j].x;
                j--;
                arrayToSort[j + 1].x = item;
            }
            else ins = 1;
        }
    }
}

Well came across Tim Sort which is kinda what I need.好吧,我遇到了 Tim Sort,这有点像我需要的。

I implemented it and it works like a charm.我实现了它,它就像一个魅力。 Ms is like 1.9 which is 3 times faster than the.Orderby algorithm and now useable for me. Ms 就像 1.9,比.Orderby 算法快 3 倍,现在对我有用。

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

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