Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am writing code for shell sort that counts the number of statements executed. I have written one for a gnome sort that functions fine, I just can't figure out why the shell sort comes up 0 everytime, any help would be appreciated, here's the code:
C#
static void Main(string[] args)
       {
           int[] Number = new int[1000];  //// create a and fill a random array
           Random numb = new Random();

           for (int i = 0; i < Number.Length; i++)
           {
               Number[i] = numb.Next(100, 999);
               Console.WriteLine(Number[i]);  //// display the instances
           }


           Console.WriteLine("Press any key to display the first twenty numbers");
           Console.ReadLine();
           for (int j = 0; j < 20; j++)
           {
               Console.WriteLine(Number[j]);
           }


           Console.WriteLine("Press any key to display the last twenty numbers sorted");
           Console.ReadLine();
           for (int k = 980; k < 1000; k++)
           {
               Console.WriteLine(Number[k]);
           }
           ShellSort(Number);
           Console.ReadLine();

       }
            public static void ShellSort(int[] values)
       {
           int i;
           int j;
           int count = 0;
           int temp;
           int statements = 0;


           while (count > 0)       //// ShellSort algorithm
           {

               for (i = 0; i < values.Length; i++)  //// loop through arrays
               {

                   j = i;
                   temp = values[i];
                   statements++;

                   while ((j >= count) && (values[j - count] > temp))
                   {
                       values[j] = values[j] - count;
                       j = j - count;
                       statements++;
                   }
                       values[j] = temp;
                       statements++;
                   }

                   if (count / 2 != 0)
                   {
                       count = count / 2;
                       statements++;
                   }

               }


           Console.WriteLine(" There are a total of {0} statements in Shell Sort.", statements);
               Console.ReadLine();

            }
       }
   }

Here is the gnomesort code that works, just to reference, this code works fine.
C#
public static void GnomeSort(int[] values)
    {
        int statement = 0;
        int i = 1;
        int j = 2;



        while (i < values.Length)             //// GnomeSort algorithm
        {

            if (values[i - 1] > values[i])
            {
                int swapValue = values[i - 1];
                values[i - 1] = values[i];
                values[i] = swapValue;
                i--;
                statement++;

                if (i == 0)
                    i = j++;
                statement++;
            }
            else
                i = j++;
            statement++;


        }
        {
            Console.WriteLine(" There are a total of {0} statements in Gnome Sort.", statement);
            Console.ReadLine();


        }
    }

Thank You
Posted

Hello,

It may be because this is wrong:

C#
public static void ShellSort(int[] values)
{
    int i;
    int j;
    int count = 0;
    int temp;
    int statements = 0;


    while (count > 0)       //// ShellSort algorithm



You initialised the value of count with 0, then test if it is more than 0 in the while. As it is 0 it will not execute.


Valery.
 
Share this answer
 
v2
Comments
LanFanNinja 25-Feb-12 19:10pm    
+5
ericsommerfelt 26-Feb-12 0:15am    
You're right the formula uses increment/count = 3; I still think there is still something wrong though, because I get 2000 statements all the time, although thank you for your help
Thanks again for the help, I did have other issues with the code, your guidence really helped though. For awhile I had a infinite loop, then I had a ; after a while, which threw exceptions, but eventually I got it, Again, thanks for the response. Here is my finished code:
C#
static long statements = 0;
        static void Main(string[] args)
        {
            int[] Number = new int[1000];  //// create a and fill a random array
            Random numb = new Random();


            for (int i = 0; i < Number.Length; i++)
            {
                Number[i] = numb.Next(100, 999);
                Console.WriteLine(Number[i]);  //// display the instances
            }
            ShellSort(Number);

            Console.WriteLine("Press any key to display the first twenty numbers");
            Console.ReadLine();
            for (int j = 0; j < 20; j++)
            {
                Console.WriteLine(Number[j]);
            }
            ShellSort(Number);
            statements = 0;

            Console.WriteLine("Press any key to display the last twenty numbers sorted");
            Console.ReadLine();
            for (int k = 980; k < 1000; k++)
            {
                Console.WriteLine(Number[k]);
            }
            ShellSort(Number);


            Console.ReadLine();


        }
             public static void ShellSort(int [] values)


{
    int j;
    int temp;

    int increment = 3;
    while (increment > 0)
    {
        for (int index = 0; index < values.Length; index++)
        {
            j = index;
            temp = values[index];
            while ((j >= increment) && values[j - increment] > temp)

            {
                values[j] = values[j] - increment;
                j = j - increment;
                statements++;
            }
            values[j] = temp;
            statements++;
        }
        if (increment / 2 != 0)
            increment = increment / 2;
        else if (increment == 1)
            increment = 0;
        else
            increment = 1;
        statements++;
    }

                Console.WriteLine(" There are a total of {0} statements in Shell Sort.", statements);

                Console.ReadLine();
            }
             }
        }
 
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