插入排序,是指将从1 –> size-1的数一个个插入到前面已经排序好的数组中。

时间复杂度:O(n^2) , O(nlgn) (lgn指使用二分查找插入点位置)

空间复杂度:O(1)

// #if __cplusplus < 201103L
// #error "must be compiled under c++11 support platform!!!"
// #endif
#include <iostream>
#include <algorithm>
#include <iterator>
#include <cassert>
using namespace std; //find insert position function
int FindInsertPos(int varList[], const int size, const int target)
{
if (!varList || size < 0)
{
assert(false);
return -1;
}
int insertPos = 0;
for (int i = 0; i < size;i++)
{
if (target < varList[i])
{
insertPos = i;
break;
}
}
return insertPos;
}
//binary find insert position function
int BinaryFindInsertPos(int varList[], const int size,const int target)
{
if (!varList || size < 0)
{
assert(false);
return -1;
}
int insertPos = 0;
int begin = 0;
int end = size - 1;
int mid = (begin + end) >> 1;
while (begin < end)
{
if (target > varList[begin] && target <= varList[mid] && 1 == (mid - begin))
{
insertPos = mid;
break;
}
else if (target > varList[mid] && target < varList[end] && 1 == (end - mid))
{
insertPos = end;
break;
}
else if (target < varList[mid])
{
end = mid - 1;
}
else if (target > varList[mid])
{
begin = mid + 1;
}
mid = (begin + end) >> 1;
} return insertPos;
}
//insert sort function
void InsertSort(int varList[], const int size)
{
if (!varList || size <= 1)
{
return;
}
auto findInsertPosFunc = BinaryFindInsertPos; //FindInsertPos
for (int i = 1; i < size; i++)
{
if (varList[i - 1] > varList[i])
{
int tmp = varList[i];
int insertPos = findInsertPosFunc(varList, i, varList[i]);
//move
for (int j = i; j > insertPos;j--)
{
varList[j] = varList[j - 1];
}
varList[insertPos] = tmp;
}
}
} void test()
{
//case counter
int testCase = 0;
//sort function object
auto sortFunc = InsertSort;
//show case result lambda function
auto showFunc = [&testCase](const char* caseDescription){cout << "case[" << testCase++ << "]\t(" << caseDescription << ") \t\tok " << endl; }; cout << "test begin : " << endl << endl; //case empty list
{
sortFunc(nullptr, 0);
showFunc("case empty list");
}
//case wrong size
{
int nTestList[] = { 13, 52, 32, 15, 66, 2, 99, 202, 103, 2 };
sortFunc(nTestList, 0);
showFunc("case wrong size");
}
//case size == 1
{
int var = 13;
int varList[] = { var };
sortFunc(varList, 1);
assert(var == varList[0]);
showFunc("case size == 1");
}
//case normal sort
{
int varList[] = { 13, 52, 32, 15, 66, 2, 99, 202, 103, 2 };
const int size = sizeof(varList) / sizeof(int);
const int resultList[] = { 2, 2, 13, 15, 32, 52, 66, 99, 103, 202 };
static_assert(sizeof(varList) == sizeof(resultList), "size of varList is not equal with resultList!!"); sortFunc(varList, size);
for (int i = 0; i < size; i++){ assert(varList[i] == resultList[i]); }
showFunc("case normal sort");
}
//case sorted list
{
int varList[] = { 2, 2, 13, 15, 32, 52, 66, 99, 103, 202 };
const int size = sizeof(varList) / sizeof(int);
const int resultList[] = { 2, 2, 13, 15, 32, 52, 66, 99, 103, 202 };
static_assert(sizeof(varList) == sizeof(resultList), "size of varList is not equal with resultList!!"); sortFunc(varList, size);
for (int i = 0; i < size; i++){ assert(varList[i] == resultList[i]); }
showFunc("case sorted list");
}
cout << endl << "test done ! " << endl << endl;
}
int main(int argc, char* argv[])
{
test();
return 0;
}

