stellaartois
asked on
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
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();
}
}
}
ASKER
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
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
ASKER
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
I must be missing something obvious surely.
Luke
ASKER
Attached
error.bmp
error.bmp
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
All I can suggest is you try a reboot of visual studio (or the PC ) and see if that cures it.
0003.jpg
Aha - are you stepping past the line then checking or BEFORE the division has taken place?
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
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
Thanks for your time
Luke
ASKER
Thank you for teaching me a lesson guys
If numberCount is zero then you get the result you are getting, what value is in numberCount before you divide by 2 ?