1.直接插入排序

直接插入排序的过程可以理解为一个固定长度的数组被分为两个集合,即已排序集合和未排序。

开始时已排序集合为空,而未排序集合即为整个数组。当排序开始后插入一个对象,已排序集合元素数目加1,相应地未排序集合元素数目减1,重复插入过程直至将未排序集合清空为止,这时排序集合就是最终结果。如下图:

C++实现如下,为了使得程序可以对各种基本数据类型都能排序,使用了模板类,注意模板类的类声明和成员函数实现必须在同一个cpp文件里面,不能分开!!

 #ifndef INSERTSORT_H
#define INSERTSORT_H
#include <vector>
#include <iostream>
using std::cout;
using std::endl;
using std::vector; template <typename T>
class InsertSort
{
private:
unsigned len;
vector<T> list;
public:
InsertSort(vector<T> _list, unsigned _len)
{
for (unsigned i = ; i < _len; ++i) list.push_back(_list[i]);
this->len = _len;
}
void insertSort()
{
T insertNum;
for (unsigned i = ; i < len; ++i) // Insert number for len times
{
insertNum = list[i]; // The number is to be inserted
unsigned j = i;
while (j && insertNum < list[j-]) // Find the position to insert the target number in
{
list[j] = list[j - ];
--j;
}
list[j] = insertNum;
}
}
void out()
{
for (unsigned i = ; i < len; ++i)
{
cout << list[i] << " ";
if ((i+)% == ) cout << endl;
}
cout << endl;
}
};
#endif

2.二分插入排序

因为直接插入排序在搜索插入位置的时候,效率很低,对于大数组,尤其不适用,于是采用二分插入排序,又叫折半插入排序,二分插入排序是采用折半查找法寻找要插入的位置。

下面演示了折半查找法在目标数组中确定数字35的位置:

  • 首先确定目标数组的中间位置数字为45
  • 由于45 > 35,所以降原数组折半并取左半部分子数组作为搜索目标,左半部分的中间元素为23
  • 由于23 < 35,所以再折半,选择子数组的有半部分作为搜索目标
  • 而35 < 36,于是确定35的位置在23和36之间。

下面给出C++实现:

 #ifndef BINARYINSERTSORT_H
#define BINARYINSERTSORT_H
#include <vector>
using std::vector;
template <typename T>
class BinaryInsertSort
{
private:
int len;
vector<T> list;
public:
BinaryInsertSort(vector<T> _list, int _len)
{
for (int i = ; i < _len; ++i) list.push_back(_list[i]);
this->len = _len;
}
void binaryInsertSort()
{
int middle;
for (int i = ; i < len; ++i)
{
T insertNum = list[i];
int left = ;
int right = i - ;
while (left <= right)//Find the insertation position with binary search
{
middle = (left + right) / ;
if (insertNum > list[middle])
left = middle + ;
else
right = middle - ;
}
for (int j = i; j > left; --j) list[j] = list[j-];
list[left] = insertNum;
}
}
void out()
{
for (unsigned i = ; i < len; ++i)
{
cout << list[i] << " ";
if ((i+)% == ) cout << endl;
}
cout << endl;
}
};
#endif

3.测试运行

 #include "InsertSort.h"
#include "BinaryInsertSort.h"
#include <vector>
using namespace std; const unsigned numEle = ;
int data[numEle] = {,,,,,,,}; int main()
{
vector<int> testData;
for (unsigned i = ; i < numEle; ++i) testData.push_back(data[i]);
//InsertSort<int> test(testData, numEle);
//test.insertSort();
BinaryInsertSort<int> test(testData, numEle);
test.binaryInsertSort();
test.out();
return ; }

4. 参考文献

左飞:C++数据结构原理与经典问题求解

