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. 性能测试之Windows常见性能计数器

    性能计数器(counter)是描述服务器或操作系统性能的一些数据指标.计数器在性能测试中发挥着“监控和分析”的关键作用,尤其是在分析系统的可扩展性.进行性能瓶颈的定位时,对计数器的取值的分析非常关键. ...

  2. <转>安卓软件测试的几个要点

    1.界面 ① 文字错误.图片不显示或显示不正确.缺少输入项.按钮的大小和点击效果 ② 布局.图片和配色设计问题,测试人员很难进入 ③ 提示信息,提示信息语言准确简洁,有指导性.在应该提示的位置放入提示 ...

  3. GitHub使用教程及常见错误解决

    1.下载Git并安装 Git for Windows Git-1.8.4-preview20130916.exe 按照默认步骤完成安装 2.设置SSH建立计算机与Github的链接 2.1 点击 开始 ...

  4. ajax 访问--提高安全性

    首先受到struts token的启发,产生了客户端发起的ajax请求进行验证的想法,大致思路是客户端每次请求产生一个key ,然后服务端接收到key,然后解析,判断是否为合法key, 对于不带key ...

  5. @Html.Raw()

    asp.net mvc中把html字符串以html效果输出来, @string变更输出的是HTML代码, 如果想以HTML标签效果输出来可以用函数@Html.Raw(str) 输出来的就是网效果了, ...

  6. Scrum之Sprint会议

    Scrum的项目过程有一系列的Sprint组成. Sprint的长度一般控制在2-4周. 通过固定的周期保持良好的节奏. 产品的设计.开发.测试都在Sprint期间完成. Sprint结束时交付可以工 ...

  7. [转] js prototype详解

    JavaScript能够实现的面向对象的特征有:·公有属性(public field)·公有方法(public Method)·私有属性(private field)·私有方法(private fie ...

  8. FbinstTool万能启动超级简单教程

    转载自http://bbs.wuyou.com/forum.php?mod=viewthread&tid=156383 秒到1分钟时间.如果你导入的是超过几百M以上的文件,相对的等待时间会更长 ...

  9. chrome 浏览器 开发者工具 性能检测 参数解释

    Sending is time spent uploading the data/request to the server. It occurs between blocking and waiti ...

  10. 用pdo实现的织梦后台留言板

    <?php //ini_set("display_errors", "On"); include("data/common.inc.php&qu ...