title: C# heapSort ,System.Timers; to check algorithm time
body: <p>I have to check HeapSort algorithm time in C# , my problem is that I Know I must use System.Timers , because I don't know how to measures the algorithm time.
  I have to check the algorithm time for table contains 1000 ,10 000 , 100 000 and 1000 000 integers.</p>
  
  <p>Help me good people please.</p>
  
  <p>This is the code:
  <pre><code>
      using System;</p>
  
  <pre><code>namespace Sort
  {
      class Program
      {
          public static void Adjust(int[] list, int i, int m)
          {
              int Temp = list[i];
              int j = i * 2 + 1;
  
              while (j &lt;= m)
              {
                  if (j &lt; m)
                      if (list[j] &lt; list[j + 1])
                          j = j + 1;
                  if (Temp &lt; list[j])
                  {
                      list[i] = list[j];
                      i = j;
                      j = 2 * i + 1;
                  }
                  else
                  {
                      j = m + 1;
                  }
              }
  
              list[i] = Temp;
          }
  
          public static void HeapSort(int[] list)
          {
              int i;
              //Boulding a heap
              for (i = (list.Length - 1) / 2; i &gt;= 0; i--)
                  Adjust(list, i, list.Length - 1);
  
              for (i = list.Length - 1; i &gt;= 1; i--)
              {
                  int Temp = list[0];
                  list[0] = list[i];
                  list[i] = Temp;
                  Adjust(list, 0, i - 1);
              }
          }
  
          static void Main(string[] args)
          {
              Console.Title = "HeapSort";
              int i;
              int[] a = { 12, 3, -12, 27, 34, 23, 1, 81, 45,
                      17, 9, 23, 11, 4, 121 };
              Console.WriteLine("Data before sort ");
              for (i = 0; i &lt; a.Length; i++)
                  Console.Write(" {0} ", a[i]);
              Console.WriteLine();
              HeapSort(a);
              Console.WriteLine("Data after sort");
              for (i = 0; i &lt; a.Length; i++)
                  Console.Write(" {0} ", a[i]);
              Console.ReadLine();
          }
      }
  }
  </code></pre>
  
  <p></pre></code></p>
  
  <p>I've write this with You help , does is good ?
  <pre><code></p>
  
  <p>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Diagnostics;</p>
  
  <p>namespace Sort
  {
      class Program
      {</p>
  
  <pre><code>    public static void Adjust(int[] list, int i, int m)
      {
          int Temp = list[i];
          int j = i * 2 + 1;
  
          while (j &lt;= m)
          {
  
              if (j &lt; m)
                  if (list[j] &lt; list[j + 1])
                      j = j + 1;
  
  
              if (Temp &lt; list[j])
              {
                  list[i] = list[j];
                  i = j;
                  j = 2 * i + 1;
              }
              else
              {
                  j = m + 1;
              }
          }
  
          list[i] = Temp;
      }
  
  
  
  
  
      public static void HeapSort (int[] list)
  </code></pre>
  
  <p>{
              int i;
      //Boulding a heap
      for (i = (list.Length - 1) / 2;i >=0;i--)
          Adjust (list, i, list.Length - 1);</p>
  
  <pre><code>for ( i = list.Length - 1; i &gt;= 1; i--)
  {
      int Temp = list [0];
      list [0] = list [i];
      list [i] = Temp;
      Adjust (list, 0, i - 1);
  }
  </code></pre>
  
  <p>}</p>
  
  <pre><code>    static void Main(string[] args)
      {
          Console.Title = "HeapSort";
          int i;
          Random myRandom = new Random();//Creating instance of class Random
          Stopwatch myTime = new Stopwatch(); //variable for time measurement
  
  
  
  
          int[] a = new int[1000]; //table contents 1000 variables
  
  
          for (i = 0; i &lt; a.Length; i++)
              a[i] = myRandom.Next(100);
  
          Console.WriteLine("Data before sort ");
          for (i = 0; i &lt; a.Length; i++)
              Console.Write(" {0} ", a[i]);
          Console.WriteLine();
          myTime.Start();
          HeapSort(a);
          myTime.Stop();
  
          string TimeEl = myTime.Elapsed.ToString();
  
          Console.WriteLine("Data after sort");
          for (i = 0; i &lt; a.Length; i++)
              Console.Write(" {0} ", a[i]);
          Console.WriteLine();
          Console.WriteLine();
          Console.WriteLine("time elapsed: {0} ", TimeEl);
          Console.ReadLine();
  
  
  
  
      }
  
  
      }
  }
  </code></pre>
  
  <p></pre></code></p>