Repository navigation
Expand file tree
/
Copy pathsection4.html
More file actions
766 lines (375 loc) · 31.4 KB
/
Copy pathsection4.html
File metadata and controls
766 lines (375 loc) · 31.4 KB
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
<!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="../chapter1/section5.html" />
<link rel="prev" href="../chapter1/section3.html" />
</head>
<body>
<div class="book"
data-level="1.4"
data-chapter-title="算法效率衡量"
data-filepath="chapter1/section4.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 active" 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 " 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>
<h2 id="执行时间反应算法效率">执行时间反应算法效率</h2>
<p>对于同一问题,我们给出了两种解决算法,在两种算法的实现中,我们对程序执行的时间进行了测算,发现两段程序执行的时间相差悬殊(214.583347秒相比于0.182897秒),由此我们可以得出结论:<strong>实现算法程序的执行时间可以反应出算法的效率,即算法的优劣。</strong></p>
<h2 id="单靠时间值绝对可信吗?">单靠时间值绝对可信吗?</h2>
<p>假设我们将第二次尝试的算法程序运行在一台配置古老性能低下的计算机中,情况会如何?很可能运行的时间并不会比在我们的电脑中运行算法一的214.583347秒快多少。</p>
<p><strong>单纯依靠运行的时间来比较算法的优劣并不一定是客观准确的!</strong></p>
<p>程序的运行离不开计算机环境(包括硬件和操作系统),这些客观原因会影响程序运行的速度并反应在程序的执行时间上。那么如何才能客观的评判一个算法的优劣呢?</p>
<h2 id="时间复杂度与大o记法">时间复杂度与“大O记法”</h2>
<p>我们假定计算机执行算法每一个基本操作的时间是固定的一个时间单位,那么有多少个基本操作就代表会花费多少时间单位。算然对于不同的机器环境而言,确切的单位时间是不同的,但是对于算法进行多少个基本操作(即花费多少时间单位)在规模数量级上却是相同的,由此可以忽略机器环境的影响而客观的反应算法的时间效率。</p>
<p>对于算法的时间效率,我们可以用“大O记法”来表示。</p>
<p><strong>“大O记法”:对于单调的整数函数f,如果存在一个整数函数g和实常数c>0,使得对于充分大的n总有f(n)<=c*g(n),就说函数g是f的一个渐近函数(忽略常数),记为f(n)=O(g(n))。也就是说,在趋向无穷的极限意义下,函数f的增长速度受到函数g的约束,亦即函数f与函数g的特征相似。</strong></p>
<p><strong>时间复杂度:假设存在函数g,使得算法A处理规模为n的问题示例所用时间为T(n)=O(g(n)),则称O(g(n))为算法A的渐近时间复杂度,简称时间复杂度,记为T(n)</strong></p>
<h2 id="如何理解大o记法">如何理解“大O记法”</h2>
<p>对于算法进行特别具体的细致分析虽然很好,但在实践中的实际价值有限。对于算法的时间性质和空间性质,最重要的是其数量级和趋势,这些是分析算法效率的主要部分。而计量算法基本操作数量的规模函数中那些常量因子可以忽略不计。例如,可以认为3n<sup>2</sup>和100n<sup>2</sup>属于同一个量级,如果两个算法处理同样规模实例的代价分别为这两个函数,就认为它们的效率“差不多”,都为n<sup>2</sup>级。</p>
<h2 id="最坏时间复杂度">最坏时间复杂度</h2>
<p>分析算法时,存在几种可能的考虑:</p>
<ul>
<li>算法完成工作最少需要多少基本操作,即<strong>最优时间复杂度</strong></li>
<li>算法完成工作最多需要多少基本操作,即<strong>最坏时间复杂度</strong></li>
<li>算法完成工作平均需要多少基本操作,即<strong>平均时间复杂度</strong></li>
</ul>
<p>对于最优时间复杂度,其价值不大,因为它没有提供什么有用信息,其反映的只是最乐观最理想的情况,没有参考价值。</p>
<p>对于最坏时间复杂度,提供了一种保证,表明算法在此种程度的基本操作中一定能完成工作。</p>
<p>对于平均时间复杂度,是对算法的一个全面评价,因此它完整全面的反映了这个算法的性质。但另一方面,这种衡量并没有保证,不是每个计算都能在这个基本操作内完成。而且,对于平均情况的计算,也会因为应用算法的实例分布可能并不均匀而难以计算。</p>
<p><strong>因此,我们主要关注算法的最坏情况,亦即最坏时间复杂度。</strong></p>
<h2 id="时间复杂度的几条基本计算规则">时间复杂度的几条基本计算规则</h2>
<ol>
<li>基本操作,即只有常数项,认为其时间复杂度为O(1)</li>
<li>顺序结构,时间复杂度按<strong>加法</strong>进行计算</li>
<li>循环结构,时间复杂度按<strong>乘法</strong>进行计算</li>
<li>分支结构,时间复杂度<strong>取最大值</strong></li>
<li>判断一个算法的效率时,往往只需要关注操作数量的最高次项,其它次要项和常数项可以忽略</li>
<li>在没有特殊说明时,我们所分析的算法的时间复杂度都是指<strong>最坏时间复杂度</strong></li>
</ol>
</section>
</div>
</div>
</div>
<a href="../chapter1/section3.html" class="navigation navigation-prev " aria-label="Previous page: 第二次尝试"><i class="fa fa-angle-left"></i></a>
<a href="../chapter1/section5.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>