static int statements = 0;
static void Main(string[] args)
{
int[] TestNumber = new int[1000]; //// create a new random array
Random number = new Random(); //// to test statistics
for (int i = 0; i < TestNumber.Length; i++)
{
TestNumber[i] = number.Next(100, 999);
Console.WriteLine("\n To test sorting algorithms with 1,000, 3 digit numbers, ten times, press any key...");
Console.ReadLine();
for (int j = 0; j < 10; j++)
{
ShellSort(TestNumber);
}
int average1 = 0;
average1 = statements / 10;
Console.WriteLine("The average number of statements in Shellsort is {0}", average1);
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();
}
}
}
Here is with the for each: The method is changed to public static int [] ShellSort (int [] values), with a return values, then I tried a for each, I get an error because "i" has been declared in parent scope:
for (int i = 0; i < TestNumber.Length; i++)
{
for (int j = 0; j < 10; j++)
{
TestNumber[i] = number.Next(100, 999);
Console.WriteLine("\n To test sorting algorithms with 1,000, 3 digit numbers, ten times, press any key...");
Console.ReadLine();
foreach (int i in ShellSort(TestNumber))
{
Console.Write("" + i.ToString());
}
int average1 = 0;
average1 = statements / 10;
Console.WriteLine("The average number of statements in Shellsort is {0}", average1);
Console.ReadLine();
I was wondering if doing it with loops would work best, or do I need to make another array for this?; Stuck and frustrated, there's more to this and I been working on it for three weeks now... Still feel lost. Thanks for your help.

New Topic/Question
Reply




MultiQuote




|