堆排序问题
我现在正在研究堆排序。到目前为止,我的代码输出错误。例如,我输入了 4 3 5 2 1,我输入的第一个数字始终位于最后一个索引上。输出将为 1 2 3 5 4。任何想法我的代码有什么问题。
int[] nums = new int[100];
int SizeNum;
int x;
int currentPass;
int nPass = 1;
private void ExeButton_Click(object sender, EventArgs e)
{
nPass = 1;
string[] numsInString = EntNum.Text.Split(' '); //split values in textbox
for (int j = 0; j < numsInString.Length; j++)
{
nums[j] = int.Parse(numsInString[j]);
}
if (SizeNum == numsInString.Length)
{
SortArray(currentPass);
ResultText.AppendText("\n\n");
}
}
}
public void SortArray(int currentPass)
{
int i;
int temp;
for (i = (SizeNum / 2) - 1; i >= SizeNum; i--)
{
siftDown(i, x, currentPass + 1);
}
for (i = SizeNum - 1; i >= 1; i--)
{
temp = nums[0];
nums[0] = nums[i];
nums[i] = temp;
siftDown(0, i - 1, currentPass + 1);
Display(currentPass);
}
Display(currentPass);
}
public void siftDown(int root, int bottom, int currentPass)
{
bool done = false;
int maxChild;
int temp;
while ((root * 2 <= bottom) && (!done))
{
if (root * 2 == bottom)
maxChild = root * 2;
else if (nums[root * 2] > nums[root * 2 + 1])
maxChild = root * 2;
else
maxChild = root * 2 + 1;
Display(currentPass);
if (nums[root] < nums[maxChild])
{
temp = nums[root];
nums[root] = nums[maxChild];
nums[maxChild] = temp;
root = maxChild;
}
else
{
done = true;
}
}
Display(currentPass);
}
public void Display(int currentPass)
{
int i;
String numbers = "";
ResultText.AppendText("Pass " + nPass + ": ");
for (i = 0; i < SizeNum; i++)
numbers += nums[i].ToString() + " , ";
ResultText.AppendText(numbers + "\n");
nPass++;
}
I'm working now on heap sort. The codes I have so far has a wrong output. For example I entered 4 3 5 2 1, the first number I entered always position on the last index. The output will be 1 2 3 5 4. Any ideas what is the problem with my codes.
int[] nums = new int[100];
int SizeNum;
int x;
int currentPass;
int nPass = 1;
private void ExeButton_Click(object sender, EventArgs e)
{
nPass = 1;
string[] numsInString = EntNum.Text.Split(' '); //split values in textbox
for (int j = 0; j < numsInString.Length; j++)
{
nums[j] = int.Parse(numsInString[j]);
}
if (SizeNum == numsInString.Length)
{
SortArray(currentPass);
ResultText.AppendText("\n\n");
}
}
}
public void SortArray(int currentPass)
{
int i;
int temp;
for (i = (SizeNum / 2) - 1; i >= SizeNum; i--)
{
siftDown(i, x, currentPass + 1);
}
for (i = SizeNum - 1; i >= 1; i--)
{
temp = nums[0];
nums[0] = nums[i];
nums[i] = temp;
siftDown(0, i - 1, currentPass + 1);
Display(currentPass);
}
Display(currentPass);
}
public void siftDown(int root, int bottom, int currentPass)
{
bool done = false;
int maxChild;
int temp;
while ((root * 2 <= bottom) && (!done))
{
if (root * 2 == bottom)
maxChild = root * 2;
else if (nums[root * 2] > nums[root * 2 + 1])
maxChild = root * 2;
else
maxChild = root * 2 + 1;
Display(currentPass);
if (nums[root] < nums[maxChild])
{
temp = nums[root];
nums[root] = nums[maxChild];
nums[maxChild] = temp;
root = maxChild;
}
else
{
done = true;
}
}
Display(currentPass);
}
public void Display(int currentPass)
{
int i;
String numbers = "";
ResultText.AppendText("Pass " + nPass + ": ");
for (i = 0; i < SizeNum; i++)
numbers += nums[i].ToString() + " , ";
ResultText.AppendText(numbers + "\n");
nPass++;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题出在这一行:
由于
SizeNum
字段未初始化,其值为默认值,即 0。因此,当您插入
"5 4 3 2 1"
时,numsInString.Length
将等于5
,然后该if 中的代码未达到
。事实上,如果您设置
SizeNum = numsInString.Length
您的代码似乎可以工作。无论如何,正如其他用户所指出的,如果您使用的是 Visual Studio 或 Sharp-Develop 这样的 IDE,您应该使用调试器,它对于查找代码问题确实非常有帮助。
以下是 Visual Studio 的操作方法: http://msdn.microsoft.com/ en-us/library/sc65sadd.aspx
One problem is at this line:
As
SizeNum
field is not initialized, its value is the default one, i.e. 0.Therefore when you insert
"5 4 3 2 1"
,numsInString.Length
become equal to5
and then the code in thatif
is not reached.In fact if you set
SizeNum = numsInString.Length
your code seems to work.Anyway, as pointed out by other users, if you're using an IDE like visual studio or sharp-develop, you should use the debugger that is really really helpful to find code problems.
Here's an how-to for Visual Studio: http://msdn.microsoft.com/en-us/library/sc65sadd.aspx
SortArray 的第一个 for 循环应该是
This isbuilding you heap。您将从子级的第二层到最后一层开始,一直移动到树的顶部节点。
The first for loop of SortArray should be
This is building you heap. You are starting from the second to last layer of children and moving all the way to the top node of the tree.