随机数组和system.tick(排序算法) [英] random array and system.tick (sorting algorithms)

查看:89
本文介绍了随机数组和system.tick(排序算法)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

// array of integers to hold values
private int[] a = new int[100];

// number of elements in array
private int x;

// Selection Sort Algorithm
public void sortArray()
{
  int i, j;
  int min, temp;

  for (i = 0; i < x - 1; i++)
  {
    min = i;

    for (j = i + 1; j < x; j++)
    {
      if (a[j] < a[min])
      {
        min = j;
      }
    }

    temp = a[i];
    a[i] = a[min];
    a[min] = temp;
  }
}

public static void Main()
{
  // Instantiate an instance of the class
  selectionSort mySort = new selectionSort();

  // Get the number of elements to store in the array
  Console.Write("Number of elements in the array (less than 100) : ");
  string s = Console.ReadLine();
  mySort.x = Int32.Parse(s);

  // Array header
  Console.WriteLine("");
  Console.WriteLine("-----------------------");
  Console.WriteLine(" Enter array elements  ");
  Console.WriteLine("-----------------------");

  // Get array elements
  for (int i = 0; i < mySort.x; i++)
  {
    Console.Write("<{0}> ", i + 1);
    string s1 = Console.ReadLine();
    mySort.a[i] = Int32.Parse(s1);
  }

  // Sort the array
  mySort.sortArray();

  // Output sorted array
  Console.WriteLine("");
  Console.WriteLine("-----------------------");
  Console.WriteLine(" Sorted array elements ");
  Console.WriteLine("-----------------------");

  for (int j = 0; j < mySort.x; j++)
  {
    Console.WriteLine(mySort.a[j]);
  }

  // Here to stop app from closing
  Console.WriteLine("\n\nPress Return to exit.");
  Console.Read();
}



如何使它成为整数数组并使用
初始化数组 从0到99的随机值.
如何在排序数组之前和排序之后立即获取当前的系统刻度计数(System.DateTime.Now.Ticks).记录这些值之间的差异,以确定排序所花的时间(一个刻度是100纳秒).



how can make it array of integers and initialize the array with
random values from 0 to 99.
how to get the current System tick count (System.DateTime.Now.Ticks) immediately before sorting the array and immediately after sorting. Record the difference in these values to determine how long the sort took (One tick is 100 nanoseconds).

推荐答案

您可以使用随机整数填充数组,如下所示
You can fill in your array with random integers as shown below
Random randomNumber = new Random();
a = new int[100];
for (int row = 0; row < 100; row++)
{
  a[row] = randomNumber.Next(0,99);
}



对于执行时间,您可以检查此
如何:在C#中测量执行时间 [ ^ ]



For execution time you can check this
How To: Measure execution time in C#[^]


对于随机值,请使用类System.Random;您需要使用其方法Next(int maxValue);请参阅 http://msdn.microsoft.com/en-us/library/system.random.aspx [^ ] .

(我不想注意到这一点,但是异常多的初学者会犯完全白痴的错误,您不应重复:他们周期性地创建类System.Random的实例,这是一个大问题的根源.此类的实例应只能创建一次.)

我想您需要测量某些计算的时间.为此,除了类System.Diagnostics.Stopwatch之外,不要使用其他任何内容,请参阅 http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx [http://msdn.microsoft.com/en-us/library/system.timespan.aspx [
For random values, use the class System.Random; you need to use its method Next(int maxValue); see http://msdn.microsoft.com/en-us/library/system.random.aspx[^].

(I hate to note this, but abnormally high number of beginners make totally idiotic mistake which you should not repeat: they create an instance of the class System.Random in cycle, which is a source of big problems. The instance of this class should be created only once.)

I guess you need to measure time of certain calculations. For this purpose, do not use anything but the class System.Diagnostics.Stopwatch, see http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx[^].

This is the most accurate tool available. You can learn what accuracy you can have using the static properties System.Diagnostics.Stopwatch.Frequency and System.Diagnostics.Stopwatch.IsHighResolution.

The property Elapsed returns the value of the type System.TimeSpan. You should better use double properties of this type to obtain the real-time values in fractional seconds, milliseconds, etc., using its properties System.TimeSpan.TotalSeconds, System.TimeSpan.TotalMilliseconds, etc. See http://msdn.microsoft.com/en-us/library/system.timespan.aspx[^].

—SA


这篇关于随机数组和system.tick(排序算法)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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