首页 > 解决方案 > 创建 1000 个数组并使用冒泡排序和选择排序 (C#) 对它们进行排序

问题描述

我是编程新手。C# 是我的第一门编程语言。

我有一个任务,我必须使用数组创建和测试冒泡排序算法和选择排序算法。我想我现在明白这些了。

作业的下一部分我遇到了一些麻烦。

我必须编写一个程序,询问用户一个数字 (n) 并创建 1000 个 n 大小的数组。

因此,如果用户输入 5 作为数字,我的程序必须创建并排序1000 个长度为 5 的数组。

我必须使用我创建的冒泡排序和选择排序方法。

在我这样做之后,我必须将一个名为running_time的变量初始化为 0。我必须创建一个迭代 1000 次的 for 循环,并且在循环体中我必须创建一个包含 n 个随机整数的数组。

然后我必须得到时间并将其设置为开始时间。我的教授说要注意排序是在每个数组构建后开始的,所以我应该只对排序过程进行计时。

然后我必须得到时间并将其设置为结束时间。我必须从结束时间中减去开始时间并将结果添加到总时间中。

程序运行后,请注意 1. 排序的项目数 2. 每个数组的平均运行时间(总时间/1000)

然后我必须使用 500、2500 和 5000 作为数组的大小重复该过程。

这是我创建一个包含 n 个空格并填充随机整数的数组的代码。

//Asks the user for number
        Console.WriteLine("Enter a number: ");
        n = Convert.ToInt32(Console.ReadLine());

        //Creates an array of the length of the user entered number
        int[] randArray = new int[n];

        //Brings in the random class so we can use it.
        Random r = new Random();

        Console.WriteLine("This is the array: ");

//For loop that will put in a random number for each spot in the array. 
        for (int i = 0; i < randArray.Length; i++) {
            randArray[i] = r.Next(n);
            Console.Write(randArray[i] + " ");
        }
        Console.WriteLine();

这是我的冒泡排序算法代码:

//Now performing bubble sort algorithm:
        for (int j = 0; j <= randArray.Length - 2; j++) {
            for (int x = 0; x <= randArray.Length - 2; x++) {
                if (randArray[x] > randArray[x + 1]) {
                    temp = randArray[x + 1];
                    randArray[x + 1] = randArray[x];
                    randArray[x] = temp;
                }
            }
        }

//For each loop that will print out the sorted array
        foreach (int array in randArray) {
            Console.Write(array + " ");
        }
        Console.WriteLine();

这是我的选择排序算法代码:

//Now performing selection sort algorithm
        for (int a = 0; a < randArray1.Length - 1; a++) {
            minkey = a;
            for (int b = a + 1; b < randArray1.Length; b++) {
                if (randArray1[b] < randArray1[minkey]) {
                    minkey = b;
                }
            }
            tempSS = randArray1[minkey];
            randArray1[minkey] = randArray1[a];
            randArray1[a] = tempSS;
        }

//For loop that will print the array after it is sorted.
        Console.WriteLine("This is the array after the selection sort algorithm.");
        for (int c = 0; c < randArray1.Length; c++) {
            Console.Write(randArray1[c] + " ");
        }
        Console.WriteLine();

这是非常压倒性的,因为我是新手,我仍在学习这门语言。

有人可以在开始时指导我如何创建 1000 个不同的数组,其中填充了随机数,然后是其余的。我将不胜感激。谢谢你。

标签: c#arraysalgorithmsortingrandom

解决方案


所以你有几个问题让你不知所措。

让我们看看每一个

接受用户输入

Console.WriteLine("Enter a length");
while (!int.TryParse(Console.ReadLine(), out var length))
   Console.WriteLine("omg! you had one job");

使用 out 参数调用方法

从 C# 7.0 开始,您可以在方法调用的参数列表中声明 out 变量,而不是在单独的变量声明中。这会生成更紧凑、更易读的代码,并且还可以防止您在方法调用之前无意中为变量赋值。下面的示例与前面的示例类似,不同之处在于它在对 Int32.TryParse 方法的调用中定义了 number 变量。

填充数组

private static Random _rand = new Random();
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

...

public static string RandomString(int length)
{
   var result = Enumerable.Range(0, length)
                          .Select(s => chars[_rand.Next(length)])
                          .ToArray();
   return new string(result);
}

创建随机字符数组

var arr = Enumerable.Range(0, size)
                    .Select(i => RandomString(length)).ToArray();

如何计时

var sw = Stopwatch.StartNew();

// something to time

var milliseconds = sw.ElapsedMilliseconds

现在将所有内容映射在一起,我将把这些细节留给你


其他资源

Enumerable.Range(Int32, Int32) 方法

生成指定范围内的整数序列。

Enumerable.Select 方法

将序列的每个元素投影到新形式中。

秒表类

提供一组可用于准确测量经过时间的方法和属性。

随机类

表示伪随机数生成器,它是一种生成满足某些随机性统计要求的数字序列的设备。


推荐阅读