堆排序 C# 中的大型数组排序

本文关键字:大型 数组排序 堆排序 | 更新日期: 2023-09-27 18:31:55

我的堆排序实现有一个小问题。基本上,我实现了它,它基本上适用于具有 6 个或更少元素的数组。但是由于某种原因,任何大于 6 个元素的东西,排序都是错误的。

例如:

排序 {10,64,7,99,32,18

} 得到这个:7,10,18,32,64,99

排序 {10,64,7,99,32,18,2,48

} 得到:2,7,10,32,18,48,64,99

我的实现如下。随着数组的大小变大,排序在某种意义上变得更加混乱,并给出不正确的输出。我该如何解决这个问题?

class Program
{
    static void Main(string[] args)
    {
        int[] arr = { 10, 64, 7, 99, 32, 18};
        HeapSort hs = new HeapSort();
        hs.PerformHeapSort(arr);
        Console.ReadLine();
    }
}
class HeapSort
{
    private int heapSize;
    private void BuildHeap(int[] arr)
    {
        heapSize = arr.Length-1;
        for (int i = heapSize/2; i >= 0; i--)
        {
            Heapify(arr, i);
        }
    }
    private void Swap(int[] arr, int x, int y)//function to swap elements
    {
        int temp = arr[x];
        arr[x] = arr[y];
        arr[y] = temp;
    }
    private void Heapify(int[] arr, int index)
    {
        int left = 2 * index;
        int right = 2 * index + 1;
        int largest;
        if (left <= heapSize && arr[left] > arr[index])
        {
            largest = left;
        }
        else
        {
            largest = index;
        }
        if (right <= heapSize && arr[right] > arr[largest])
        {
            largest = right;
        }
        else
        {
            largest = index;
        }
        if (largest != index)
        {
            Swap(arr, index, largest);
            Heapify(arr, largest);
        }
    }
    public void PerformHeapSort(int[] arr)
    {
        BuildHeap(arr);
        for (int i = arr.Length-1; i >= 0; i--)
        {
            Swap(arr, 0, i);
            heapSize--;
            Heapify(arr, 0);
        }
        DisplayArray(arr);
    }
    private void DisplayArray(int[] arr)
    {
        for (int i = 0; i < arr.Length; i++)
        { Console.Write("[{0}]", arr[i]); }
    }
}

堆排序 C# 中的大型数组排序

错误包含在函数Heapify 中。函数的更正版本:

        private void Heapify(int[] arr, int index)
        {
            int left = 2 * index + 1;
            int right = 2 * index + 2;
            int largest = index;
            if (left <= heapSize && arr[left] > arr[index])
            {
                largest = left;
            }
            if (right <= heapSize && arr[right] > arr[largest])
            {
                largest = right;
            }
            if (largest != index)
            {
                Swap(arr, index, largest);
                Heapify(arr, largest);
            }
        }

方法中的错误实现 - Heapify(int[] arr, int index)

您可以像下面这样操作:

    public static void Heapify(int[] arr, int index)
    {
        int left = (index + 1) * 2 - 1;
        int right = (index + 1) * 2;
        int largest = 0;
        if (left < heapSize && arr[left] > arr[index])
        {
            largest = left;
        }
        else {
            largest = index;
        }
        if (right < heapSize && arr[right] > arr[largest])
        {
             largest = right;
        }
        if (largest != index)
        {
            Swap(arr, index, largest);
            Heapify(arr, largest);
        }
    }