如何创建一个循环,要求用户输入两个数字,一个是数组的长度,另一个是循环迭代的次数
How to create a loop that ask the user for two numbers, one will be for the length of the array, and the other for how many times the loop iterates
public static void sortInterations()
{
Random r = new Random ();
Console.WriteLine ("Please enter the number of items in the array that will be sorted:");
int numitems = Convert.ToInt32(Console.ReadLine());
int[] arrayOfInts = new int[numitems];
Console.WriteLine ("Please enter a number for iterations:");
int iterations = Convert.ToInt32 (Console.ReadLine ());
TimeSpan runningTime;
for (int index = 0; index < iterations; index++)
{
for (int count = 0; count < arrayOfInts.Length; count++)
{
arrayOfInts [count] = r.Next (numitems);
}
}
DateTime startTime = DateTime.Now;
selectionSort (arrayOfInts);
runningTime = DateTime.Now.Subtract(startTime);
Console.WriteLine ("Time for ___ sort to complete: " + runningTime.ToString(@"mm\:ss\.ffffff"));
sortInterations ();
}
这就是我目前所拥有的!基本上我想用随机数填充数组,然后使用冒泡排序对它进行排序,用户输入变量迭代的次数,然后打印出花费的时间。
希望这对您有所帮助。您可能希望在创建新的随机整数数组的每次迭代中获得一个新种子,以避免获得相同或接近相同的数字。
public static void sortInterations()
{
Random r = new Random();
Console.WriteLine("Please enter the number of items in the array that will be sorted:");
// no checking for invalid input i.e. "aA" will crash here when you try to convert
int numitems = Convert.ToInt32(Console.ReadLine());
int[] arrayOfInts = new int[numitems];
Console.WriteLine("Please enter a number for iterations:");
// no checking for invalid input i.e. "aA" will crash here when you try to convert
int iterations = Convert.ToInt32(Console.ReadLine());
// loop for each random array
for (int index = 0; index < iterations; index++)
{
// loop to generate an array of random numbers
for (int count = 0; count < arrayOfInts.Length; count++)
{
arrayOfInts[count] = r.Next(numitems);
}
// a random array has been created start the clock and sort it
Stopwatch elpased = new Stopwatch();
elpased.Start();
selectionSort(arrayOfInts);
elpased.Stop();
Console.WriteLine("Sort array Number: " + index + " Time for sort to complete in milliseconds: " + elpased.ElapsedMilliseconds.ToString());
}
Console.ReadLine();
}
public static void sortInterations()
{
Random r = new Random ();
Console.WriteLine ("Please enter the number of items in the array that will be sorted:");
int numitems = Convert.ToInt32(Console.ReadLine());
int[] arrayOfInts = new int[numitems];
Console.WriteLine ("Please enter a number for iterations:");
int iterations = Convert.ToInt32 (Console.ReadLine ());
TimeSpan runningTime;
for (int index = 0; index < iterations; index++)
{
for (int count = 0; count < arrayOfInts.Length; count++)
{
arrayOfInts [count] = r.Next (numitems);
}
}
DateTime startTime = DateTime.Now;
selectionSort (arrayOfInts);
runningTime = DateTime.Now.Subtract(startTime);
Console.WriteLine ("Time for ___ sort to complete: " + runningTime.ToString(@"mm\:ss\.ffffff"));
sortInterations ();
}
这就是我目前所拥有的!基本上我想用随机数填充数组,然后使用冒泡排序对它进行排序,用户输入变量迭代的次数,然后打印出花费的时间。
希望这对您有所帮助。您可能希望在创建新的随机整数数组的每次迭代中获得一个新种子,以避免获得相同或接近相同的数字。
public static void sortInterations()
{
Random r = new Random();
Console.WriteLine("Please enter the number of items in the array that will be sorted:");
// no checking for invalid input i.e. "aA" will crash here when you try to convert
int numitems = Convert.ToInt32(Console.ReadLine());
int[] arrayOfInts = new int[numitems];
Console.WriteLine("Please enter a number for iterations:");
// no checking for invalid input i.e. "aA" will crash here when you try to convert
int iterations = Convert.ToInt32(Console.ReadLine());
// loop for each random array
for (int index = 0; index < iterations; index++)
{
// loop to generate an array of random numbers
for (int count = 0; count < arrayOfInts.Length; count++)
{
arrayOfInts[count] = r.Next(numitems);
}
// a random array has been created start the clock and sort it
Stopwatch elpased = new Stopwatch();
elpased.Start();
selectionSort(arrayOfInts);
elpased.Stop();
Console.WriteLine("Sort array Number: " + index + " Time for sort to complete in milliseconds: " + elpased.ElapsedMilliseconds.ToString());
}
Console.ReadLine();
}