排序数组以及相应的索引 [英] Sort Array along with the Corresponding indices

查看:87
本文介绍了排序数组以及相应的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个名为zz的一维数组,其中包含{5,3,8,1,9,6}.元素索引的当前顺序是:0,1,2,3,4,5.

现在,我想按升序对数组z进行排序,以使其对应的索引也根据其各自的元素进行排序.

例如:按升序排列的Elts:1、3、5、6、8、9

和相应的索引将是:3,1,0,5,2,4.

另一个例子:如果数组元素重复,我应该对以下代码进行什么添加"?预期指数Oreder:1,0,4,3,2

Hi,

I have a one-d Array named zz containing {5,3,8,1,9,6}. Current order of indices of the elements is : 0,1,2,3,4,5.

Now I want to sort the array z in ascending order in such a way that its corresponding indices also sort according to its respective elements.

Ex: Elts in Ascending order : 1,3,5,6,8,9

and Respective indices will be : 3,1,0,5,2,4.

Another Ex: What "Additions" should i make to the following code, if the Array Element repeats ?? Epected Oreder of Indices : 1,0,4,3,2

int[] A = new int[6] { 3, 2, 8, 5, 3 };  //{ 5, 3, 8, 1, 9, 6 };
            int[] B = new int[6];
            int[] index = new int[6];
            int flag = 0;
            Console.WriteLine("Array A:");
            for (int i = 0; i < A.Length; i++)
                Console.Write(A[i]);
            Console.WriteLine();
            Array.Copy(A, B, A.Length);
            Console.WriteLine("Array B:");
            for (int i = 0; i < B.Length; i++)
                Console.Write(B[i]);
            Console.WriteLine();
            Array.Sort(A);
            Console.WriteLine("Sorted Array B:");
            for (int i = 0; i < A.Length; i++)
                Console.Write(A[i]);
            Console.WriteLine();


            for (int i = 0; i < 6; i++)
            {
                for (int j = 0; j < 6; j++)
                {
                    flag = 0;
                    for (int k = 0; k < 6; k++)
                    {
                        if (A[j + i] == B[k])
                        {
                            index[i] = k;
                            flag = 1;
                            break;
                        }
                    }
                    if (flag == 1)
                        break;
                }
            }

            Console.WriteLine("Index Array index:");
            for (int i = 0; i < index.Length; i++)
                Console.Write(index[i]);
            Console.Read();



有没有现成的功能可用?希望问题清楚.请紧急提供解决方案.


在此先感谢您,



Is there any ready made function available ? Hope the question is clear..Kindly provide the solution on urgent basis.


Thanks in Advance,

推荐答案

您想将数组转换为int类型的列表,对其进行排序,然后将排序后的列表重新转换为数组.

You want to turn your array into a list of type int, sort that and turn the sorted list back into an array.

var intList = new List<int>(A);
intList.Sort();

var sortedArrayOfIntegers = intList.ToArray();
</int>



希望对您有所帮助.



hope this helps.


尝试一下.

Try this.

int[] A = { 3, 2, 8, 5, 3 };  //{ 5, 3, 8, 1, 9, 6 };
            int[] index = new int[A.Length];
            int[] result = new int[A.Length];
            Array.Copy(A, result, A.Length);
            for (int i = 0; i < A.Length; i++) index[i] = i;
            Console.WriteLine("Input Array:");
            for (int i = 0; i < A.Length; i++)
                Console.Write(A[i]);
            Console.WriteLine();
            Console.WriteLine("Original Index Array:");
            for (int i = 0; i < index.Length; i++)
                Console.Write(index[i]);
            Console.WriteLine();
            int lastIndex = 0;
            int tempValue = 0;
            for (int i = 0; i < A.Length; i++)
            {
                for (int j = i + 1; j < A.Length; j++)
                {
                    if (result[i] > result[j])
                    {
                        tempValue = result[i];
                        result[i] = result[j];
                        result[j] = tempValue;
                        lastIndex = index[i];
                        index[i] = index[j];
                        index[j] = lastIndex;
                    }
                }
            }

            Console.WriteLine("Result Array:");
            for (int i = 0; i < result.Length; i++)
                Console.Write(result[i]);
            Console.WriteLine();
            Console.WriteLine("Sorted Index Array:");
            for (int i = 0; i < index.Length; i++)
                Console.Write(index[i]);
            Console.Read();


这篇关于排序数组以及相应的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