如何对填充了随机数的二维数组进行排序

时间:2012-12-02 11:18:48

标签: c# arrays sorting 2d

我正在做课程作业,
我需要创建一个随机数的二维数组,并将它们分类为气泡或其他排序代码。我对单个数组很好,但问题是填充了随机数的二维数组,我只是不明白。 随机数应由(-I,I)区间组成,它是用户输入。抱歉英语不好,没有获得任何学位。在视觉C#窗体上工作。 寻找简单的情侣cicles方法。 例。 :A[MxN] ->>> B[MxN] (Sorted 1.....n)

2 个答案:

答案 0 :(得分:0)

获取随机数是微不足道的:

Random rnd;

for (int y = 0; y < h; y++)
    for (int x = 0; x < w; x++)
        array[y][x] = l - rnd.Next(2 * l + 1);

Random.Next()将返回介于0和给定参数之间的随机值(不包括参数;这是+ 1的原因。)

2D数组可以像1D数组一样进行排序,它只取决于您想要处理多行的方式,例如:它只是用于显示,还是你想为每一行排序等等。

答案 1 :(得分:0)

这是一个解决方案,首先按顺序对每一行的列进行排序,然后按第一列,第二列等对每行进行排序:

namespace StackOverflow.Demos
{

    class Program
    {

        public static void Main(string[] args) 
        {
            new Program();
            Console.WriteLine("Done");
            Console.ReadKey();
        }
        Program()
        {
            double[,] data = GenerateData();
            OutputData(data, "Before");
            SortData(ref data);
            OutputData(data, "After");
        }
        double[,] GenerateData()
        {
            Random randomGenerator = new Random(DateTime.UtcNow.Millisecond);
            double[,] data = new double[5, 5];
            for (int i = 0; i < data.GetLength(0); i++)
            {
                for (int j = 0; j < data.GetLength(1); j++)
                {
                    data[i, j] = (randomGenerator.NextDouble() * 2) - 1;
                }
            }
            return data;
        }
        void OutputData(double[,] data, string message)
        {
            Console.WriteLine("=====================");
            Console.WriteLine(message);
            Console.WriteLine("=====================");
            for (int i = 0; i < data.GetLength(0); i++)
            {
                for (int j = 0; j < data.GetLength(1); j++)
                {
                    Console.Write(data[i, j]);
                    Console.Write("\t");
                }
                Console.WriteLine();
            }
        }
        void SortData(ref double[,] data)
        {
            //sort sub arrays
            SortDataRows(ref data);
            //sort this array
            for (int i = 0; i < data.GetLength(0)-1; i++)
            {
                for (int j = i; j < data.GetLength(0); j++)
                {
                    for (int k = 0; k < data.GetLength(1); k++)
                    {
                        if (data[i, k].CompareTo(data[j, k]) < 0) //if already in order exit loop
                        {
                            break;
                        } else if (data[i, k].CompareTo(data[j, k]) > 0) //if out of order switch and loop
                        {
                            SwapRows(ref data, i, j);
                            break;
                        }//else orders are equal so far; continue to loop
                    }
                }
            }
        }
        void SortDataRows(ref double[,] data)
        {
            for (int row = 0; row < data.GetLength(0); row++)
            {
                for (int i = 0; i < data.GetLength(1) - 1; i++)
                {
                    for (int j = i; j < data.GetLength(1); j++)
                    {
                        if (data[row, i].CompareTo(data[row, j]) > 0)
                        {
                            Swap<double>(ref data[row, i], ref data[row, j]);
                        }
                    }
                }
            }
        }
        void Swap<T>(ref T a, ref T b)
        {
            T temp = a;
            a = b;
            b = temp;
        }
        void SwapRows(ref double[,]data, int i, int j)
        {
            for (int k = 0; k < data.GetLength(1); k++)
            {
                Swap<double>(ref data[i, k], ref data[j, k]);
            }
        }
    }

}

代码不是最好的(还没喝过一杯茶),但应该做你想要的。

这是一个更好的解决方案(不是使用2D数组,而是使用可以轻松转换为此类数组的结构):

唱System.Diagnostics;

namespace StackOverflow.Demos
{

    class Program
    {

        public static void Main(string[] args) 
        {
            new Program();
            Console.WriteLine("Done");
            Console.ReadKey();
        }
        Program()
        {
            List<List<double>> data = GenerateData(5, 5).ToList<List<double>>();
            OutputData(data,"Before");
            foreach (List<double> item in data)
            {
                item.Sort();
            }
            data.Sort(CompareListOfDoubles);
            OutputData(data,"After");
        }

        private IEnumerable<List<double>> GenerateData(int index1, int index2)
        {
            Random rnd = new Random(DateTime.UtcNow.Millisecond);
            List<double> result;
            for (int i = 0; i < index1; i++)
            {
                result = new List<double>(index2);
                for (int j = 0; j < index2; j++)
                {
                    result.Add(rnd.NextDouble() * 2 - 1);
                }
                yield return result;
            }
        }
        private void OutputData(List<List<double>> data, string message)
        {
            Console.WriteLine(message);
            foreach (List<double> list in data)
            {
                foreach (double datum in list)
                {
                    Console.Write(datum);
                    Console.Write("\t");
                }
                Console.WriteLine();
            }
        }
        static int CompareListOfDoubles(List<double> a, List<double> b)
        {
            for (int i = 0; i < a.Count; i++)
            {
                if (i > b.Count) return -1;
                if (a[i] > b[i]) return -1;
                if (a[i] < b[i]) return 1;
            }
            if (b.Count > a.Count) return 1;
            return 0;
        }
        double[,] ConvertListListDoubleTo2DArray(List<List<double>> data)
        {
            double[,] result = new double[data.Count, data[0].Count];
            for (int i = 0; i < result.GetLength(0); i++)
            {
                for (int j = 0; j < result.GetLength(1); j++)
                {
                    result[i, j] = data[i][j];
                }
            }
            return result;
        }
    }