Shell Sort in C#. Variable will not set as divided value.

Hello,

I am programming a ShellSort in C#.  I am having trouble with assigning a variable called 'gap' and I can't for the life of me understand why it will not come out with the correct value.

If I hover over the piece of problem code, it shows that the outcome of the calculation is '5', but then the variable always seems to have the value 0.

Thanks for any assistance, I've included my code in the code section and I've pointed out the problem code.

Thanks for any help,

Luke
// Code by Luke Coulton 29.03.2011
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ShellSortConsole
{
    class Program
    {
        // Method for the  Shell Sort algorithm
        static List<int> ShellSort(List<int> Data)
        {
            int numberCount = Convert.ToInt32(Data.Count);
            

            //
            // This won't divide properly!!  Always giving value '0'
            // \/ \/ \/ \/ \/ \/ \/
            //
            int gap = numberCount / 2;

            // Do-while loop for whilst gap is more than zero
            do
            {
                for (int i = (gap + 1); i <= Data.Count; i++)
                {
                    // Create the new pointer value
                    int pointer = i - gap;

                    do
                    {

                        if (Data[pointer] > Data[pointer + gap])
                        {
                            int temp = Data[pointer];
                            Data[pointer] = Data[pointer + gap];
                            Data[pointer + gap] = temp;

                            // Set the new pointer value
                            pointer = pointer - gap;
                        }
                        else
                        {
                            pointer = 0;
                        }

                    } while (pointer > 0);

                    gap = gap / 2;
                }

            } while (gap > 0);

            return Data;
        }

        static void Main(string[] args)
        {

            // List of values to sort
            List<int> sortingValues = new List<int> { 500, 300, 200, 400, 100, 500,300,200,400,100 };

            // Invoke the method
            List<int> numberz = ShellSort(sortingValues);

            foreach (int sortedValue in numberz)
	        {
                Console.Write(sortedValue + " ");
            }

            // ReadLine to prevent closure of console
            Console.ReadLine();

        }
    }
}

Open in new window

LVL 2
stellaartoisAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
int numberCount = Convert.ToInt32(Data.Count);

If numberCount is zero then you get the result you are getting, what value is in numberCount before you divide by 2 ?
stellaartoisAuthor Commented:
Hy AndyAinscow,

Thanks for the response.

The numberCount variable shows as 10 when I hover over it.  This is the count of the number of values in List<int> sortingValues as it is passed to the ShellSort() method.

Cheers for further assistance,

Luke
stellaartoisAuthor Commented:
I have attached this image showing that the values are set, but then when I hover over the 'gap' variable it is set to '0'.

I must be missing something obvious surely.

Luke
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

stellaartoisAuthor Commented:
Attached
error.bmp
AndyAinscowFreelance programmer / ConsultantCommented:
Wierd - it works here without problems.  I've just copied/pasted into a new console app.

All I can suggest is you try a reboot of visual studio (or the  PC ) and see if that cures it.
0003.jpg
AndyAinscowFreelance programmer / ConsultantCommented:
Aha - are you stepping past the line then checking or BEFORE the division has taken place?
AndyAinscowFreelance programmer / ConsultantCommented:
Don't forget - a breakpoint stops when the line of code is reached, not after that line of code is executed.  Your pictures look like you are on the line of code when you check the value.
khan_webguruCommented:
Hello Bro!

I see your attached image in which you are displaying the values. Please one thing keep in your mind when you will be on line this

int gap = numberCount / 2;

then it will not show gap new value just press F10 and go to the next line then after execution of the line gap value will be change then check it.  

make sure gap variable is not using some where else in parallel process just change the varible name and try again.

Regards,

Asif Ahmed KHan

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
stellaartoisAuthor Commented:
Oh damn!!  I feel stupid now.  It's becuase I've done a break point and it hasn't executed the code yet.  This is one lesson I won't forget, if you break point it doesn't execute that line of code!

Thanks for your time

Luke
stellaartoisAuthor Commented:
Thank you for teaching me a lesson guys
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.