学希尔排序的时候,觉得有序性保持的性质十分神奇,但哪里都找不到数学证明。最后在Donald E. Knuth的The Art of Computer Programming中找到了(显然我没有读过这套书),现摘录并整理之。

Theorem K. If a k-ordered file is h-sorted, it remains k-ordered.

Thus a file that is first 7-sorted, then 5-sorted, becomes both 7-ordered and 5-ordered. And if we 3-sort it, the result is ordered by 7s, 5s, and 3s. Examples of this remarkable property can be seen in Table 4 on page 85.

Proof. Exercise 20 shows that Theorem K is a consequence of the following fact:

Lemma L. Let m, n, r be nonnegative integers, and let (x1 , ... , xm+r) and (y1 , ... , yn+r) be any sequences of numbers such that

                y1 ≤ xm+1 ,  y2 ≤ xm+2 ,  ... ,  y≤ xm+r                (7)

If the x's and y's are sorted independently, so that x1 ≤ ... ≤ xm+r and y1 ≤ ... ≤ yn+r , the relations (7) will still be valid.

Proof. All but m of the x’s are known to be dominate (that is, to be greater than or equal to) some y, where distinct x’s dominate distinct y’s. Let 1 ≤ j ≤ r. Since xm+j after sorting dominates m + j of the x’s, it dominates at least j of the y’s; therefore it dominates the smallest j of the y’s; hence xm+j ≥ yj after sorting.

Exercise 20. Show that Theorem K follows from Lemma L.

Solution. (This is much harder to write down than to understand.) Assume that a k-ordered file R1 , ... , RN has been h-sorted, and let 1 ≤ i ≤ N - k; we want to show that Ki ≤ Ki+k . Find u, v such that i ≡ u and i + k ≡ v (modulo h), 1 ≤ u, v ≤ h; and apply Lemma L with xj = Kv+(j-1)h , yj = Ku+(j-1)h . Then the first r elements Ku , Ku+h , ... , Ku+(r-1)h of the y’s are respectively  the last r elememts Ku+k , Ku+k+h , ... , Ku+k+(r-1)h of the x’s, where r is the greatest integer such that u + k + (r - 1)h ≤ N.

定理  如果一个序列是k有序的,在h排序后它保持k有序。

证明  引理:设m, n, r为非负整数,(x1 , ... , xm+r)与(y1 , ... , yn+r)为满足以下性质的序列:

y1 ≤ xm+1 ,  y2 ≤ xm+2 ,  ... ,  y≤ xm+r

那么如果xy被分别排序,使x1 ≤ ... ≤ xm+ry1 ≤ ... ≤ yn+r ,则以上关系仍成立。

m个以外所有x都大于等于某个y,其中不同的x大于等于不同的y。设1 ≤ j ≤ r。由于排序后xm+j大于等于x中的m + j个,它至少大于y中的j个。因此它大于等于y中最小的j个,故排序后xm+j ≥ yj,引理得证。

假设k有序的序列R1 , ... , RNh排序,设1 ≤ i ≤ N - k,即证Ki ≤ Ki+k。存在u、v使i ≡ ui + k ≡ v (mod h),其中1 ≤ u,v ≤ h。将xj = Kv+(j-1)h ,yj = Ku+(j-1)h代入引理。则y中的前r个元素Ku , Ku+h , ... , Ku+(r-1)h分别小于等于x中的后r个元素Ku+k , Ku+k+h , ... , Ku+k+(r-1)h ,其中r是满足u + k + (r - 1)h ≤ N的最大整数。证毕。

因此一开始7有序的序列在5排序后同时成为7有序与5有序的。如果再3排序,结果是7、5与3有序的。

