本文介绍了在shell排序中检查语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在为shell排序编写代码,该代码对执行的语句数进行计数.我已经写了一个能正常工作的gnome排序程序,我只是想不出为什么shell排序程序每次都会出现0,任何帮助将不胜感激,这是代码:
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:
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();
}
}
}
这是有效的gnomesort代码,仅供参考,此代码可以正常工作.
Here is the gnomesort code that works, just to reference, this code works fine.
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();
}
}
谢谢你
推荐答案
public static void ShellSort(int[] values)
{
int i;
int j;
int count = 0;
int temp;
int statements = 0;
while (count > 0) //// ShellSort algorithm
您将count的值初始化为0,然后测试一会儿它是否大于0.由于它为0,因此不会执行.
Valery.
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.
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();
}
}
}
这篇关于在shell排序中检查语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!