Jonathan Crispe

Jonathan Crispe

  • NA
  • 46
  • 48.6k

random array and tick frequency

Nov 10 2011 1:05 PM
    // 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;
          }
        }
        Random randomNumber = new Random();
        a = new int[100];
        for (int row = 0; row < 100; row++)
        {
            a[row] = randomNumber.Next(0, 99);
        }
        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();
    }
  }

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).

Answers (2)