-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsection5.html
790 lines (398 loc) · 26.1 KB
/
section5.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
<!DOCTYPE HTML>
<html lang="en" >
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>希尔排序 | 数据结构与算法(Python)</title>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<meta name="description" content="">
<meta name="generator" content="GitBook 2.6.7">
<meta name="HandheldFriendly" content="true"/>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
<link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="../gitbook/style.css">
<link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
<link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
<link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
<link rel="next" href="../chapter6/section6.html" />
<link rel="prev" href="../chapter6/section4.html" />
</head>
<body>
<div class="book"
data-level="6.5"
data-chapter-title="希尔排序"
data-filepath="chapter6/section5.md"
data-basepath=".."
data-revision="Fri Mar 31 2017 18:24:30 GMT+0800 (CST)"
data-innerlanguage="">
<div class="book-summary">
<nav role="navigation">
<ul class="summary">
<li class="chapter " data-level="0" data-path="index.html">
<a href="../index.html">
<i class="fa fa-check"></i>
数据结构与算法(Python)
</a>
</li>
<li class="chapter " data-level="1" data-path="chapter1/index.html">
<a href="../chapter1/index.html">
<i class="fa fa-check"></i>
<b>1.</b>
引入概念
</a>
<ul class="articles">
<li class="chapter " data-level="1.1" data-path="chapter1/section1.html">
<a href="../chapter1/section1.html">
<i class="fa fa-check"></i>
<b>1.1.</b>
第一次尝试
</a>
</li>
<li class="chapter " data-level="1.2" data-path="chapter1/section2.html">
<a href="../chapter1/section2.html">
<i class="fa fa-check"></i>
<b>1.2.</b>
算法的提出
</a>
</li>
<li class="chapter " data-level="1.3" data-path="chapter1/section3.html">
<a href="../chapter1/section3.html">
<i class="fa fa-check"></i>
<b>1.3.</b>
第二次尝试
</a>
</li>
<li class="chapter " data-level="1.4" data-path="chapter1/section4.html">
<a href="../chapter1/section4.html">
<i class="fa fa-check"></i>
<b>1.4.</b>
算法效率衡量
</a>
</li>
<li class="chapter " data-level="1.5" data-path="chapter1/section5.html">
<a href="../chapter1/section5.html">
<i class="fa fa-check"></i>
<b>1.5.</b>
算法分析
</a>
</li>
<li class="chapter " data-level="1.6" data-path="chapter1/section6.html">
<a href="../chapter1/section6.html">
<i class="fa fa-check"></i>
<b>1.6.</b>
常见时间复杂度
</a>
</li>
<li class="chapter " data-level="1.7" data-path="chapter1/section7.html">
<a href="../chapter1/section7.html">
<i class="fa fa-check"></i>
<b>1.7.</b>
Python内置类型性能分析
</a>
</li>
<li class="chapter " data-level="1.8" data-path="chapter1/section8.html">
<a href="../chapter1/section8.html">
<i class="fa fa-check"></i>
<b>1.8.</b>
数据结构
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="2" data-path="chapter2/index.html">
<a href="../chapter2/index.html">
<i class="fa fa-check"></i>
<b>2.</b>
顺序表
</a>
<ul class="articles">
<li class="chapter " data-level="2.1" data-path="chapter2/section1.html">
<a href="../chapter2/section1.html">
<i class="fa fa-check"></i>
<b>2.1.</b>
顺序表的形式
</a>
</li>
<li class="chapter " data-level="2.2" data-path="chapter2/section2.html">
<a href="../chapter2/section2.html">
<i class="fa fa-check"></i>
<b>2.2.</b>
顺序表的结构与实现
</a>
</li>
<li class="chapter " data-level="2.3" data-path="chapter2/section3.html">
<a href="../chapter2/section3.html">
<i class="fa fa-check"></i>
<b>2.3.</b>
顺序表的操作
</a>
</li>
<li class="chapter " data-level="2.4" data-path="chapter2/section4.html">
<a href="../chapter2/section4.html">
<i class="fa fa-check"></i>
<b>2.4.</b>
Python中的顺序表
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="3" data-path="chapter3/index.html">
<a href="../chapter3/index.html">
<i class="fa fa-check"></i>
<b>3.</b>
链表
</a>
<ul class="articles">
<li class="chapter " data-level="3.1" data-path="chapter3/section1.html">
<a href="../chapter3/section1.html">
<i class="fa fa-check"></i>
<b>3.1.</b>
单向链表
</a>
</li>
<li class="chapter " data-level="3.2" data-path="chapter3/section2.html">
<a href="../chapter3/section2.html">
<i class="fa fa-check"></i>
<b>3.2.</b>
单项循环链表
</a>
</li>
<li class="chapter " data-level="3.3" data-path="chapter3/section3.html">
<a href="../chapter3/section3.html">
<i class="fa fa-check"></i>
<b>3.3.</b>
双向链表
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="4" data-path="chapter4/index.html">
<a href="../chapter4/index.html">
<i class="fa fa-check"></i>
<b>4.</b>
栈
</a>
<ul class="articles">
<li class="chapter " data-level="4.1" data-path="chapter4/section1.html">
<a href="../chapter4/section1.html">
<i class="fa fa-check"></i>
<b>4.1.</b>
栈结构实现
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="5" data-path="chapter5/index.html">
<a href="../chapter5/index.html">
<i class="fa fa-check"></i>
<b>5.</b>
队列
</a>
<ul class="articles">
<li class="chapter " data-level="5.1" data-path="chapter5/section1.html">
<a href="../chapter5/section1.html">
<i class="fa fa-check"></i>
<b>5.1.</b>
队列的实现
</a>
</li>
<li class="chapter " data-level="5.2" data-path="chapter5/section3.html">
<a href="../chapter5/section3.html">
<i class="fa fa-check"></i>
<b>5.2.</b>
双端队列
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="6" data-path="chapter6/index.html">
<a href="../chapter6/index.html">
<i class="fa fa-check"></i>
<b>6.</b>
排序与搜索
</a>
<ul class="articles">
<li class="chapter " data-level="6.1" data-path="chapter6/section1.html">
<a href="../chapter6/section1.html">
<i class="fa fa-check"></i>
<b>6.1.</b>
冒泡排序
</a>
</li>
<li class="chapter " data-level="6.2" data-path="chapter6/section2.html">
<a href="../chapter6/section2.html">
<i class="fa fa-check"></i>
<b>6.2.</b>
选择排序
</a>
</li>
<li class="chapter " data-level="6.3" data-path="chapter6/section3.html">
<a href="../chapter6/section3.html">
<i class="fa fa-check"></i>
<b>6.3.</b>
插入排序
</a>
</li>
<li class="chapter " data-level="6.4" data-path="chapter6/section4.html">
<a href="../chapter6/section4.html">
<i class="fa fa-check"></i>
<b>6.4.</b>
快速排序
</a>
</li>
<li class="chapter active" data-level="6.5" data-path="chapter6/section5.html">
<a href="../chapter6/section5.html">
<i class="fa fa-check"></i>
<b>6.5.</b>
希尔排序
</a>
</li>
<li class="chapter " data-level="6.6" data-path="chapter6/section6.html">
<a href="../chapter6/section6.html">
<i class="fa fa-check"></i>
<b>6.6.</b>
归并排序
</a>
</li>
<li class="chapter " data-level="6.7" data-path="chapter6/section7.html">
<a href="../chapter6/section7.html">
<i class="fa fa-check"></i>
<b>6.7.</b>
常见排序算法效率比较
</a>
</li>
<li class="chapter " data-level="6.8" data-path="chapter6/section8.html">
<a href="../chapter6/section8.html">
<i class="fa fa-check"></i>
<b>6.8.</b>
搜索
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="7" data-path="chapter7/index.html">
<a href="../chapter7/index.html">
<i class="fa fa-check"></i>
<b>7.</b>
树与树算法
</a>
<ul class="articles">
<li class="chapter " data-level="7.1" data-path="chapter7/section1.html">
<a href="../chapter7/section1.html">
<i class="fa fa-check"></i>
<b>7.1.</b>
二叉树
</a>
</li>
<li class="chapter " data-level="7.2" data-path="chapter7/section2.html">
<a href="../chapter7/section2.html">
<i class="fa fa-check"></i>
<b>7.2.</b>
二叉树的遍历
</a>
</li>
</ul>
</li>
<li class="divider"></li>
<li>
<a href="https://www.gitbook.com" target="blank" class="gitbook-link">
Published with GitBook
</a>
</li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<!-- Actions Left -->
<!-- Title -->
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i>
<a href="../" >数据结构与算法(Python)</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<section class="normal" id="section-">
<h1 id="希尔排序">希尔排序</h1>
<p>希尔排序(Shell Sort)是插入排序的一种。也称缩小增量排序,是直接插入排序算法的一种更高效的改进版本。希尔排序是非稳定排序算法。该方法因DL.Shell于1959年提出而得名。 希尔排序是把记录按下标的一定增量分组,对每组使用直接插入排序算法排序;随着增量逐渐减少,每组包含的关键词越来越多,当增量减至1时,整个文件恰被分成一组,算法便终止。</p>
<h2 id="希尔排序过程">希尔排序过程</h2>
<p>希尔排序的基本思想是:将数组列在一个表中并对列分别进行插入排序,重复这过程,不过每次用更长的列(步长更长了,列数更少了)来进行。最后整个表就只有一列了。将数组转换至表是为了更好地理解这算法,算法本身还是使用数组进行排序。</p>
<p>例如,假设有这样一组数[ 13 14 94 33 82 25 59 94 65 23 45 27 73 25 39 10 ],如果我们以步长为5开始进行排序,我们可以通过将这列表放在有5列的表中来更好地描述算法,这样他们就应该看起来是这样(竖着的元素是步长组成):</p>
<pre><code>13 14 94 33 82
25 59 94 65 23
45 27 73 25 39
10
</code></pre><p>然后我们对每列进行排序:</p>
<pre><code>10 14 73 25 23
13 27 94 33 39
25 59 94 65 82
45
</code></pre><p>将上述四行数字,依序接在一起时我们得到:[ 10 14 73 25 23 13 27 94 33 39 25 59 94 65 82 45 ]。这时10已经移至正确位置了,然后再以3为步长进行排序:</p>
<pre><code>10 14 73
25 23 13
27 94 33
39 25 59
94 65 82
45
</code></pre><p>排序之后变为:</p>
<pre><code>10 14 13
25 23 33
27 25 59
39 65 73
45 94 82
94
</code></pre><p>最后以1步长进行排序(此时就是简单的插入排序了)</p>
<h2 id="希尔排序的分析">希尔排序的分析</h2>
<p><img src="../images/shellsort.png" alt="shellsort"></p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">shell_sort</span><span class="hljs-params">(alist)</span>:</span>
n = len(alist)
<span class="hljs-comment"># 初始步长</span>
gap = n / <span class="hljs-number">2</span>
<span class="hljs-keyword">while</span> gap > <span class="hljs-number">0</span>:
<span class="hljs-comment"># 按步长进行插入排序</span>
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(gap, n):
j = i
<span class="hljs-comment"># 插入排序</span>
<span class="hljs-keyword">while</span> j>=gap <span class="hljs-keyword">and</span> alist[j-gap] > alist[j]:
alist[j-gap], alist[j] = alist[j], alist[j-gap]
j -= gap
<span class="hljs-comment"># 得到新的步长</span>
gap = gap / <span class="hljs-number">2</span>
alist = [<span class="hljs-number">54</span>,<span class="hljs-number">26</span>,<span class="hljs-number">93</span>,<span class="hljs-number">17</span>,<span class="hljs-number">77</span>,<span class="hljs-number">31</span>,<span class="hljs-number">44</span>,<span class="hljs-number">55</span>,<span class="hljs-number">20</span>]
shell_sort(alist)
print(alist)
</code></pre>
<h2 id="时间复杂度">时间复杂度</h2>
<ul>
<li>最优时间复杂度:根据步长序列的不同而不同</li>
<li>最坏时间复杂度:O(n<sup>2</sup>)</li>
<li>稳定想:不稳定</li>
</ul>
<h2 id="希尔排序演示">希尔排序演示</h2>
<p><img src="../images/shellsort.gif" alt="shellsort"></p>
</section>
</div>
</div>
</div>
<a href="../chapter6/section4.html" class="navigation navigation-prev " aria-label="Previous page: 快速排序"><i class="fa fa-angle-left"></i></a>
<a href="../chapter6/section6.html" class="navigation navigation-next " aria-label="Next page: 归并排序"><i class="fa fa-angle-right"></i></a>
</div>
</div>
<script src="../gitbook/app.js"></script>
<script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
<script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
<script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
<script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
<script>
require(["gitbook"], function(gitbook) {
var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
gitbook.start(config);
});
</script>
</body>
</html>