Heap Sort - recursion

Heap Sort 

  1. Build a max heap using exsiting array, which is called Heapify
  2. Swap root with the last element, and re-Heapify the root node

Heapify

Heapify(array, n, i) = 1) compare node[i] with children   2)node[i] is already the largest one, no op.  3) child is largest one, swap, then Heapify(array, n, 2*i + 1 or 2i* + 2)

Code

    public class HeapSort
    {
        /// <summary>
        /// Heapify the array
        /// </summary>
        /// <param name="array">array</param>
        /// <param name="n">total size of the heap</param>
        /// <param name="i">starting node</param>
        public void Heapify(int[] array, int n, int i)
        {
            int left = 2*i + 1;
            int right = 2*i + 2;

            int largest = i;

            if (left < n)
            {
                if (array[left] > array[largest])  //  <---- 此处注意,要用array[largest] instead of array[i], 这样我们可以一直跟踪最大的下标, 而不会错误的交换次大的下标
                {
                    largest = left;
                }
            }

            if (right < n)
            {
                if (array[right] > array[largest])
                {
                    largest = right;
                }
            }

            if (largest != i)
            {
                int temp = array[i];
                array[i] = array[largest];
                array[largest] = temp;
                Heapify(array, n, largest);
            }
        }

        public void BuildHeap(int[] array)
        {
            for(int i = array.Length / 2 - 1; i >= 0; i--)
            {
                Heapify(array, array.Length, i);
            }
        }

        public void Sort(int[] array)
        {
            if (array == null || array.Length == 0)
            {
                return;
            }

            BuildHeap(array);

            for(int i = array.Length - 1; i >= 0; i--)
            {
                Swap(ref array[0], ref array[i]);
                Heapify(array, i, 0);
            }
        }

        public void Print(int[] array)
        {
            for(int i = 0; i < array.Length; i++)
            {
                Console.WriteLine(array[i]);
            }
        }

        private void Swap(ref int a, ref int b)
        {
            int temp = a;
            a = b;
            b = temp;
        }
    }

Comments

  1. Heap sort is a in place sort.
  2. Time complexity is O(NLogN) for Heapify.A quick look over the above algorithm suggests that the running time is O(nlg(n)), since each call to Heapify costs O(lg(n)) and Build-Heap makes O(n) such calls.

转载于:https://www.cnblogs.com/xuyanran/p/8415918.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值