zoukankan      html  css  js  c++  java
  • 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;
                }
            }
  • 相关阅读:
    [bzoj 1031][JSOI2007]字符加密Cipher
    [bzoj 3224] tyvj 1728 普通平衡树
    分治算法例题
    Codeforces 1146F. Leaf Partition(树形DP)
    Codeforces 1146H. Satanic Panic(极角排序+DP)
    Codeforces 578E. Walking!(贪心+线段树)
    学习日记0802函数递归,三元表达式,列表生成式,字典生成式,匿名函数+内置函数
    学习日记0808
    0803学习日志迭代器
    学习日记0804生成器
  • 原文地址:https://www.cnblogs.com/Langzi127/p/1689990.html
Copyright © 2011-2022 走看看