希尔排序的正确性 (Correctness of ShellSort)的更多相关文章

  1. Java基础知识强化57:经典排序之希尔排序(ShellSort)

    1. 希尔排序的原理: 希尔排序(Shell Sort)是插入排序的一种.也称缩小增量排序,是直接插入排序算法的一种更高效的改进版本.希尔排序是非稳定排序算法.该方法因DL.Shell于1959年提出 ...

  2. C#算法基础之希尔排序

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  3. 希尔排序(shell‘ sort)

    希尔排序是1959 年由D.L.Shell 提出来的,相对直接排序有较大的改进.希尔排序又叫缩小增量排序 基本思想: 先将整个待排序的记录序列分割成为若干子序列分别进行直接插入排序,待整个序列中的记录 ...

  4. java排序算法之希尔排序

    希尔排序是冲破二次时间屏障的第一批算法之一. 它是通过比较相距一定间隔的元素来工作,各趟比较所用的距离随着算法的进行而减小,直到最后一趟(比较相邻元素)为止.因此希尔排序也叫缩减增量排序. 希尔排序使 ...

  5. C数据结构排序算法——希尔排序法用法总结(转http://www.cnblogs.com/skywang12345/p/3597597.html)

    希尔排序介绍 希尔排序(Shell Sort)是插入排序的一种,它是针对直接插入排序算法的改进.该方法又称缩小增量排序,因DL.Shell于1959年提出而得名. 希尔排序实质上是一种分组插入方法.它 ...

  6. 希尔排序(Shellsort)

    首先,Shell是发明这个算法的人名,不是这个算法的思想或者特点. 希尔排序,也称为增量递减排序.基本思路,是把原来的序列,等效视为一个矩阵的形式.矩阵的列数,也称为宽度或者增量,记为w. 假设数组A ...

  7. C#数据结构与算法系列(二十一):希尔排序算法(ShellSort)

    1.介绍 希尔排序是希尔(Donald Shell)于1959年提出的一种排序算法.希尔排序也是一种插入排序,它是简单插入排序经过改进之后的一个更高效的版本,也称为缩小增量排序. 2.基本思想 希尔排 ...

  8. 排序--ShellSort 希尔排序

    希尔排序 no 实现 希尔排序其实就是插入排序.只不过希尔排序在比较的元素的间隔不是1. 我们知道插入排序 都是 一个一个和之前的元素比较.发现比之前元素小就交换位置.但是希尔排序可能是和前第n个元素 ...

  9. uva 10152 ShellSort 龟壳排序(希尔排序?)

    今天状态总是很糟,这种题目卡了一天... 是不是休息时间太少了,头脑迟钝了... 名字叫希尔排序,我还以为跟它有关,还搜索了下资料. 只要找到trick就会发现是很水的题目.只要对比下就能找到哪些是移 ...

随机推荐

  1. String常量池和intern方法

    String s1 = "Hello"; String s2 = "Hello"; String s3 = "Hel" + "lo ...

  2. WPF ContextMenu+VisualTreeHelper实现删除控件操作

    <UserControl  MouseRightButtonDown="UserControl_MouseRightButtonDown" >    <UserC ...

  3. Java下载文件方法

    public static void download(String path, HttpServletResponse response) { try { // path是指欲下载的文件的路径. F ...

  4. Mybatis学习笔记之---环境搭建与入门

    Mybatis环境搭建与入门 (一)环境搭建 (1)第一步:创建maven工程并导入jar包 <dependencies> <dependency> <groupId&g ...

  5. Java——数据结构(链表)

    链表,可扩展长度,泛型. public class Link { Node header = null; //头结点 int length;//当前链表长度 class Node { Node nex ...

  6. SonarQube部署及代码质量扫描入门教程

    一.前言 1.本文主要内容 CentOS7下SonarQube部署 Maven扫描Java项目并将扫描结果提交到SonarQube Server SonarQube扫描报表介绍 2.环境信息 工具/环 ...

  7. 【KakaJSON手册】04_JSON转Model_04_值过滤

    在KakaJSON手册的第2篇文章中提过:由于JSON格式能表达的数据类型是比较有限的,所以服务器返回的JSON数据有时无法自动转换成客户端想要的数据类型 比如客户端想要的是Date类型,服务器返回的 ...

  8. Kubernetes-保障集群内节点和网络安全

    13.1.在pod中使用宿主节点的Linux命名空间 13.1.1.在pod中使用宿主节点的网络命名空间 在pod的yaml文件中就设置spec.hostNetwork: true 这个时候pod使用 ...

  9. Linux - 通过expect工具实现脚本的自动交互

    目录 1 安装expect工具 2 expect的常用命令 3 作用原理简介 3.1 示例脚本 3.2 脚本功能解读 4 其他脚本使用示例 4.1 直接通过expect执行多条命令 4.2 通过she ...

  10. [JavaScript] 节流(throttle)-防抖(debounce) 不懵圈指北

    网易云课堂 > 微专业 > 前端高级开发工程师 01.前端高级-JavaScript进阶 > 3.函数式编程 Underscore源码分析 > 3.4.3 throttle 与 ...