0

I created a program to perform bubble sort on random numbers and I am trying to include the time it takes to go through each sorting operations in milliseconds. I tried a few ways but my program does not even acknowledge the code for it. Does anybody see what I am doing wrong?

    class BubbleSort
    {
    static void Main(string[] args)
    {
        int[] a = { 50000, 250000, 25000, 750000, 500000, 100, 100000, 
    1000000, 1000 };
        int t;


        Console.WriteLine("Array is: ");
        for (int i = 0; i < a.Length; i++)
        {
            Console.WriteLine(a[i]);
        }
        DateTime start = DateTime.Now;
        DateTime end;
        for (int j = 0; j <= a.Length - 2; j++)
        {
            for (int i = 0; i <= a.Length - 2; i++)
            {
                if (a[i] > a[i + 1])
                {
                    t = a[i + 1];
                    a[i + 1] = a[i];
                    a[i]=t;

                }
            }

        }

        end = DateTime.Now;
        TimeSpan ts = end.Subtract(start);
        Console.WriteLine("The Sorted Array: ");
        foreach (int Array in a)
            Console.Write(Array + " ");
        Console.Read();
        Console.WriteLine("Duration = {0} ms", ts.TotalMilliseconds);
    }
Maya Wazni
  • 23
  • 3
  • 2
    Use a `StopWatch` to time code. `DateTime` isnt accurate enough – Ňɏssa Pøngjǣrdenlarp May 25 '19 at 20:43
  • Possible duplicate of [How do you run and compare two sorting algorithms and see which one finishes first?](https://stackoverflow.com/questions/41786137/how-do-you-run-and-compare-two-sorting-algorithms-and-see-which-one-finishes-fir) – Simply Ged May 25 '19 at 22:28
  • 1
    That is such a tiny set to sort...it's barely going to register! – Idle_Mind May 25 '19 at 23:16
  • to benchmark performance you may need to run thousands or millions of times. A bunch of those tiny arrays can be sorted in a blink. And you also need to warm up the JITter. After just one iteration the JITter won't even bother optimize your code. See https://stackoverflow.com/a/15366908/995714 – phuclv May 26 '19 at 00:39
  • What does "my program does not even acknowledge the code for it" mean? – Enigmativity May 26 '19 at 00:45

0 Answers0