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. Linux makefile教程之总述二[转]

    Makefile 总述——————— 一.Makefile里有什么? Makefile里主要包含了五个东西:显式规则.隐晦规则.变量定义.文件指示和注释. 1.显式规则.显式规则说明了,如何生成一个或 ...

  2. [转] 接触C# 反射 2

    逆心 原文 反射基础,2013-4. 反射用于在程序运行过程中,获取类里面的信息或发现程序集并运行的一个过程.通过反射可以获得.dll和.exe后缀的程序集里面的信息.使用反射可以看到一个程序集内部的 ...

  3. [Papers]NSE, $u$, Lorentz space [Sohr, JEE, 2001]

    $$\bex \bbu\in L^{p,r}(0,T;L^{q,\infty}(\bbR^3)),\quad\frac{2}{p}+\frac{3}{q}=1,\quad 3<q<\inf ...

  4. 【Mongo】MongoVUE的使用

    我一直是在终端操作的mongo,各种update set remove insert,后来查了下有个叫mongoVUE的客户端,界面化比较方便操作吧~ 网上搜了下,说MongoVUE已经n年没有更新了 ...

  5. UITableViewCell 自定义绘制 性能高

    // //  FoodListTableViewCellB.m //  BabyFood // //  Created by zhuang chaoxiao on 16/3/7. //  Copyri ...

  6. org.unsaved transient instance - save the transient instance before flushing: bug解决方案

    最近开发和学习过程中,遇到很多零碎的知识点,在此简单地记录下: 1.遇如下bug: org.unsaved transient instance - save the transient instan ...

  7. Ioc注入方式写dubbo client(非set beans)

    @Autowired注解的方式注解 Spring框架中进行注入式,使用@Autowired. @Autowired可以对成员变量.方法和构造函数进行标注,来完成自动装配的工作,这里必须明确:@Auto ...

  8. APP测试时不可忽视搭建代理服务器抓包测试的必要性

    这几天测的一个app,后台从已有服务器搬迁到了阿里云,接口api之类的都没有变化,但测试时发现客户端始终无法使用,每次点击都无法获得服务器反馈 用python编写脚本调接口,没问题,返回数据一切正常, ...

  9. 【Linux】Semaphore信号量线程同步的例子

    0. 信号量 Linux下的信号量和windows下的信号量稍有不同. Windows Windows下的信号量有一个最大值和一个初始值,初始值和最大值可以不同.  而且Windows下的信号量是一个 ...

  10. 浅谈“be practical and realistic”

    一 “实事求是”这个词,一般认为是古人的一种治学观念,后来经咏芝的发明.阐释.以及“应用”,成为“基本思想路线”(具体可参看大学思想政治教科书),被称为“活的灵魂”.这里不想过多地牵扯政治话题,仅就我 ...