数据结构 - 2-路插入排序 具体解释 及 代码(C++)
2-路插入排序 具体解释 及 代码
本文地址: http://blog.csdn.net/caroline_wendy/article/details/24267679
2-路插入排序的思想非常有意思:
通过一个辅助的循环数组, 假设大于最大的元素, 则插入至尾部, 假设小于最小的元素, 则插入至头部,
假设在两者之间, 採用折半查找的方式,移动一部分的元素;
设计到循环数组的中间值的查找和数据移动的问题.
因为折半查找能够降低比較次数,
首尾插入又不须要移动元素, 即移动次数约为[(n^2)/8], 有1/2的元素不须要移动, 每次移动(1/2*1/2), 所以移动次数为[(n^2)/8];
可是须要n个辅助空间, 时间复杂度扔是O(n^2);
代码例如以下:
/*
* test.cpp
*
* Created on: 2014.04.21
* Author: Spike
*/ /*eclipse cdt, gcc 4.8.1*/ #include <iostream>
#include <deque> void print(const std::deque<int>& L) {
for(auto i: L) {
std::cout << i << " ";
} std::cout << std::endl;
} void insertSort(std::deque<int>& L)
{
int first(0), final(0);
std::size_t n(L.size()); std::deque<int> D(n); D[0] = L[0]; for(std::size_t i=1; i<n; ++i) {
if (L[i] < D[first]) { //小于最小元素
first = (first-1+n)%n;
D[first] = L[i];
print(D);
} else if (L[i] > D[final]) { //大于最大元素
final = (final+1+n)%n;
D[final] = L[i];
print(D);
} else {
//折半查找
int low(0), high((final-first)%n);
int temp(first), end(high);
while (low <= high) {
int m = (low + high)/2;
if (L[i] < D[(m+temp)%n])
high = m-1;
else
low = m+1;
}
for (int j=end; j>=high+1; --j) {
D[(j+temp+1)%n] = D[(j+temp)%n];
}
D[(high+temp+1)%n] = L[i];
final = (final+1+n)%n;
print(D);
}
} //复制数组
for (std::size_t k=0; k<n; k++) {
L[k] = D[(first+k)%n];
}
} int main(void)
{
std::deque<int> L = {9, 3, 2, 4, 5, 8, 7, 6};
print(L);
insertSort(L);
print(L);
return 0;
}
输出:
9 3 2 4 5 8 7 6
9 0 0 0 0 0 0 3
9 0 0 0 0 0 2 3
4 9 0 0 0 0 2 3
4 5 9 0 0 0 2 3
4 5 8 9 0 0 2 3
4 5 7 8 9 0 2 3
4 5 6 7 8 9 2 3
2 3 4 5 6 7 8 9
数据结构 - 2-路插入排序 具体解释 及 代码(C++)的更多相关文章
- 数据结构 - 表插入排序 具体解释 及 代码(C++)
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u012515223/article/details/24323125 表插入排序 具体解释 及 代码 ...
- 数据结构 - 堆排序(heap sort) 具体解释 及 代码(C++)
堆排序(heap sort) 具体解释 及 代码(C++) 本文地址: http://blog.csdn.net/caroline_wendy 堆排序包括两个步骤: 第一步: 是建立大顶堆(从大到小排 ...
- 数据结构 - 归并排序(merging sort) 具体解释 及 代码
归并排序(merging sort) 具体解释 及 代码 本文地址: http://blog.csdn.net/caroline_wendy 归并排序(merging sort): 包括2-路归并排序 ...
- 数据结构 - 希尔排序(Shell's Sort) 具体解释 及 代码(C++)
数据结构 - 希尔排序(Shell's Sort) 具体解释 及 代码(C++) 本文地址: http://blog.csdn.net/caroline_wendy/article/details/2 ...
- 数据结构 - 树形选择排序 (tree selection sort) 具体解释 及 代码(C++)
树形选择排序 (tree selection sort) 具体解释 及 代码(C++) 本文地址: http://blog.csdn.net/caroline_wendy 算法逻辑: 依据节点的大小, ...
- android widget 开发实例 : 桌面便签程序的实现具体解释和源代码 (上)
如有错漏请不吝拍砖指正,转载请注明出处,很感谢 桌面便签软件是android上经常使用软件的一种,比方比較早的Sticky Note,就曾很流行, Sticky Note的介绍能够參见 http:// ...
- python数据结构之直接插入排序
python数据结构之直接插入排序 #-*-encoding:utf-8-*- ''' 直接插入排序: 从序列的第二个元素开始,依次与前一个元素比较,如果该元素比前一个元素大, 那么交换这两个元素.该 ...
- [4] 算法之路 - 插入排序之Shell间隔与Sedgewick间隔
题目 插入排序法由未排序的后半部前端取出一个值.插入已排序前半部的适当位置.概念简单但速度不快. 排序要加快的基本原则之中的一个: 是让后一次的排序进行时,尽量利用前一次排序后的结果,以加快排序的速度 ...
- Vue学习之路之登录注册实例代码
Vue学习之路之登录注册实例代码:https://www.jb51.net/article/118003.htm vue项目中路由验证和相应拦截的使用:https://blog.csdn.net/wa ...
随机推荐
- JTAG Simplified
JTAG Simplified So the other day, I explored the JTAG bus interface which is frequently found in CPL ...
- Linux进程管理工具 Supervisord 的安装 及 入门教程
Supervisor是一个进程管理工具,官方的说法: 用途就是有一个进程需要每时每刻不断的跑,但是这个进程又有可能由于各种原因有可能中断.当进程中断的时候我希望能自动重新启动它,此时,我就需要使用到了 ...
- panel内嵌程序窗体
function RunAppInPanel(const AppFileName: string; ParentHandle: HWND; var WinHandle: HWND): Boolean; ...
- DirectX - dds图片格式(DDSURFACEDESC2)
DDS是DirectDraw Surface的缩写,它是DirectX纹理压缩(DirectX Texture Compression,简称DXTC)的产物. DXTC减少了纹理内存消耗的50%甚至更 ...
- 3.13. Notepad++中Windows,Unix,Mac三种格式之间的转换
由于历史原因,导致Windows,Unix/Linux,Mac三者之间,对于文件中所用回车换行符,表示的方法,都不一样. 这就导致了很多人都会遇到回车换行符的困惑,和需要在不同格式间进行转换. 其中, ...
- python测试开发django-26.表单提交之post登录案例
前言 注册和登录功能实现都是post请求接口,只不过注册是往数据库插入数据,登录是从数据库里面查询数据. 本篇接着上一篇写个简单的登录页面请求,用户注册时密码加密存储,用户登录时候对输入的密码校验. ...
- wince程序调用另外一个wince exe程序?
记住:要释放句柄 清空内存(当前程序) 在虚拟机下测试如图: 在reyo.ini文件中配置另一wince执行程序的路径,如果不配置程序会报错: 如果配置的程序不存在报错: 没有问题就调用所在位置的wi ...
- mysql error You must reset your password using ALTER USER statement before executing this statement.
mysql修改密码Your password does not satisfy the current policy requirements 出现这个问题的原因是:密码过于简单.刚安装的mysql的 ...
- tsung执行时报Can't locate Template.pm的解决
[root@openfire-x86v-app01 20141118-0931]# tsung_stats creating subdirectory data creating subdirecto ...
- 推荐ThinkJS
之前在学习node.js时,写过一些例子和demo,偶尔也会有人发email问我node.js的一些问题.因为是二三年前写的东西,当时使用的第三方库和node.js的版本跟最新的可能有所不同甚至比较大 ...