Click here to Skip to main content
15,913,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
// 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();
}


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

You can fill in your array with random integers as shown below
C#
Random randomNumber = new Random();
a = new int[100];
for (int row = 0; row < 100; row++)
{
  a[row] = randomNumber.Next(0,99);
}


For execution time you can check this
How To: Measure execution time in C#[^]
 
Share this answer
 
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900