c# - 堆排序问题

标签 c# algorithm sorting heap

我现在正在研究堆排序。到目前为止,我的代码输出错误。例如我输入 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++;
    }

最佳答案

这一行有一个问题:

if (SizeNum == numsInString.Length)

由于SizeNum字段未初始化,其值为默认值,即0。
因此,当您插入 "5 4 3 2 1" 时,numsInString.Length 变得等于 5 然后 中的代码如果 未达到。
事实上,如果您设置 SizeNum = numsInString.Length,您的代码似乎可以正常工作。

无论如何,正如其他用户所指出的,如果您使用的是像 visual studio 或 sharp-develop 这样的 IDE,则应该使用真正有助于查找代码问题的调试器。

这是 Visual Studio 的操作方法:http://msdn.microsoft.com/en-us/library/sc65sadd.aspx

关于c# - 堆排序问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7300201/

相关文章:

ruby - 为 Ruby 中的抄袭检测引擎设计噪声过滤器

algorithm - 二叉堆和优先队列

algorithm - 获取总和最大的子矩阵?

python - python 的 sorted() 使用什么算法?

ios - 如何根据键 "id"升序对 NSDictionary 进行排序?

c# - WebDAV 获取空闲空间信息

c# - 如何将 DataGridView 定位到特定行(以便所选行位于顶部)

c# - LINQ/lambda : How can I query a DB table based on information from another table?(多对多关系)

c# - 如何处理具有未实现 IDisposable 属性的类?

python - 按字母顺序对 Tkinter 选项菜单进行排序?