1
       1/  DateTime before = DateTime.Now;
       2/  shellSort(List1);
       3/  DateTime after = DateTime.Now;
       4/  Console.WriteLine(after - before);
       5/
       6/  before = DateTime.Now;
       7/  insertionSort(List2);
       8/  after = DateTime.Now;
       9/  Console.WriteLine(after - before);

I am trying to compare the run time of two different sorting algorithms. List1 here is equal to List2. I was expecting shell sort to be faster than insertion sort but although first WriteLine differs, it usually prints something like this = 00:00:00.0035037. The second one however, either prints 00:00:00 or something smaller than the first print. I thought maybe the insertion sort was better suited for List's current state however even when i swap the line 7 and line 2 i still get the same result. What is causing this? Why is the second executed function runs faster? Or am i using the Dates completely wrong? Edit : I used Stopwatch instead of DateTime Class as advised in another post. The result is pretty much the same. The second one usually runs faster but every now and then it's slower than the first one. I also used a pre-written shellsort code to see if my implementation was bad but that was also a dead end.

As requested, shellsort and insertionsort implementations

static void shellSort(List<int> numbers) // Implementation i found online
        {
            int i, j, increment, temp;
            increment = 3;
            while (increment > 0)
            {
                for (i = 0; i < numbers.Count ; i++)
                {
                    j = i;
                    temp = numbers[i];
                    while ((j >= increment) && (numbers[j - increment] > temp))
                    {
                        numbers[j] = numbers[j - increment];
                        j = j - increment;
                    }
                    numbers[j] = temp;
                }
                if (increment / 2 != 0)
                    increment = increment / 2;
                else if (increment == 1)
                    increment = 0;
                else
                    increment = 1;
            }
        }

public static void insertionSort(List<int> numbers)
        {
            int i = 0;
            while (i != numbers.Count)
            {
                int k = i;
                while (k != 0 && numbers[k] < numbers[k - 1])
                {
                    int temp = numbers[k - 1];
                    numbers[k - 1] = numbers[k];
                    numbers[k] = temp;
                    k--;
                }
                i++;
            }
        }

Also this was my implementation of shellsort

public static void shellSort(List<int> Liste)
        {
            int n = Liste.Count;
            int gap = (Liste.Count - 1) / 2;
            while (gap > 0)
            {
                int i = 0;
                for(int k = gap; k < n; k++) { 

                    int p = i;
                    int m = k;
                    while (p >= 0)
                    {

                        if (Liste[p] > Liste[m])
                        {
                            int temp = Liste[p];
                            Liste[p] = Liste[m];
                            Liste[m] = temp;
                            m = p;
                        }
                        else
                            break;
                        p = p - gap;
                    }
                    i++;
                }
                gap = gap / 2;
            }
        }
Eclipse
  • 23
  • 3
  • [Use `StopWatch` instead of `DateTime` to measure it](http://stackoverflow.com/a/16376269/447156) Also would be better to see how you implement `shellSort` and `insertionSort` methods. – Soner Gönül Dec 05 '15 at 15:01
  • i shared the implementations as requested – Eclipse Dec 05 '15 at 15:11

0 Answers0