zoukankan      html  css  js  c++  java
  • Algorithms:HeapSort

    HeapSort is an sort algorithms, using heap tree(Full Binary Tree) for help.

    Implementing this sort should execute three steps

    1. Adjust an entire array to a heap tree(big heap tree for ascending, small heap tree for descending sort)

    2. Get the root of the heap tree arr[0] and swap it with last one of the no-sort array, that will consider as the item of the sort array.

    3. Re-adjust the new no-sort array, till the no-sort array has only one item.

    Code is below:

            /// <summary>
            
    /// Heap Sort
            
    /// </summary>
            
    /// <param name="arr"></param>
            public static void HeapSort(int[] arr)
            {
                if (arr == null)
                {
                    throw new ArgumentNullException();
                }

                // Adjust array, make it as big heap
                for (int i = arr.Length / 2 - 1; i >= 0; i--)
                {
                    HeapAdjust(arr, i, arr.Length);
                }

                for (int i = arr.Length - 1; i > 0; i--)
                {
                    // Swap the first one with last one, both of which in the left no-sort sub array
                    
    // Generated no-sort subarray (a[0]-a[i-1]) and sort subarray(a[i-1]-a[arr.Length-1]).
                    int temp = arr[i];
                    arr[i] = arr[0];
                    arr[0] = temp;

                    // Re-adjust left  no-sort subarray to a heap
                    HeapAdjust(arr, 0, i);
                }
            }

            /// <summary>
            
    /// Adjust array to a big heap
            
    /// </summary>
            
    /// <param name="arr"></param>
            
    /// <param name="i"> sub-heap's root index </param>
            
    /// <param name="len"> sub-array's length </param>
            private static void HeapAdjust(int[] arr, int i, int len)
            {
                if (arr == null)
                {
                    throw new ArgumentNullException();
                }

                if (len > arr.Length || i > len - 1 || i < 0)
                {
                    throw new ArgumentOutOfRangeException();
                }

                for (int child = 2 * i + 1; child < len; i = child, child = 2 * i + 1)
                {
                    // Choose big one of children to compare with parent 
                    if (child < len - 1 && arr[child] < arr[child + 1])
                    {
                        child++;
                    }

                    // if the big child is greater than its parent, then swap them.
                    if (arr[i] < arr[child])
                    {
                        int temp = arr[i];
                        arr[i] = arr[child];
                        arr[child] = temp;
                    }
                    // if not, break the loop
                    else
                    {
                        break;
                    }
                }
            }
  • 相关阅读:
    CentOS 6.5下Git服务器搭建
    汉化Eclipse+配色方法(官方语言包)
    Cocos2d-x内存管理研究<二>
    PHP模板解析类实例
    smarty的ASSIGN()函数
    详解 $_SERVER 函数中QUERY_STRING和REQUEST_URI区别
    phpstorm 格式化代码方法
    php get_magic_quotes_gpc()函数用法介绍
    php示例代码
    MySQL数据类型:SQL_MODE设置不容忽视
  • 原文地址:https://www.cnblogs.com/ericwen/p/HeapSort.html
Copyright © 2011-2022 走看看