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;
                }
            }
        }

 

转载于:https://www.cnblogs.com/ericwen/archive/2012/07/18/HeapSort.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值