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;
                }
            }
  • 相关阅读:
    关于jabber协议
    xmpp相关链接,
    Implementation of the Server Dialback method as defined by the RFC3920
    好吧,隐藏的文件,
    Vue之methods watch和compute的区别和联系
    面向过程编程&面向对象编程
    JS高阶---线程与事件机制(小结)
    Vuex简介
    Vuex操作步骤
    vue单页面应用刷新网页后vuex的state数据丢失的解决方案
  • 原文地址:https://www.cnblogs.com/Langzi127/p/1689990.html
Copyright © 2011-2022 走看看