• Insertion Sort


    1. 直接插入排序算法:

    代码
            /// <summary>
            
    /// 插入排序
            
    /// </summary>
            
    /// <param name="data"></param>
            public static void InsertionSort(int[] data)
            {
                
    if (data == null || data.Length < 1)
                {
                    
    throw new ArgumentNullException("data");
                }

                
    int temp, index;
                
    for (int i = 1; i < data.Length; i++)
                {
                    temp 
    = data[i];
                    index 
    = i;

                    
    while (index > 0 && temp < data[index - 1])
                    {
                        data[index] 
    = data[--index];
                    }
                    data[index] 
    = temp;
                }
            }

    2. 折半插入排序:

    代码
            /// <summary>
            
    /// 折半插入排序
            
    /// </summary>
            
    /// <param name="data"></param>
            public static void Bin_InsertionSort(int[] data)
            {
                
    if (data == null || data.Length < 1)
                {
                    
    throw new ArgumentNullException("data");
                }

                
    int low, mid, high;
                
    for (int i = 1; i < data.Length; i++)
                {
                    
    // 折半查找
                    low = 0;
                    high 
    = i - 1;
                    
    while (low <= high)
                    {
                        mid 
    = (low + high) / 2;
                        
    if (data[i] < data[mid])
                        {
                            high 
    = mid - 1;
                        }
                        
    else
                        {
                            
    if (data[i] > data[mid])
                            {
                                low 
    = mid + 1;
                            }
                            
    else
                            {
                                low 
    = mid;
                                
    break;
                            }
                        }                    
                    }

                    
    // 移动元素
                    int temp = data[i];
                    
    for (int j = i; j > low; j--)
                    {
                        data[j] 
    = data[j - 1];
                    }
                    data[low] 
    = temp;
                }
            }
  • 相关阅读:
    VCalendar
    xls和xlsx的区别
    alioss的常见问题
    别再写 shǐ 山代码了。。。
    一款高颜值的 MySQL 管理工具,超好用。。
    Spring Boot Admin 横空出世!
    Redis 官方可视化工具,功能真心强大。。。
    Nginx 从安装到高可用,一篇搞定,99% 的人都收藏了。。
    duobule 转 int 问题
    mysql慢sql查询
  • 原文地址:https://www.cnblogs.com/Langzi127/p/1689990.html
Copyright © 2020-2023  润新知