Heap Sort

堆积排序是对直接选择排序的一种改进,具体实现如下:

 

ExpandedBlockStart.gif 代码
     #region  Heap Sort
        
private   static   void  Adjust( int [] data,  int  rootIndex,  int  lastIndex)
        {
            
if  (data  ==   null   ||  data.Length  <   1 )
            {
                
throw   new  ArgumentNullException( " data " );
            }

            
int  childIndex  =   2   *  rootIndex  +   1 , temp;
            
while  (childIndex  <=  lastIndex)
            {
                
if  (childIndex  <  lastIndex  &&  data[childIndex]  <  data[childIndex  +   1 ])
                {
                    childIndex
++ ;
                }
                
if  (data[(childIndex  -   1 /   2 >=  data[childIndex])
                {
                    
break ;
                }

                temp 
=  data[(childIndex  -   1 /   2 ];
                data[(childIndex 
-   1 /   2 =  data[childIndex];
                data[childIndex] 
=  temp;

                childIndex 
=   2   *  childIndex  +   1 ;
            }
        }

        
///   <summary>
        
///  堆积排序
        
///   </summary>
        
///   <param name="data"></param>
         public   static   void  HeapSort( int [] data)
        {
            
if  (data  ==   null   ||  data.Length  <   1 )
            {
                
throw   new  ArgumentNullException( " data " );
            }

            
for  ( int  i  =  (data.Length  -   1 /   2 ; i  >=   0 ; i -- )
            {
                Adjust(data, i, data.Length 
-   1 );
            }

            
int  temp;
            
for  ( int  i  =  data.Length  -   1 ; i  >   0 ; i -- )
            {
                temp 
=  data[i];
                data[i] 
=  data[ 0 ];
                data[
0 =  temp;

                Adjust(data, 
0 , i  -   1 );
            }
        }
        
#endregion

 

 

转载于:https://www.cnblogs.com/Langzi127/archive/2010/03/23/1692651.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值