堆最小堆无法正常工作

时间:2014-02-06 19:23:24

标签: c# algorithm heap heapsort

我正在尝试使用min-heap实现heapsort。输入是正整数数组,数组的零索引存储大小。谁能发现我的错误?这里使用的语言是C#。该算法有时可以正常工作,但对于较大的数组,根不是数组中的最小值。

    static void CreateMinHeap(int[] input)
    {
        input[0] = input.Length - 1;

        for (int i = (input[0] / 2); i > 0; i--)
        {
            MinHeapify(input, i);
        }
    }

    static void MinHeapify(int[] input, int index)
    {
        var left = 2 * index;
        var right = 2 * index + 1;
        int smallest;

        if (left < input.Length && input[left] < input[index])
            smallest = left;
        else
            smallest = index;

        if (right < input.Length && input[right] < input[smallest])
            smallest = right;

        if (smallest != index)
        {
            Swap(ref input[smallest], ref input[index]);
            MinHeapify(input, smallest);
        }
    }

    static public void HeapSort(int[] input)
    {
        CreateMinHeap(input);

        for (int i = input[0]; i > 0; i--)
        {
            Swap(ref input[1], ref input[i]);
            input[0]--;
            MinHeapify(input, 1);
        }
    }

    static public void Swap(ref int a, ref int b)
    {
        var temp = a;
        a = b;
        b = temp;
    }

1 个答案:

答案 0 :(得分:1)

背景

据我所知,你在两个分区中使用你的阵列。

第一个分区包含堆,第二个分区(开始为空)包含已排序的值。

在HeapSort期间,第一个分区的大小减小,第二个分区的大小增加,直到你有一个排序的数组。

错误

问题在于,当你运行MinHeapify时,你没有告诉它堆的长度已经减少,因此它正在尝试堆积一些已排序的节点。

修复

您正在跟踪条目输入[0]中堆的大小,因此这应该很容易修复。

尝试更改:

    if (left < input.Length && input[left] < input[index])
        smallest = left;
    else
        smallest = index;

    if (right < input.Length && input[right] < input[smallest])
        smallest = right;

    if (left <= input[0] && input[left] < input[index])
        smallest = left;
    else
        smallest = index;

    if (right <= input[0] && input[right] < input[smallest])
        smallest = right;
相关问题