body, table{font-family: 微软雅黑; font-size: 13.5pt}
table{border-collapse: collapse; border: solid gray; border-width: 2px 0 2px 0;}
th{border: 1px solid gray; padding: 4px; background-color: #DDD;}
td{border: 1px solid gray; padding: 4px;}
tr:nth-child(2n){background-color: #f8f8f8;}

直接插入排序(Straight Insertion Sort):将一个记录插入到已经排好序的有序表中,从而得到一个新的、记录数增1的有序表。
#include<iostream>
using namespace std;
//直接插入排序
int straightInsertionSort(int* arr,int length);
void swap(int& elem1,int& elem2);
void test();
void printArr(int* arr,int length);
void swap(int& elem1,int& elem2)
{
        int tmp = elem1;
        elem1 = elem2;
        elem2 = tmp;
}
int straightInsertionSort(int* arr,int length)
{
        if(NULL==arr||length<=0)
                return -1;
        for(int idx=1;idx!=length;++idx)
        {
                //把后面的第idx个元素插入到前面的有序数组0~idx-1中
                int tmp = arr[idx];
                int behindIdx = idx - 1;
                while(behindIdx>=0&&arr[behindIdx]>tmp)         
                {
                        arr[behindIdx+1] = arr[behindIdx];
                        --behindIdx;
                }
                arr[++behindIdx] = tmp;
        }
        return 0;
}
void printArr(int* arr,int length)
{
        if(NULL==arr||length<=0)
                return ;
        for(int idx=0;idx!=length;++idx)
        {
                cout<<arr[idx]<<" ";
        }
        cout<<endl;
}
void test()
{
        int arr[] = {6,5,3,1,8,7,2,4};
        printArr(arr,8);
        straightInsertionSort(arr,8);
        printArr(arr,8);
        cout<<endl;
        int arr1[] = {1,2,3,4,5,6,7,8};
        printArr(arr1,8);
        straightInsertionSort(arr1,8);
        printArr(arr1,8);
        cout<<endl;
        int arr2[] = {2,2,2,2};
        printArr(arr2,4);
        straightInsertionSort(arr2,4);
        printArr(arr2,4);
        cout<<endl;
        int arr3[] = {2,2,1,2};
        printArr(arr3,4);
        straightInsertionSort(arr3,4);
        printArr(arr3,4);
        cout<<endl;
        int* arr4 = NULL;
        printArr(arr4,4);
        straightInsertionSort(arr4,4);
        printArr(arr4,4);
        cout<<endl;
}
int main()
{
        test();
        system("pause");
}

//直接插入排序改进,使用二分查找找到要插入元素的位置,然后才开始移动元素。
#include<iostream>
using namespace std;
//直接插入排序改进,二分插入排序
//对左边已经排好序的序列使用二分查找,找到右边序列待插入元素要插入的位置,然后在整体移动
void test();
void printArr(int* arr,int length);
int binaryInsertionSort(int* arr,int length);
int binaryInsertionSort(int* arr,int length)
{
        if(NULL==arr||length<=0)
                return -1;
        for(int idx=1;idx!=length;++idx)
        {
                int tmp = arr[idx];
                int left = 0;
                int right = idx-1;
                int middle;
                while(left<=right)
                {
                        middle = (left+right)/2;
                        if(arr[middle]<=tmp)    //稳定排序
                                left = middle + 1;
                        else if(arr[middle]>tmp)
                                right = middle - 1;
                }
                for(int iidx=idx-1;iidx>=left;--iidx)
                {
                        arr[iidx+1] = arr[iidx];
                }
                arr[left] = tmp;
        }
        return 0;
}
void printArr(int* arr,int length)
{
        if(NULL==arr||length<=0)
                return ;
        for(int idx=0;idx!=length;++idx)
        {
                cout<<arr[idx]<<" ";
        }
        cout<<endl;
}
void test()
{
        int arr[] = {6,5,3,1,8,7,2,4};
        printArr(arr,8);
        binaryInsertionSort(arr,8);
        printArr(arr,8);
        cout<<endl;
        int arr1[] = {1,2,0,-1,5,6,7,8};
        printArr(arr1,8);
        binaryInsertionSort(arr1,8);
        printArr(arr1,8);
        cout<<endl;
        int arr2[] = {2,2,2,2};
        printArr(arr2,4);
        binaryInsertionSort(arr2,4);
        printArr(arr2,4);
        cout<<endl;
        int arr3[] = {2,2,1,2};
        printArr(arr3,4);
        binaryInsertionSort(arr3,4);
        printArr(arr3,4);
        cout<<endl;
        int arr5[] = {1,2,3,4,5,6,7,8};
        printArr(arr5,8);
        binaryInsertionSort(arr5,8);
        printArr(arr5,8);
        cout<<endl;
        int* arr6 = NULL;
        printArr(arr6,4);
        binaryInsertionSort(arr6,4);
        printArr(arr6,4);
        cout<<endl;
}

int main()
{
        test();
        system("pause");
}

直接插入排序(Straight Insertion Sort)的更多相关文章

  1. 插入排序—直接插入排序(Straight Insertion Sort)

    基本思想: 将一个记录插入到已排序好的有序表中,从而得到一个新,记录数增1的有序表.即:先将序列的第1个记录看成是一个有序的子序列,然后从第2个记录逐个进行插插入到已入,直至整个序列有序为止. 要点: ...

  2. 排序之直接插入排序(Straight Insertion Sort)

    一.直接插入排序(Straight Insertion Sort) 排序的过程如下:给定无需序列:(3,6,9,7,1,8,2,4) ① 3,6,9,7,1,8,2,4 (将6插入到有序序列3中) ② ...

  3. 直接插入排序(Straight Insertion Sort)的C语言实现

    原创文章,转载请注明来自钢铁侠Mac博客http://www.cnblogs.com/gangtiexia   直接插入排序(Straight Insertion Sort)的基本思想是将新记录插入到 ...

  4. 直接插入排序(Straight Insertion Sort)

    直接插入排序(Straight Insertion Sort)的基本操作是将一个记录插入到已经排好序的有序表中,从而得到一个新的.记录数增1的有序表. /* 对顺序表L作直接插入排序 */ void ...

  5. ​直接插入排序(Straight Insertion Sort)

    1.定义 直接插入排序(Straight Insertion Sort)的基本操作是将一个记录插入到已经排好序的有序表中,从而得到一个新的.记录数增1的有序表. 插入排序(Insertion Sort ...

  6. 《算法4》2.1 - 插入排序算法(Insertion Sort), Python实现

    排序算法列表电梯: 选择排序算法:详见 Selection Sort 插入排序算法(Insertion Sort):非常适用于小数组和部分排序好的数组,是应用比较多的算法.详见本文 插入排序算法的语言 ...

  7. 【算法】插入排序(Insertion Sort)

    (PS:内容参考MIT算法导论) 插入排序(Insertion Sort): 适用于数目较少的元素排序 伪代码(Pseudocode): 例子(Example): 符号(notation): 时间复杂 ...

  8. 【排序基础】5、插入排序法 - Insertion Sort

    插入排序法 - Insertion Sort 文章目录 插入排序法 - Insertion Sort 插入排序设计思想 插入排序代码实现 操作:插入排序与选择排序的比较 简单记录-bobo老师的玩转算 ...

  9. 【算法】插入排序(Insertion Sort)(三)

    插入排序(Insertion Sort) 插入排序(Insertion-Sort)的算法描述是一种简单直观的排序算法.它的工作原理是通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相 ...

随机推荐

  1. 下一个更大的数 Next Greater Element

    2018-09-24 21:52:38 一.Next Greater Element I 问题描述: 问题求解: 本题只需要将nums2中元素的下一个更大的数通过map保存下来,然后再遍历一遍nums ...

  2. 在python中使用正则表达式(三)

    这里主要说一下贪婪匹配和非贪婪匹配  贪婪匹配:匹配尽可能多的字符:  非贪婪匹配:匹配尽可能少的字符 python的正则匹配默认是贪婪匹配 例子: >>> re.match(r'^ ...

  3. ThinkPHP实用项

    跟踪信息 – 两种模式 – Sql调试 – 性能调试

  4. 安卓中使用HttpURLConnection连接网络简单示例 --Android网络编程

    MainActivity.java: package thonlon.example.cn.httpurlconnectionpro; import android.os.Bundle;import ...

  5. 20170922xlVBA_GetCellTextFromWordDocument

    Sub GetCellTextFromWordDocument() '应用程序设置 Application.ScreenUpdating = False Application.DisplayAler ...

  6. Android Studio 一直卡在building解决办法

    1.随便找一个你能运行的as项目 2.打开gradle-wrapper.properties,文件目录:项目/gradle/wrapper/gradle-wrapper.properties 3.复制 ...

  7. android -------- Data Binding的使用 ( 六) 自定义属性

    今天来说说DataBinding在自定义属性的使用 默认的android命名空间下,我们会发现并不是所有的属性都能直接通过data binding进行设置,比如margin,padding,还有自定义 ...

  8. Javascript的常见数据类型以及相应操作

    JavaScript概述 1 ECMAScript和JavaScript的关系 1996年11月,JavaScript的创造者--Netscape公司,决定将JavaScript提交给国际标准化组织E ...

  9. Matlab-2:二分法工具箱

    function g=dichotomy(f,tol) %this routine uses bisection to find a zero of user-supplied %continuous ...

  10. 笔记react router 4(一)

    用过react router4.X的小伙伴一定知道,比起3.X的版本,router的使用上有了很大的改变. 首先,我们只需要安装 react-router-dom 即可使用.看到“dom”想必你就该知 ...