C++11写算法之插入排序的更多相关文章

  1. C++11写算法之顺序查找

    从这篇博文起,将尝试使用C++11来写常用算法与数据结构. 本篇博文以最简单的顺序查找作为系列博文的起点,并作约定如下: 1,变量名 : varList : 函数名 : SequentialFind ...

  2. C++11写算法之二分查找

    同样的,二分查找很好理解,不多做解释,要注意二分查找的list必须是排好序的. 这里实现了两种二分查找的算法,一种递归一种非递归,看看代码应该差不多是秒懂.想试验两种算法,改变一下findFunc函数 ...

  3. C++11写算法之选择排序

    选择排序,顾名思义,指从数组后面将最小的值找出来,然后与最前面(指当前位置)值进行交换. 时间复杂度:O(n^2) 空间复杂度:O(1) 此处应用了C++11的auto , lambda , stat ...

  4. C++11写算法之冒泡排序

    冒泡排序很形象,指从数组后面将更小的值慢慢浮到前面去,每遍历一趟使得最小值浮到最前面(指当前位置). 这里有点小技巧,当某一次遍历过程中发现无交换,则说明此时数组已经排序完成,可提前退出. 时间复杂度 ...

  5. 算法分析中最常用的几种排序算法(插入排序、希尔排序、冒泡排序、选择排序、快速排序,归并排序)C 语言版

    每次开始动手写算法,都是先把插入排序,冒泡排序写一遍,十次有九次是重复的,所以这次下定决心,将所有常规的排序算法写了一遍,以便日后熟悉. 以下代码总用一个main函数和一个自定义的CommonFunc ...

  6. 【算法】插入排序 insertion_sort

    准备写个<STL 源代码剖析>的读书笔记,开个专栏.名为<STL 的实现>,将源代码整理一遍.非常喜欢侯捷先生写在封底的八个字:天下大事.必作于细.他在书中写到:"我 ...

  7. 一步一步写算法(之挑选最大的n个数)

    原文:一步一步写算法(之挑选最大的n个数) [ 声明:版权所有,欢迎转载,请勿用于商业用途.  联系信箱:feixiaoxing @163.com] 从一堆数据中挑选n个最大的数,这个问题是网上流传的 ...

  8. m_Orchestrate learning system---二十一、怎样写算法比较轻松

    m_Orchestrate learning system---二十一.怎样写算法比较轻松 一.总结 一句话总结:(1.写出算法步骤,这样非常有利于理清思路,这样就非常简单了 2.把问题分细,小问题用 ...

  9. arcgis api for js共享干货系列之一自写算法实现地图量算工具

    众所周知,使用arcgis api for js实现地图的量算工具功能,无非是调用arcgisserver的Geometry服务(http://localhost:6080/arcgis/rest/s ...

随机推荐

  1. Admin Finder

    #Created for coded32 and his teamopenfire Eliminated Some bugs from my last code shared here as Gues ...

  2. non-compatible bean definition of same name and class

    在整合struts2.1.6+spring2.5.6开发中,使用了注解和struts-convention来实现零配置管理.spring也使用注解annotation方式.现在的问题是:我在连个个不同 ...

  3. riched32.dll riched20.dll msftedit.dll 解析

    摘要: 本文对Rich Edit控件底层消息机制进行了讲解,以期读者对Windows平台下的Rich Edit控件有一个更深入的认识,同时对于使用Win32 SDK进行开发的人员具有一定参考价值.因为 ...

  4. XP如何安装字体

    1 点击控制面板,选择外观和主题,然后在左侧菜单中选择字体 2 点击文件,安装新字体选择驱动器和文件夹以找到自己下载的字体文件位置(如微软雅黑.ttf)点击确定之后可以安装.

  5. EasyBoot使用方法

    1 修改背景图片直接替换掉EasyBoot\disk1\ezboot目录下面的BACK.BMP文件即可.但是限于DOS功能限制,只能使用640×480像素,256位色的BMP图片.   2 鼠标左键单 ...

  6. 【C/C++学院】0831-类与对象的异常/面试100题1-100

    类与对象的异常 Cpp异常 #include <iostream> #include <string.h> using namespace std; //标识错误的类型 cla ...

  7. pcap学习

    #include <pcap.h> char errbuf[PCAP_ERRBUF_SIZE]; pcap_t *pcap_open_live(const char *device, in ...

  8. 【Java】Java_07 浮点型

    浮点型 类型 占用存储空间 表数范围 Float 4字节 -3.403E38~3.403E38 Double 8字节 -1.798E308~1.798E308 float类型又被称作单精度类型,尾数可 ...

  9. Loadrunner Analysis之Web Page Diagnostics

    Loadrunner Analysis之Web Page Diagnostics 分类: LoadRunner 性能测试 2012-12-31 18:47 1932人阅读 评论(2) 收藏 举报 di ...

  10. [1-4] 把时间当做朋友(李笑来)Chapter 4 【开拓我们的心智】 摘录

    1. 获得知识的基本途径  所有的人获取知识的最为基础的手段就是“体验”. 比“体验”再高级一点的获取知识的手段,就是“试错”(Trial and Error). 在“试错”这个手段的基础上,另外一个 ...