zoukankan      html  css  js  c++  java
  • Heap Sort

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

    代码
         #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
  • 相关阅读:
    计算1的个数
    【环境配置】配置git
    Spoj 9887 Binomial coefficients 构造
    程序猿与HR博弈之:有城府的表达你的兴趣爱好
    C和指针 (pointers on C)——第六章:指针(上)
    关于undo表空间配置错误的ORA-30012
    每天进步一点点——Linux系统中的异常堆栈跟踪简单实现
    javaScript 对象的使用
    手机游戏加密那点事儿_2d资源加密_1
    支持向量机
  • 原文地址:https://www.cnblogs.com/Langzi127/p/1692651.html
Copyright © 2011-2022 走看看