希尔排序( Shell Sort)
原文地址:http://www.stoimen.com/blog/,在此感谢作者!
Insertion sort is a great algorithm, because it’s very intuitive and it is easy to implement, but the problem
is that it makes many exchanges for each “light” element in order to put it on the right place. Thus
“light” elements at the end of the list may slow down the performance of insertion sort a lot. That is why
in 1959 Donald Shell proposed an algorithm that tries to overcome this problem by comparing items of
the list that lie far apart.
How to choose gap size
Not a cool thing about Shell sort is that we’ve to choose “the perfect” gap sequence for our list. However
this is not an easy task, because it depends a lot of the input data. The good news is that there are some
gap sequences proved to be working well in the general cases.
Shell Sequence
Donald Shell proposes a sequence that follows the formula FLOOR(N/2 ), then for N = 1000, we get the
following sequence: [500, 250, 125, 62, 31, 15, 7, 3, 1]
Pratt Sequence
Pratt proposes another sequence that’s growing with a slower pace than the Shell’s sequence. He
proposes successive numbers of the form (2^q)* (3^q) or [1, 2, 3, 4, 6, 8, 9, 12, …].
Knuth Sequence
Knuth in other hand proposes his own sequence following the formula (3^k – 1) / 2 or [1, 4, 14, 40, 121, …]
Complexity
Yet again we can’t determine the exact complexity of this algorithm, because it depends on the gap
sequence. However we may say what is the complexity of Shell sort with the sequences of Knuth, Pratt
and Donald Shell. For the Shell’s sequence the complexity is O(n*n ), while for the Pratt’s sequence it is
O(n*log (n)). The best approach is the Knuth sequence where the complexity is O(n^(3/2) ), as you can see
on the diagram bellow.
Application
Well, as insertion sort and bubble sort, Shell sort is not very effective compared to quicksort or merge
sort. The good thing is that it is quite easy to implement (not easier than insertion sort), but in general it
should be avoided for large data sets. Perhaps the main advantage of Shell sort is that the list can be
sorted for a gap greater than 1 and thus making less exchanges than insertion sort.
希尔排序( Shell Sort)的更多相关文章
- [算法] 希尔排序 Shell Sort
希尔排序(Shell Sort)是插入排序的一种,它是针对直接插入排序算法的改进.该方法又称缩小增量排序,因DL.Shell于1959年提出而得名. 希尔排序实质上是一种分组插入方法.它的基本思想是: ...
- 排序算法--希尔排序(Shell Sort)_C#程序实现
排序算法--希尔排序(Shell Sort)_C#程序实现 排序(Sort)是计算机程序设计中的一种重要操作,也是日常生活中经常遇到的问题.例如,字典中的单词是以字母的顺序排列,否则,使用起来非常困难 ...
- 希尔排序Shell sort
希尔排序Shell Sort是基于插入排序的一种改进,同样分成两部分, 第一部分,希尔排序介绍 第二部分,如何选取关键字,选取关键字是希尔排序的关键 第一块希尔排序介绍 准备待排数组[6 2 4 1 ...
- 希尔排序——Shell Sort
前言: 数据序列1: 13-17-20-42-28 利用插入排序,13-17-20-28-42. Number of swap:1;数据序列2: 13-17-20-42-14 利用插入排序,13-14 ...
- 排序之希尔排序(shell sort)
前言 本篇博客是在伍迷兄的博客基础上进行的,其博客地址点击就可以进去,里面好博客很多,我的排序算法都来自于此:一些数据结构方面的概念我就不多阐述了,伍迷兄的博客中都有详细讲解,而我写这些博客只是记录自 ...
- Python排序算法——希尔排序(Shell’s Sort)
有趣的事,Python永远不会缺席! 如需转发,请注明出处:小婷儿的python https://www.cnblogs.com/xxtalhr/p/10793487.html 一.希尔排序(Shel ...
- 数据结构 - 希尔排序(Shell's Sort) 具体解释 及 代码(C++)
数据结构 - 希尔排序(Shell's Sort) 具体解释 及 代码(C++) 本文地址: http://blog.csdn.net/caroline_wendy/article/details/2 ...
- 希尔排序(Shell)
希尔排序的实质就是分组插入排序,该方法又称缩小增量排序. 该方法的基本思想是:先将整个待排元素序列分割成若干个子序列(由相隔某个“增量”的元素组成的)分别进行直接插入排序,然后依次缩减增量再进行排序, ...
- 希尔排序(shell)理论---不含源码
| 版权声明:本文为博主原创文章,未经博主允许不得转载. 希尔排序,是一个缩小增量排序.它根据步长来进行排序,步长不同可能会产生不同的序列,但是他们的最终结果是相同的,希尔排序的官方理论难以理解, ...
随机推荐
- hihocoder 1181 欧拉路.二
传送门:欧拉路·二 #1181 : 欧拉路·二 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在上一回中小Hi和小Ho控制着主角收集了分散在各个木桥上的道具,这些道具其 ...
- UVA294DIvisors(唯一分解定理+约数个数)
题目链接 题意:输入两个整数L,U(L <= U <= 1000000000, u - l <= 10000),统计区间[L,U]的整数中哪一个的正约数最多,多个输出最小的那个 本来 ...
- 静态内部类和Lazy<T>优雅实现单例
public class Singleton { private Singleton() { } public static Singleton GetInstance() { return Conc ...
- 操作haproxy配置文件教师版
作用: 可查,可增,可删,可修改 #_*_coding:utf-8_*_ import os def file_handle(filename,backend_data,record_list=Non ...
- textView中判断文本长度,自定义表情长度为1,emoj表情长度为1,输入限制
static const int MAX_LIMIT_NUMS = 100; /**< 输入个数限制 */ // self.inputNumberTipsLabel 控制器的view上一个用 ...
- iOS Application Life Cycle 应用程序生命周期
应用程序的状态 IOS的应用程序一共有5种状态. Not running(未运行):程序未启动 Inactive(未激活):其他两个状态切换时出现的短暂状态.唯一在此状态停留时间比较长的情况是:当用户 ...
- VS2013 EMMET插件学习
在VS2013中搜索EMMET插件,安装,重启IDE,即可使用. 最简单的一个用法示例: 在编辑器里输入:ul>li*5 按快捷键:CTRL+1 即可生成如下代码: <ul> < ...
- property中的strong 、weak、copy 、assign 、retain 、unsafe_unretained 与autoreleasing区别和作用详解
iOS5中加入了新知识,就是ARC,其实我并不是很喜欢它,因为习惯了自己管理内存.但是学习还是很有必要的. 在iOS开发过程中,属性的定义往往与retain, assign, copy有关,我想大家都 ...
- POJ 3267 The Cow Lexicon
又见面了,还是原来的配方,还是熟悉的DP....直接秒了... The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submis ...
- 利用LineRenderer画出一个圆,类似于lol中的攻击范围
http://www.unity蛮牛.com/blog-5945-1409.html 本人大四狗,学unity半年有余,写此文章纯粹记录自己的心得. 废话不多说,进入主题.... 效果如图: 首先要理 ...