数据结构复习:直接插入排序与二分插入排序的C++实现的更多相关文章

  1. 我的Java开发学习之旅------>Java经典排序算法之二分插入排序

    一.折半插入排序(二分插入排序) 将直接插入排序中寻找A[i]的插入位置的方法改为采用折半比较,即可得到折半插入排序算法.在处理A[i]时,A[0]--A[i-1]已经按关键码值排好序.所谓折半比较, ...

  2. 数据结构复习:希尔排序的C++实现

    1.原理介绍 希尔排序又称为缩小增量排序,由D.L.Shell在1959年提出而得名. 该算法先取一个小于数据表中元素个数 n 的整数gap, 并以此作为第一个间隔,将数据分为gap个子序列,所有距离 ...

  3. 数据结构图文解析之:直接插入排序及其优化(二分插入排序)解析及C++实现

    0. 数据结构图文解析系列 数据结构系列文章 数据结构图文解析之:数组.单链表.双链表介绍及C++模板实现 数据结构图文解析之:栈的简介及C++模板实现 数据结构图文解析之:队列详解与C++模板实现 ...

  4. 向Array中添加二分插入排序

    二分插入排序思路 先在有序区通过二分查找的方法找到移动元素的起始位置,然后通过这个起始位置将后面所有的元素后移. 二分插入排序实现 Function.prototype.method = functi ...

  5. java实现 排序算法(鸡尾酒排序&选择排序&插入排序&二分插入排序)

    1.鸡尾酒排序算法 源程序代码: package com.SuanFa; public class Cocktial {    public static void main(String[] arg ...

  6. 排序算法(2)--Insert Sorting--插入排序[2]--binary insertion sort--折半(二分)插入排序

    作者QQ:1095737364    QQ群:123300273     欢迎加入! 1.基本思想 二分法插入排序的思想和直接插入一样,只是找合适的插入位置的方式不同,这里是按二分法找到合适的位置,可 ...

  7. ZT 二分插入排序也称折半插入排序

    二分插入排序也称折半插入排序,基本思想是:设数列[0....n]分为两部分一部分是[0...i]为有序序列,另一部分是[i+1.....n]为无序序列,从无序序列中取一个数 x ,利用二分查找算法找到 ...

  8. c语言描述的二分插入排序法

    #include<stdio.h> #include<stdlib.h> //二分插入排序法 void BinsertSort(int a[],int n){ int low, ...

  9. js【生成规定数量不重复随机数】、【冒泡排序】、【鸡尾酒排序】、【选择排序】、【插入排序】、【未完工的二分插入排序】------【总结】

    [生成规定数量不重复随机数] function creatRandom( num ){ var randomLen = num, ranArr = [], thisRan = null, whileO ...

随机推荐

  1. centos6.5 安装fctix 五笔输入法

    摸索了大半晚上,终于搞定,网上的东西看了N多篇不是这问题就是那问题,看来不同的OS下,小白我还是太嫩了些. 1,删除输入法,这一步是清除输入法,操作完成后,桌面/系统/首先项/输入法的IM Choos ...

  2. BF-KMP 算法

    #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include<string. ...

  3. VMware 命令行下安装以及导入Ubuntu系统

    前提: 鉴于个人PC性能太弱,考虑是否可以将在PC上搭建好的环境移植到高性能服务器上.想到后就干呗. 下载完对应操作系统的安装包后按如下步骤操作: 安装包名称:VMware-Workstation-F ...

  4. sql server 2008 r2 出问题

    1.想利用sql2008的数据挖掘功能,以为是没有安装全,所以就卸载了. (1)利用Windows Installer Clean UP将以前的卸载干净 (2)出现了Could not open ke ...

  5. linq性能剖析

    Orcas(VS2008&Framework3.5)给我们带来了很多令人兴奋的新特性,尤其是LINQ的引进,可以说方便了一大批开发 人员和框架设计人员.过去,当我们使用O/RMapping的一 ...

  6. effective c++:对象的赋值运算

    operator 中处理”自我赋值“ operator=操作符缺省情况下返回引用——TYPE& TYPE::operator=(const TYPE&),原因很简单,operator= ...

  7. 彻底弄懂css中单位px和em的区别(转)

    国内的设计师大都喜欢用px,而国外的网站大都喜欢用em,那么两者有什么区别,又各自有什么优劣呢? 1. IE无法调整那些使用px作为单位的字体大小: 2. 国外的大部分网站能够调整的原因在于其使用了e ...

  8. 定制一个FlatBuffers编译器

    个人并不喜欢FlatBuffers编译器生成的代码,原因是我已经习惯了unix风格的代码. 不喜欢之处大致有以下: 1 命名法使用了Pascal命名法,而我个人习惯了小写字母加下划线式的unix式命名 ...

  9. [转]Python文件操作

    前言 这里的“文件”不单单指磁盘上的普通文件,也指代任何抽象层面上的文件.例如:通过URL打开一个Web页面“文件”,Unix系统下进程间通讯也是通过抽象的进程“文件”进行的.由于使用了统一的接口,从 ...

  10. the behavior of the UICollectionViewFlowLayout is not defined because:

    the behavior of the UICollectionViewFlowLayout is not defined because:    the item height must be le ...