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. centos下安装python

    下载网址:http://ftp.gnu.org/gnu/gdb/ 1.编译python必须安装开发工具 # yum groupinstall "Development tools" ...

  2. Ajax请求内嵌套Ajax请求的方法

    前段时间做项目,需要把全国省市的两个XML文件整合成一个JSON格式的数据,手写的话觉得数据太多了,而且容易出错,于是就想到了用Ajax嵌套的方法来解决,就想平时用Ajax的方法直接嵌套,都会先读出外 ...

  3. SWFUpload接受服务器Action返回的参数

    首先我们要了解这个函数 function uploadSuccess(file, serverData) { try { var progress = new FileProgress(file, t ...

  4. distinguish and differentiate

    According to Cambridge Dictionary distinguish:to recognize or understand the difference between two ...

  5. Effective java笔记6--异常

    充分发挥异常的优点,可以提高一个程序的可读性.可靠性和可维护性.如果使用不当的话,它们也会带来负面影响. 一.只针对不正常的条件才使用异常 先看一段代码: //Horrible abuse of ex ...

  6. 数往知来 ADO.NET <八>

    ADO.NET基础 学习目的:通过程序访问数据库 ,ADO.NET就是一组类库, -->connection   用来连接数据库的类 语法:首先需要一个连接字符串 -->以SQL serv ...

  7. SandDock 应用示例

    直接上代码 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data ...

  8. Weibo Crawler in Action

    1.要写一个微博爬虫,得分开几个模块来做: (1)模拟登录 (2)模拟浏览 (3)针对短时间内大量访问而引起怀疑的禁止登陆解决方案 (4)其他 (1)模拟登陆模块 前提:要模拟登录,得首先知道在登录微 ...

  9. Web Api 2 怎么支持 Session

    Add protected void Application_PostAuthorizeRequest() { System.Web.HttpContext.Current.SetSessionSta ...

  10. jszs 历史管理

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...