6

I need a float[] to be sorted. And I need to know where the old indices are in new array. That's why I can't use Array.Sort(); or whatever. So I would like to write a function that sorts the array for me and remembers from what index it took each value:

float[] input  = new float[] {1.5, 2, 0, 0.4, -1, 96, -56, 8, -45};
// sort
float[] output; // {-56, -45, -1, 0, 0.4, 1.5, 2, 8, 96};
int[] indices; // {6, 8, 4, 2, 3, 0, 1, 7, 5};

Size of arrays would be around 500. How should I approach this ? What sorting algorithm etc.


After solved: It always surprises me how powerful C# is. I didn't even though of it being able to do that task on it's own. And since I already heard that Array.Sort() is very fast I'll take it.

4

5 回答 5

13
float[] input = new float[] { 1.5F, 2, 0, 0.4F, -1, 96, -56, 8, -45 };
int[] indices = new int[input.Length];
for (int i = 0; i < indices.Length; i++) indices[i] = i;
Array.Sort(input, indices);
// input and indices are now at the desired exit state

基本上,2 参数版本对两个Array.Sort数组应用相同的操作,在第一个数组上运行实际的排序比较。这通常以相反的方式使用 - 通过所需的索引重新排列某些东西;但这也有效。

于 2013-07-01T08:14:43.667 回答
5

您可以使用 Array.Sort() 的重载,它采用两个数组,并根据对第一个数组的排序方式对第二个数组进行排序:

float[] input  = new [] { 1.5f, 2, 0, 0.4f, -1, 96, -56, 8, -45 };
int[] indices = Enumerable.Range(0, input.Length).ToArray();
Array.Sort(input, indices);
于 2013-07-01T08:14:15.073 回答
4

您可以创建一个新的索引数组,然后使用 Array.Sort 对它们进行排序并将它们input视为键:

float[] input = new float[] { 1.5F, 2, 0, 0.4F, -1, 96, -56, 8, -45 };
int[] indicies = Enumerable.Range(0, input.Length).ToArray();
Array.Sort(input, indicies);
于 2013-07-01T08:14:24.303 回答
0

如果你使用 linq:

    float[] input = new float[] { 1.5F, 2, 0, 0.4F, -1, 96, -56, 8, -45 };

    var result = input.Select(x => new { Value = x, Index = input.ToList().IndexOf(x)}).OrderBy(x => x.Value).ToList();

    // sort
    float[] output = result.Select(x => x.Value).ToArray();
    int[] indices = result.Select(x => x.Index).ToArray();

在结果中,您获得了具有值及其索引的对象。

于 2013-07-01T08:19:52.923 回答
0

AList<KeyValuePair<int,float>>和自定义分拣机也可以工作。每对的键保存原始索引。

    private void Form1_Load(object sender, EventArgs e)
    {           
        List<KeyValuePair<int,float>> data = new List<KeyValuePair<int,float>>
        {
             new KeyValuePair<int,float>(0,1.5f),
             new KeyValuePair<int,float>(1,2),
             new KeyValuePair<int,float>(2,0),
             new KeyValuePair<int,float>(3,0.4f),
             new KeyValuePair<int,float>(4,-1),
             new KeyValuePair<int,float>(5,96),
             new KeyValuePair<int,float>(6,-56),
             new KeyValuePair<int,float>(7,8),
             new KeyValuePair<int,float>(8,-45)
        };
        data.Sort(SortByValue);
        foreach (KeyValuePair<int, float> kv in data)
        {
            listBox1.Items.Add(kv.Key.ToString() + " - " + kv.Value.ToString());
        }


    }
    private int SortByValue(KeyValuePair<int, float> a, KeyValuePair<int, float> b)
    {
        return a.Value.CompareTo(b.Value);
    }
于 2013-07-01T08:47:37.943 回答