-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathalgoI_week2_2.html
1089 lines (1014 loc) · 71 KB
/
algoI_week2_2.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
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="zh-Hant"
>
<head>
<title>[Algorithms I] Week 2-2 Elementary Sorts - mx's blog</title>
<!-- Using the latest rendering mode for IE -->
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="theme-color" content="#6b5594">
<meta name="msapplication-navbutton-color" content="#6b5594">
<meta name="apple-mobile-web-app-status-bar-style" content="#6b5594">
<link rel="manifest" href="/manifest.json">
<link rel="canonical" href="https://x-wei.github.io/algoI_week2_2.html">
<meta name="author" content="mx" />
<meta name="keywords" content="algorithm" />
<meta name="description" content="1. Introduction rearanging array of size N into ascending order test client code: Insertion.sort(a); sort any datatype callback callback = reference to executable code i.e. passing functions as argument to sort() method sort() function calls object's compareTo() method → implement the Comparable interface: public class XX implements Comparable ..." />
<meta property="og:site_name" content="mx's blog" />
<meta property="og:type" content="article"/>
<meta property="og:title" content="[Algorithms I] Week 2-2 Elementary Sorts"/>
<meta property="og:url" content="https://x-wei.github.io/algoI_week2_2.html"/>
<meta property="og:description" content="1. Introduction rearanging array of size N into ascending order test client code: Insertion.sort(a); sort any datatype callback callback = reference to executable code i.e. passing functions as argument to sort() method sort() function calls object's compareTo() method → implement the Comparable interface: public class XX implements Comparable ..."/>
<meta property="article:published_time" content="2015-08-18" />
<meta property="article:section" content="notes" />
<meta property="article:tag" content="algorithm" />
<meta property="article:author" content="mx" />
<meta property="og:image"
content="https://x-wei.github.io/algoI_week2_2.png"/>
<!-- Bootstrap -->
<link href="https://x-wei.github.io/theme/css/bootstrap.min.css" rel="stylesheet">
<link href="https://x-wei.github.io/theme/css/font-awesome.min.css" rel="stylesheet">
<link href="https://x-wei.github.io/theme/css/pygments/manni.css" rel="stylesheet">
<link href="https://x-wei.github.io/theme/tipuesearch/tipuesearch.css" rel="stylesheet">
<link rel="stylesheet" href="https://x-wei.github.io/theme/css/style.css" type="text/css"/>
<link href="https://x-wei.github.io/feeds/atom.xml" type="application/atom+xml" rel="alternate"
title="mx's blog ATOM Feed"/>
<link href="https://x-wei.github.io/theme/css/material.min.css" rel="stylesheet">
<link href="https://x-wei.github.io/theme/css/ripples.css" rel="stylesheet">
</head>
<body>
<div style="display:none" id="title">[Algorithms I] Week 2-2 Elementary Sorts - mx's blog</div>
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
<span class="sr-only">切换导航</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a href="https://x-wei.github.io/" class="navbar-brand">
mx's blog </a>
</div>
<div class="collapse navbar-collapse navbar-ex1-collapse">
<ul class="nav navbar-nav">
<li class="dropdown hidden-md hidden-lg hidden-xl">
<a class="dropdown-toggle" data-toggle="dropdown" href="javascript:void(0)">
<i class="fa fa-user"></i><span class="caret"></span>
</a>
<ul class="dropdown-menu">
</ul>
</li>
<ul class="nav navbar-nav hidden-xs hidden-sm">
</ul>
</ul>
<ul class="nav navbar-nav hidden-md hidden-lg hidden-xl">
<li class="dropdown hidden-md hidden-lg hidden-xl">
<a class="dropdown-toggle" data-toggle="dropdown" href="javascript:void(0)">
<i class="fa fa-folder-o"></i><span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li >
<a href="https://x-wei.github.io/category/misc.html"><i class="fa fa-folder-o"></i> Misc</a>
</li>
<li >
<a href="https://x-wei.github.io/category/music.html"><i class="fa fa-folder-o"></i> Music</a>
</li>
<li class="active">
<a href="https://x-wei.github.io/category/notes.html"><i class="fa fa-folder-o"></i> Notes</a>
</li>
<li >
<a href="https://x-wei.github.io/category/soft.html"><i class="fa fa-folder-o"></i> Soft</a>
</li>
<li >
<a href="https://x-wei.github.io/category/tech.html"><i class="fa fa-folder-o"></i> Tech</a>
</li>
</ul>
</li>
</ul>
<ul class="nav navbar-nav hidden-xs hidden-sm">
<li >
<a href="https://x-wei.github.io/category/misc.html"><i class="fa fa-folder-o"></i> Misc</a>
</li>
<li >
<a href="https://x-wei.github.io/category/music.html"><i class="fa fa-folder-o"></i> Music</a>
</li>
<li class="active">
<a href="https://x-wei.github.io/category/notes.html"><i class="fa fa-folder-o"></i> Notes</a>
</li>
<li >
<a href="https://x-wei.github.io/category/soft.html"><i class="fa fa-folder-o"></i> Soft</a>
</li>
<li >
<a href="https://x-wei.github.io/category/tech.html"><i class="fa fa-folder-o"></i> Tech</a>
</li>
</ul>
<ul class="nav navbar-nav navbar-right hidden-sm hidden-md hidden-lg hidden-xl">
<li class="dropdown hidden-md hidden-lg hidden-xl">
<a class="dropdown-toggle" data-toggle="dropdown" href="javascript:void(0)">
<i class="fa fa-search"></i><span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li><span>
<form class="navbar-search" action="/search.html">
<input type="text" class="search-query form-control col-lg-16" placeholder="Search" name="q" id="tipue_search_input" required>
</form></span>
</li>
</ul>
</li>
</ul>
<ul class="nav navbar-right navbar-form hidden-xs">
<li><span>
<form class="navbar-search" action="/search.html">
<input type="text" class="search-query form-control col-lg-16" placeholder="查找" name="q" id="tipue_search_input" required>
</form></span>
</li>
</ul>
<ul class="nav navbar-nav navbar-right hidden-xs">
<li><a href="https://x-wei.github.io/archives.html"><i class="fa fa-th-list"></i><span class="icon-label">Archive</span></a></li>
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
</div> <!-- /.navbar -->
<!-- Banner -->
<!-- End Banner -->
<div class="container" style="min-height: 100%;height: auto !important;height: 100%;">
<div class="row" style="padding-bottom:80px;padding-top:80px;">
<div class="col-xl-21 col-lg-20 col-md-18">
<div id="loading-block">
<ol class="breadcrumb">
<li><a href="https://x-wei.github.io/" title="mx's blog"><i class="fa fa-home fa-lg"></i></a></li>
<li><a href="https://x-wei.github.io/category/notes.html" title="notes">notes</a></li>
<li class="active">[Algorithms I] Week 2-2 Elementary Sorts</li>
</ol>
<section id="content" class="article-content">
<article>
<header class="page-header jumbotron jumbotron-primary panel-primary" id="article-header">
<div class="panel-heading">
<h1>
[Algorithms I] Week 2-2 Elementary Sorts
<a href="https://x-wei.github.io/algoI_week2_2.html"
rel="bookmark"
class="btn btn-primary btn-lg"
title="到 [Algorithms I] Week 2-2 Elementary Sorts 的永久链接">
<i class="mdi-action-launch"></i>
</a>
</h1>
</div>
<div class="panel-body">
<div class="post-info">
<span class="published">
<time datetime="2015-08-18T00:00:00+02:00"><i class="fa fa-calendar"></i> Tue, 18 Aug 2015</time>
</span>
<span class="btn-group">
<a href="https://x-wei.github.io/tag/algorithm.html" class="btn btn-primary btn-xs"><i class="fa fa-tag"></i> algorithm</a>
</span>
<span class="label label-default">Series</span>
Part 5 of «Algorithms Princeton MOOC I»
</div><!-- /.post-info --> </div>
</header>
<div class="entry-content jumbotron" id="article-content">
<div class="panel panel-default">
<div class="panel-heading">
目录
</div>
<div class="panel-boy">
<div id="toc"><ul><li><a class="toc-href" href="#1-introduction" title="1. Introduction">1. Introduction</a><ul><li><a class="toc-href" href="#callback" title="callback">callback</a></li><li><a class="toc-href" href="#helper-functions" title="helper functions">helper functions</a></li></ul></li><li><a class="toc-href" href="#2-selection-sort_1" title="2. Selection Sort">2. Selection Sort</a><ul><li><a class="toc-href" href="#invariants" title="invariants">invariants</a></li><li><a class="toc-href" href="#implementation" title="implementation">implementation</a></li><li><a class="toc-href" href="#analysis" title="analysis">analysis</a></li></ul></li><li><a class="toc-href" href="#3-insertion-sort_1" title="3. Insertion sort">3. Insertion sort</a><ul><li><a class="toc-href" href="#invariants_1" title="invariants">invariants</a></li><li><a class="toc-href" href="#implementation_1" title="implementation">implementation</a></li><li><a class="toc-href" href="#analysis_1" title="analysis">analysis</a><ul><li><a class="toc-href" href="#best-case-and-worst-case" title="best case and worst case">best case and worst case</a></li><li><a class="toc-href" href="#partially-sorted-arrays" title="partially sorted arrays">partially sorted arrays</a></li></ul></li></ul></li><li><a class="toc-href" href="#4-shell-sort_2" title="4. Shell Sort">4. Shell Sort</a><ul><li><a class="toc-href" href="#implementation_2" title="implementation">implementation</a></li><li><a class="toc-href" href="#analysis_2" title="analysis">analysis</a></li></ul></li><li><a class="toc-href" href="#of-compares-cte-n-of-h-used_1" title="of compares < Cte * N * (# of h used )">of compares < Cte * N * (# of h used )</a></li><li><a class="toc-href" href="#5-shuffling" title="5. shuffling">5. shuffling</a><ul><li><a class="toc-href" href="#shuffle-array-using-sort" title="shuffle array using sort">shuffle array using sort</a></li><li><a class="toc-href" href="#knuth-shuffle" title="Knuth shuffle">Knuth shuffle</a></li><li><a class="toc-href" href="#example-online-poker" title="example: online poker">example: online poker</a></li></ul></li><li><a class="toc-href" href="#6-convex-hull_1" title="6. Convex Hull">6. Convex Hull</a><ul><li><a class="toc-href" href="#convex-hull" title="convex hull">convex hull</a></li><li><a class="toc-href" href="#algo" title="Algo">Algo</a></li><li><a class="toc-href" href="#implementation_3" title="implementation">implementation</a></li></ul></li></ul></div>
</div>
</div>
<h1 id="1-introduction">1. Introduction</h1>
<p>rearanging array of size N into ascending order <br/>
test client code: <code>Insertion.sort(a);</code> </p>
<p>sort <em>any</em> datatype </p>
<h3 id="callback">callback</h3>
<p><em>callback = reference to executable code</em> <br/>
i.e. passing functions as argument to sort() method<br/>
sort() function calls object's <code>compareTo()</code> method <br/>
<img alt="" class="img-responsive" src="../images/algoI_week2_2/pasted_image.png"/> <br/>
→ implement the <code>Comparable</code> interface: </p>
<div class="highlight"><pre><span class="code-line"><span></span><span class="err"> public class XX implements Comparable<XX>{</span></span>
<span class="code-line"><span class="err"> ...</span></span>
<span class="code-line"><span class="err"> }</span></span>
</pre></div>
<p>the interface: </p>
<div class="highlight"><pre><span class="code-line"><span></span><span class="err"> public interface Comparable<Item>{</span></span>
<span class="code-line"><span class="err"> public int compareTo(Item that);</span></span>
<span class="code-line"><span class="err"> }</span></span>
</pre></div>
<p>compareTo():<br/>
return -1 (if this<that)/+1/0;
needs a <em>total order.</em>
→ in the sort() implementation:<br/>
has not dependencies on type of data. </p>
<div class="highlight"><pre><span class="code-line"><span></span><span class="w"> </span><span class="k">public</span><span class="w"> </span><span class="k">static</span><span class="w"> </span><span class="n">void</span><span class="w"> </span><span class="n">sort</span><span class="p">(</span><span class="n">Comparable</span><span class="err">[]</span><span class="w"> </span><span class="n">a</span><span class="p">)</span><span class="err">{</span><span class="w"></span></span>
<span class="code-line"><span class="w"> </span><span class="k">if</span><span class="p">(</span><span class="n">a</span><span class="o">[</span><span class="n">i</span><span class="o">]</span><span class="p">.</span><span class="n">compareTo</span><span class="p">(</span><span class="n">a</span><span class="o">[</span><span class="n">j</span><span class="o">]</span><span class="p">)</span><span class="o">></span><span class="mi">0</span><span class="p">)...</span><span class="w"></span></span>
<span class="code-line"><span class="w"> </span><span class="err">}</span><span class="w"></span></span>
</pre></div>
<h3 id="helper-functions">helper functions</h3>
<ul>
<li>
<p>less</p>
<p>private static boolean less(Comparable v, Comparable u){
returnv.compareTo(u)<0;
}</p>
</li>
<li>
<p>exch</p>
<p>private void exch(Comparable[] a, int i, int j) {
Comparable swap = a[i];
a[i] = a[j];
a[j] = swap;
}</p>
</li>
<li>
<p>isSorted</p>
</li>
</ul>
<p>test if sorted
if algo passes the test using only less ant swap, then it's correct. </p>
<h1 id="2-selection-sort_1">2. Selection Sort</h1>
<p>Idea: each time<em>: find the minimum from the remaining items.</em>
<strong>a[min] is the smallest element to right of a[i] ⇒ swap a[i] and a[min]</strong> (elements to left of i are sorted) <br/>
<img alt="" class="img-responsive" src="../images/algoI_week2_2/pasted_image002.png"/> </p>
<h2 id="invariants">invariants</h2>
<ul>
<li>entries to the left of i are in sorted order, and are fixed (<em>in final position</em>) ever since</li>
<li>no entry to the right of i is smaller than any entry to the left of i</li>
</ul>
<h2 id="implementation">implementation</h2>
<div class="highlight"><pre><span class="code-line"><span></span><span class="k">public</span><span class="w"> </span><span class="k">class</span><span class="w"> </span><span class="n">SelectionSort</span><span class="w"> </span><span class="n">extends</span><span class="w"> </span><span class="n">AbstractSorting</span><span class="err">{</span><span class="w"></span></span>
<span class="code-line"><span class="o">//</span><span class="p">...</span><span class="w"></span></span>
<span class="code-line"><span class="k">public</span><span class="w"> </span><span class="k">static</span><span class="w"> </span><span class="n">void</span><span class="w"> </span><span class="n">sort</span><span class="p">(</span><span class="n">Comparable</span><span class="err">[]</span><span class="w"> </span><span class="n">a</span><span class="p">)</span><span class="err">{</span><span class="w"></span></span>
<span class="code-line"><span class="w"> </span><span class="k">for</span><span class="p">(</span><span class="nc">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o"><</span><span class="n">a</span><span class="p">.</span><span class="n">length</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="err">{</span><span class="w"></span></span>
<span class="code-line"><span class="w"> </span><span class="nc">int</span><span class="w"> </span><span class="nf">min</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">i</span><span class="p">;</span><span class="w"></span></span>
<span class="code-line"><span class="w"> </span><span class="k">for</span><span class="p">(</span><span class="nc">int</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">;</span><span class="w"> </span><span class="n">j</span><span class="o"><</span><span class="n">a</span><span class="p">.</span><span class="n">length</span><span class="p">;</span><span class="w"> </span><span class="n">j</span><span class="o">++</span><span class="p">)</span><span class="w"></span></span>
<span class="code-line"><span class="w"> </span><span class="k">if</span><span class="p">(</span><span class="k">less</span><span class="p">(</span><span class="n">a</span><span class="o">[</span><span class="n">j</span><span class="o">]</span><span class="p">,</span><span class="n">a</span><span class="o">[</span><span class="n">min</span><span class="o">]</span><span class="p">))</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="nf">min</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">j</span><span class="p">;</span><span class="w"></span></span>
<span class="code-line"><span class="w"> </span><span class="n">exch</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="nf">min</span><span class="p">,</span><span class="n">i</span><span class="p">);</span><span class="w"></span></span>
<span class="code-line"><span class="w"> </span><span class="err">}</span><span class="w"></span></span>
<span class="code-line"><span class="err">}</span><span class="w"></span></span>
<span class="code-line"><span class="err">}</span><span class="w"></span></span>
</pre></div>
<h2 id="analysis">analysis</h2>
<p><strong>proposition:</strong>
selection sort uses <em>N-1 + N-2 + ... + 1 = ~N^2/2</em> compares, and <em>N</em> exchanges.
→ quadratic time</p>
<p><img alt="" class="img-responsive" src="../images/algoI_week2_2/pasted_image003.png"/></p>
<ul>
<li><strong>insensitive</strong> to input: quadratic time <em>even if input is already sorted.</em> </li>
<li>data movement is <em>minimum</em>: linear time of exchanges (every exchange puts an item to its final position)</li>
</ul>
<h1 id="3-insertion-sort_1">3. Insertion sort</h1>
<p>quite different performance characteritics than selection sort. </p>
<p>Idea: <strong>In iteration i: move all entries larger than a[i] to its left.</strong> <br/>
<img alt="" class="img-responsive" src="../images/algoI_week2_2/pasted_image004.png"/> </p>
<h2 id="invariants_1">invariants</h2>
<ul>
<li>entries to the left of i are in ascending order (but <em>not</em> in final position)</li>
<li>entries to the right of i are not yet been seen </li>
</ul>
<p><img alt="" class="img-responsive" src="../images/algoI_week2_2/pasted_image005.png"/> </p>
<h2 id="implementation_1">implementation</h2>
<div class="highlight"><pre><span class="code-line"><span></span><span class="n">publc</span><span class="w"> </span><span class="k">class</span><span class="w"> </span><span class="n">InsertionSorting</span><span class="w"> </span><span class="n">extends</span><span class="w"> </span><span class="n">AbstractSorting</span><span class="err">{</span><span class="w"></span></span>
<span class="code-line"><span class="w"> </span><span class="k">public</span><span class="w"> </span><span class="k">static</span><span class="w"> </span><span class="n">void</span><span class="w"> </span><span class="n">sort</span><span class="p">(</span><span class="n">Comparable</span><span class="err">[]</span><span class="w"> </span><span class="n">a</span><span class="p">)</span><span class="err">{</span><span class="w"></span></span>
<span class="code-line"><span class="w"> </span><span class="k">for</span><span class="p">(</span><span class="nc">int</span><span class="w"> </span><span class="n">i</span><span class="o">=</span><span class="mi">1</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o"><</span><span class="n">a</span><span class="p">.</span><span class="n">length</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"></span></span>
<span class="code-line"><span class="w"> </span><span class="k">for</span><span class="p">(</span><span class="nc">int</span><span class="w"> </span><span class="n">j</span><span class="o">=</span><span class="n">i</span><span class="p">;</span><span class="w"> </span><span class="n">j</span><span class="o">></span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">j</span><span class="c1">--){</span></span>
<span class="code-line"><span class="w"> </span><span class="k">if</span><span class="p">(</span><span class="k">less</span><span class="p">(</span><span class="n">a</span><span class="o">[</span><span class="n">j</span><span class="o">]</span><span class="p">,</span><span class="n">a</span><span class="o">[</span><span class="n">j-1</span><span class="o">]</span><span class="p">))</span><span class="w"></span></span>
<span class="code-line"><span class="w"> </span><span class="n">exch</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="n">j</span><span class="p">,</span><span class="n">j</span><span class="o">-</span><span class="mi">1</span><span class="p">);</span><span class="w"></span></span>
<span class="code-line"><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="k">break</span><span class="p">;</span><span class="w"></span></span>
<span class="code-line"><span class="w"> </span><span class="err">}</span><span class="w"></span></span>
<span class="code-line"><span class="w"> </span><span class="err">}</span><span class="w"></span></span>
<span class="code-line"><span class="w"> </span><span class="o">//</span><span class="p">...</span><span class="w"></span></span>
<span class="code-line"><span class="err">}</span><span class="w"></span></span>
</pre></div>
<h2 id="analysis_1">analysis</h2>
<p><strong>proposition </strong>(average case): <br/>
(the performance <em>on average</em> — <em>for randomly sorted array</em> ) <br/>
<img alt="" class="img-responsive" src="../images/algoI_week2_2/pasted_image007.png"/> <br/>
<em>proof:</em>
<em>expect each entry to move halfway back</em> <br/>
<img alt="" class="img-responsive" src="../images/algoI_week2_2/pasted_image008.png"/> </p>
<h3 id="best-case-and-worst-case">best case and worst case</h3>
<p><strong>best case</strong>
if array already sorted min ascending order:
<em>N-1 compares, 0 exchanges.</em> </p>
<p><strong>worst case</strong>
if array sorted in descending order:
every element goes all the way back → 1/2<em>N^2 compares, 1/2</em>N^2 exchanges</p>
<h3 id="partially-sorted-arrays">partially sorted arrays</h3>
<p>def.<strong>" inversion"</strong>
an inversion is a pair of entries that are out of order. </p>
<p>def. <strong>"partially sorted"</strong><br/>
An array is called partially sorted if the <em>number of inversions is <= c</em>N. *</p>
<p><strong>proposition. </strong><br/>
<strong>Insertion sort runs in linear time for partially sorted array.</strong>
<em>proof.</em>
<em>number of exchanges = number of inversions. </em>
<em>number of compares = number of exchanges + N-1</em></p>
<h1 id="4-shell-sort_2">4. Shell Sort</h1>
<p>First non-trival sorting methode: an improvement of insertion sort. </p>
<p>def. <strong>"h-sorted array"</strong><br/>
an array is h-sorted if every h-interleaved subarray is sorted. (h=1: just a sorted array) <br/>
<img alt="" class="img-responsive" src="../images/algoI_week2_2/pasted_image009.png"/> </p>
<p>Idea: move entries >1 position at a time by <em>h-sorting</em> the array, then decrease h. </p>
<p>use decreasing sequences of value h: <br/>
<img alt="" class="img-responsive" src="../images/algoI_week2_2/pasted_image010.png"/> </p>
<h2 id="implementation_2">implementation</h2>
<p><strong>How to h -sort </strong><br/>
simply insertion sort with <em>stride length=h</em>.<br/>
why insertion sort: <br/>
for big h: small subarray <br/>
for small h: nearly in order </p>
<p><strong>proposition</strong><br/>
A g-sorted array <em>remains g-sorted</em> after h-sorting it. <br/>
<img alt="" class="img-responsive" src="../images/algoI_week2_2/pasted_image011.png"/> <br/>
(subtle to prove...) </p>
<p><strong>which sequence of h to use</strong>
<code>3x+1</code> sequence proposed by Knuth. <em>1,4,13,40....</em></p>
<div class="highlight"><pre><span class="code-line"><span></span><span class="k">public</span><span class="w"> </span><span class="k">class</span><span class="w"> </span><span class="n">ShellSort</span><span class="w"> </span><span class="n">extends</span><span class="w"> </span><span class="n">AbstractSort</span><span class="err">{</span><span class="w"></span></span>
<span class="code-line"><span class="w"> </span><span class="k">public</span><span class="w"> </span><span class="k">static</span><span class="w"> </span><span class="n">void</span><span class="w"> </span><span class="n">sort</span><span class="p">(</span><span class="n">Comparable</span><span class="err">[]</span><span class="w"> </span><span class="n">a</span><span class="p">)</span><span class="err">{</span><span class="w"></span></span>
<span class="code-line"><span class="w"> </span><span class="nc">int</span><span class="w"> </span><span class="n">h</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="n">N</span><span class="o">=</span><span class="n">a</span><span class="p">.</span><span class="n">length</span><span class="p">;</span><span class="w"></span></span>
<span class="code-line"><span class="w"> </span><span class="k">while</span><span class="p">(</span><span class="n">h</span><span class="o"><</span><span class="n">N</span><span class="o">/</span><span class="mi">3</span><span class="p">)</span><span class="w"> </span><span class="n">h</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">h</span><span class="o">*</span><span class="mi">3</span><span class="o">+</span><span class="mi">1</span><span class="p">;</span><span class="o">//</span><span class="n">find</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n">beginning</span><span class="w"> </span><span class="n">h</span><span class="w"> </span><span class="p">(</span><span class="n">N</span><span class="o">></span><span class="n">h</span><span class="o">></span><span class="n">N</span><span class="o">/</span><span class="mi">3</span><span class="p">)</span><span class="w"></span></span>
<span class="code-line"><span class="w"> </span><span class="k">while</span><span class="p">(</span><span class="n">h</span><span class="o">>=</span><span class="mi">1</span><span class="p">)</span><span class="err">{</span><span class="o">//</span><span class="n">performs</span><span class="w"> </span><span class="n">h</span><span class="o">-</span><span class="n">sort</span><span class="w"></span></span>
<span class="code-line"><span class="w"> </span><span class="k">for</span><span class="p">(</span><span class="nc">int</span><span class="w"> </span><span class="n">i</span><span class="o">=</span><span class="w"> </span><span class="n">h</span><span class="p">;</span><span class="n">i</span><span class="o"><</span><span class="n">N</span><span class="p">;</span><span class="n">i</span><span class="o">+=</span><span class="n">h</span><span class="p">)</span><span class="w"></span></span>
<span class="code-line"><span class="w"> </span><span class="k">for</span><span class="p">(</span><span class="nc">int</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">i</span><span class="p">;</span><span class="n">j</span><span class="o">-</span><span class="n">h</span><span class="o">>=</span><span class="mi">0</span><span class="p">;</span><span class="n">j</span><span class="o">-=</span><span class="n">h</span><span class="p">)</span><span class="w"></span></span>
<span class="code-line"><span class="w"> </span><span class="k">if</span><span class="p">(</span><span class="w"> </span><span class="k">less</span><span class="p">(</span><span class="n">a</span><span class="o">[</span><span class="n">j</span><span class="o">]</span><span class="p">,</span><span class="n">a</span><span class="o">[</span><span class="n">j-h</span><span class="o">]</span><span class="p">)</span><span class="w"> </span><span class="p">)</span><span class="w"></span></span>
<span class="code-line"><span class="w"> </span><span class="n">exch</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="n">j</span><span class="p">,</span><span class="n">j</span><span class="o">-</span><span class="n">h</span><span class="p">)</span><span class="w"></span></span>
<span class="code-line"><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="k">break</span><span class="p">;</span><span class="w"></span></span>
<span class="code-line"><span class="w"> </span><span class="n">h</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">h</span><span class="o">/</span><span class="mi">3</span><span class="p">;</span><span class="w"></span></span>
<span class="code-line"><span class="w"> </span><span class="err">}</span><span class="w"></span></span>
<span class="code-line"><span class="w"> </span><span class="err">}</span><span class="w"></span></span>
<span class="code-line"><span class="w"> </span><span class="o">//</span><span class="p">...</span><span class="w"></span></span>
<span class="code-line"><span class="w"> </span><span class="n">private</span><span class="w"> </span><span class="k">static</span><span class="w"> </span><span class="k">boolean</span><span class="w"> </span><span class="n">isHsorted</span><span class="p">(</span><span class="n">Comparable</span><span class="err">[]</span><span class="w"> </span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="nc">int</span><span class="w"> </span><span class="n">h</span><span class="p">)</span><span class="w"> </span><span class="err">{</span><span class="w"></span></span>
<span class="code-line"><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="nc">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">h</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">a</span><span class="p">.</span><span class="n">length</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"></span></span>
<span class="code-line"><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="k">less</span><span class="p">(</span><span class="n">a</span><span class="o">[</span><span class="n">i</span><span class="o">]</span><span class="p">,</span><span class="w"> </span><span class="n">a</span><span class="o">[</span><span class="n">i-h</span><span class="o">]</span><span class="p">))</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="k">false</span><span class="p">;</span><span class="w"></span></span>
<span class="code-line"><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="k">true</span><span class="p">;</span><span class="w"></span></span>
<span class="code-line"><span class="w"> </span><span class="err">}</span><span class="w"></span></span>
<span class="code-line"><span class="err">}</span><span class="w"></span></span>
</pre></div>
<p>每次hsort, 外围的循环是<code>for(int i= h;i<N;i+=h)</code>, 需要理解一下: i移动一次以后, 进行的是另一个subarray 的插入排序, 当移动到N-1的时候所有subarray的插入排序才结束. (也就是说不是先完成一个subarray的插入排序再完成另一个, 这些是插入排序是同步进行的)</p>
<h2 id="analysis_2">analysis</h2>
<p><strong>proposition (for worst case )</strong><br/>
<img alt="" class="img-responsive" src="../images/algoI_week2_2/pasted_image012.png"/> <br/>
→ better than quadratic time ! </p>
<p><strong>property (found in practice)</strong></p>
<h1 id="of-compares-cte-n-of-h-used_1">of compares < Cte * N * (# of <em>h</em> used )</h1>
<p>→ #compares < NlgN * Cte <br/>
<img alt="" class="img-responsive" src="../images/algoI_week2_2/pasted_image013.png"/> <br/>
*accurate model has not been discovered *</p>
<p>(所以shellsort在实际使用中几乎和快速排序一样快! — 尽管没有数学证明来保证)</p>
<p><strong>why we are interested in shell sort</strong>
useful in practice: </p>
<ul>
<li>fast for medium sized arrays (beat even the classical sophistiated algorithms) </li>
<li>tiny code volumn (used in embeded systems) </li>
</ul>
<p>lead to interesting questions for 50 years: </p>
<ul>
<li>asymptotic growth rate ?</li>
<li>best sequence of h ?</li>
<li>average case performance ?</li>
</ul>
<h1 id="5-shuffling">5. shuffling</h1>
<h2 id="shuffle-array-using-sort">shuffle array using sort</h2>
<p>one way to shuffle an array: </p>
<ol>
<li>for each array entry, generate a random real number</li>
<li>sort the array of real numbers</li>
<li>⇒ the original array is shuffled !</li>
</ol>
<p><strong>proposition </strong><br/>
this shuffle sort produces a uniformly random permutation of input array <br/>
<em>drawback: cost for sorting...</em></p>
<p>Goal: get uniformly random permutation in <em>linear time</em>. </p>
<h2 id="knuth-shuffle">Knuth shuffle</h2>
<p>algo: </p>
<blockquote>
<p>for i = [0,N):<br/>
- r = rand( [0~i<strong>]</strong> ) or rand( <strong>[</strong>i, N-1] ) <br/>
- swap a[r] and a[i] </p>
</blockquote>
<p><img alt="" class="img-responsive" src="../images/algoI_week2_2/pasted_image014.png"/></p>
<p>implementation: </p>
<div class="highlight"><pre><span class="code-line"><span></span><span class="err">public static void shuffle(Object[] a){</span></span>
<span class="code-line"><span class="err"> for(int i=0;i<a.length;i++){</span></span>
<span class="code-line"><span class="err"> int r = StdRandom.uniform(i+1);</span></span>
<span class="code-line"><span class="err"> exch(a,r,i);</span></span>
<span class="code-line"><span class="err"> } </span></span>
<span class="code-line"><span class="err">}</span></span>
</pre></div>
<p><strong>proposition</strong><br/>
Knuth algo produces an uniformly random permutation of input array. </p>
<p><em>proof.</em><br/>
Sufficient to prove that, for card i and position j, the proba(card i comes to position j) = 1/N. </p>
<ol>
<li>if i<=j, P = 1/j * j/(j+1) * (j+1)/(j+2) * ... * (N-1)/N</li>
<li>if j<i, P = 1/i * i/(i+1) * (i+1)/(i+2) * ... * (N-1)/N</li>
</ol>
<p>CQFD.</p>
<h2 id="example-online-poker">example: online poker</h2>
<p><a href="https://www.cigital.com/papers/download/developer_gambling.php">https://www.cigital.com/papers/download/developer_gambling.php</a> ←那个扑克网站已经被黑出翔了... <br/>
<img alt="" class="img-responsive" src="../images/algoI_week2_2/pasted_image015.png"/> <br/>
bugs: </p>
<ol>
<li>r never get 52 (52th card never moved)</li>
<li>r = rand(N) instead of rand(0~i), → shuffle not uniform</li>
<li>random() uses 32bit seed: only 2^32 possible shuffles, <em>2^32<52!</em></li>
<li>seed = millisec from midnight, ~86*10^6 suffles</li>
</ol>
<h1 id="6-convex-hull_1">6. Convex Hull</h1>
<p>application of sorting for the field of computational geometry. </p>
<h2 id="convex-hull">convex hull</h2>
<p>smallest polygoneenclosing all N points. <br/>
<img alt="" class="img-responsive" src="../images/algoI_week2_2/pasted_image017.png"/> </p>
<ul>
<li>input: N points</li>
<li>output: sequence of <em>vertices</em> in counterclockwise (<em>ccw</em>) order. </li>
</ul>
<p>application: robot motion planning; farest pair. </p>
<p><strong>geometric properties</strong>: </p>
<ul>
<li>can traverse convex hull by making only ccw turns</li>
<li>let p be the point with lowest y-coord, wrt p, vertices appear in increasing order of polar angle. </li>
</ul>
<p><img alt="" class="img-responsive" src="../images/algoI_week2_2/pasted_image018.png"/> </p>
<h2 id="algo">Algo</h2>
<blockquote>
<p><strong>Graham scan algorithm:</strong><br/>
* choose p with smallest y coord <br/>
* <em>sort points by polar angle</em> with p <br/>
* consider points in order (stack is used), <em>discard unless creates a ccw turn</em>. </p>
</blockquote>
<p><img alt="" class="img-responsive" src="../images/algoI_week2_2/pasted_image019.png"/> <img alt="" class="img-responsive" src="../images/algoI_week2_2/pasted_image020.png"/> <img alt="" class="img-responsive" src="../images/algoI_week2_2/pasted_image021.png"/> </p>
<p><strong>CCW</strong> <br/>
given three points a b c, returns if a→b→c is a CCW turn. <br/>
<img alt="" class="img-responsive" src="../images/algoI_week2_2/pasted_image023.png"/> <br/>
(assumption: no 3 points on a line)<br/>
⇒ calculate <em>cross product of ab and bc ⇒ determinants!</em> <br/>
<img alt="" class="img-responsive" src="../images/algoI_week2_2/pasted_image024.png"/> <br/>
area>0 ⇔ CCW </p>
<h2 id="implementation_3">implementation</h2>
<div class="highlight"><pre><span class="code-line"><span></span><span class="k">public</span> <span class="k">class</span> <span class="n">Point2D</span><span class="err">{</span></span>
<span class="code-line"><span class="n">private</span> <span class="n">double</span> <span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">;</span></span>
<span class="code-line"><span class="k">public</span> <span class="k">static</span> <span class="nb">boolean</span> <span class="n">ccw</span><span class="p">(</span><span class="n">Point2D</span> <span class="n">a</span><span class="p">,</span><span class="n">Point2D</span> <span class="n">b</span><span class="p">,</span><span class="n">Point2D</span> <span class="k">c</span><span class="p">)</span><span class="err">{</span></span>
<span class="code-line"> <span class="n">double</span> <span class="n">area2</span> <span class="o">=</span> <span class="p">(</span><span class="n">b</span><span class="p">.</span><span class="n">x</span><span class="o">-</span><span class="n">a</span><span class="p">.</span><span class="n">x</span><span class="p">)</span><span class="o">*</span><span class="p">(</span><span class="k">c</span><span class="p">.</span><span class="n">y</span><span class="o">-</span><span class="n">a</span><span class="p">.</span><span class="n">y</span><span class="p">)</span> <span class="o">-</span> <span class="p">(</span><span class="n">b</span><span class="p">.</span><span class="n">y</span><span class="o">-</span><span class="n">a</span><span class="p">.</span><span class="n">y</span><span class="p">)</span><span class="o">*</span><span class="p">(</span><span class="k">c</span><span class="p">.</span><span class="n">x</span><span class="o">-</span><span class="n">a</span><span class="p">.</span><span class="n">x</span><span class="p">);</span></span>
<span class="code-line"> <span class="k">return</span> <span class="n">area2</span><span class="o">></span><span class="mi">0</span><span class="p">;</span></span>
<span class="code-line"><span class="err">}</span></span>
<span class="code-line"></span>
<span class="code-line"><span class="err">}</span></span>
</pre></div>
<p>convex hull: </p>
<div class="highlight"><pre><span class="code-line"><span></span><span class="k">public</span><span class="w"> </span><span class="k">static</span><span class="w"> </span><span class="n">Stack</span><span class="o"><</span><span class="n">Point2D</span><span class="o">></span><span class="w"> </span><span class="n">GrahamScan</span><span class="p">(</span><span class="n">Point2D</span><span class="err">[]</span><span class="w"> </span><span class="n">p</span><span class="p">)</span><span class="err">{</span><span class="w"></span></span>
<span class="code-line"><span class="w"> </span><span class="o">/</span><span class="cm">/* assumes that points are sorted by polar angle in p[]</span></span>
<span class="code-line"><span class="cm"> Stack<Point2D> hull = new Stack<Point2D>();</span></span>
<span class="code-line"><span class="cm"> hull.push(p[0]);</span></span>
<span class="code-line"><span class="cm"> hull.push(p[1]);</span></span>
<span class="code-line"><span class="cm"> for(int i=2;i<p.length;i++){</span></span>
<span class="code-line"><span class="cm"> Point2D b = hull.pop(), a = hull.peek(), c = p[i];</span></span>
<span class="code-line"><span class="cm"> while(!Point2D.ccw(a,b,c)){</span></span>
<span class="code-line"><span class="cm"> b = hull.pop();</span></span>
<span class="code-line"><span class="cm"> a = hull.peek();</span></span>
<span class="code-line"><span class="cm"> }</span></span>
<span class="code-line"><span class="cm"> //now a,b,c makes a ccw turn:</span></span>
<span class="code-line"><span class="cm"> hull.push(b);</span></span>
<span class="code-line"><span class="cm"> hull.push(c);</span></span>
<span class="code-line"><span class="cm"> }</span></span>
<span class="code-line"><span class="cm">}</span></span>
</pre></div>
<p>running time: <em>NlgN for sorting and linear for the rest.</em> </p>
</div>
<div class="entry-content jumbotron" id="source-content" style="display: none">
<!-- <pre><code id="source-code">
</code></pre> -->
<div id="source-code"></div>
</div>
<!-- /.entry-content -->
<div class="row" id="prevnext">
<div class="col-xs-12">
<a href="https://x-wei.github.io/algoI_week2_1.html" class="btn btn-default btn-lg" style="float:left;clear:both;background-color:#fff;">
<h4><i class="fa fa-arrow-left"></i>
[Algorithms I] Week 2-1 Stacks and Queues
</h4>
</a>
</div>
<div class="col-xs-12">
<a href="https://x-wei.github.io/algoI_week3_1.html" class="btn btn-default btn-lg" style="float:right;clear:both;background-color:#fff;">
<h4>
[Algorithms I] Week 3-1 Mergesort<i class="fa fa-arrow-right"></i>
</h4>
</a>
</div>
</div>
<div class="panel panel-default" id="series">
<div class="panel-heading">
<h4>
Part 5 of series «Algorithms Princeton MOOC I»:
</h4>
</div>
<ul class="list-group">
<li class="list-group-item"><a class="btn btn-default" href='https://x-wei.github.io/algoI_week1_1.html' style="text-align:left">[Algorithms I] Week 1-1 Union-Find</a></li>
<li class="list-group-item"><a class="btn btn-default" href='https://x-wei.github.io/algoI_week1_2.html' style="text-align:left">[Algorithms I] Week 1-2 Analysis of Algorithms</a></li>
<li class="list-group-item"><a class="btn btn-default" href='https://x-wei.github.io/algoI_week1_lab.html' style="text-align:left">[Algorithms I] Week1-Lab: Percolation</a></li>
<li class="list-group-item"><a class="btn btn-default" href='https://x-wei.github.io/algoI_week2_1.html' style="text-align:left">[Algorithms I] Week 2-1 Stacks and Queues</a></li>
<li class="list-group-item"><a class="btn btn-primary" href='https://x-wei.github.io/algoI_week2_2.html' style="text-align:left">[Algorithms I] Week 2-2 Elementary Sorts</a></li>
<li class="list-group-item"><a class="btn btn-default" href='https://x-wei.github.io/algoI_week3_1.html' style="text-align:left">[Algorithms I] Week 3-1 Mergesort</a></li>
<li class="list-group-item"><a class="btn btn-default" href='https://x-wei.github.io/algoI_week3_2.html' style="text-align:left">[Algorithms I] Week 3-2 Quicksort</a></li>
<li class="list-group-item"><a class="btn btn-default" href='https://x-wei.github.io/algoI_week4_1.html' style="text-align:left">[Algorithms I] Week 4-1 Priority Queue</a></li>
<li class="list-group-item"><a class="btn btn-default" href='https://x-wei.github.io/algoI_week4_2a.html' style="text-align:left">[Algorithms I] Week 4-2a Elementry Symbol Tables</a></li>
<li class="list-group-item"><a class="btn btn-default" href='https://x-wei.github.io/algoI_week4_2b.html' style="text-align:left">[Algorithms I] Week 4-2b Binary Search Trees</a></li>
<li class="list-group-item"><a class="btn btn-default" href='https://x-wei.github.io/algoI_week5_1.html' style="text-align:left">[Algorithms I] Week 5-1 Balanced Search Trees</a></li>
<li class="list-group-item"><a class="btn btn-default" href='https://x-wei.github.io/algoI_week5_2.html' style="text-align:left">[Algorithms I] Week 5-2 Geometric Applications of BSTs</a></li>
<li class="list-group-item"><a class="btn btn-default" href='https://x-wei.github.io/algoI_week6.html' style="text-align:left">[Algorithms I] Week 6 Hash Tables</a></li>
</ul>
</div>
<section class="comments" id="comments">
<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
<div class="panel panel-primary">
<div class="panel-heading" role="tab" id="disqus-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#disqus-comments" aria-expanded="true" aria-controls="disqus-comments">
<i class="fa fa-comments-o"></i> Disqus 留言
</a>
</h4>
</div>
<div id="disqus-comments" class="panel-collapse collapse.show" role="tabpanel" aria-labelledby="disqus-heading">
<div class="panel-body">
<div class="tab-pane fade active in" id="disqus-comments">
<div id="disqus_thread"></div>
<script type="text/javascript">
/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
var disqus_shortname = 'xweisblog'; // required: replace example with your forum shortname
var disqus_identifier = 'algoI_week2_2';
var disqus_url = 'https://x-wei.github.io/algoI_week2_2.html';
var disqus_config = function () {
this.language = "zh";
};
/* * * DON'T EDIT BELOW THIS LINE * * */
(function () {
var dsq = document.createElement('script');
dsq.type = 'text/javascript';
dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
</script>
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by
Disqus.</a></noscript>
<a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
</div>
</div>
</div>
</div>
</div>
</section> </article>
</section>
</div>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<a data-dismiss="modal" href="javascript:void(0);">
<img id="mimg" src="" style="width:100%;height:auto">
</a>
</div>
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
</div>
<div class="col-xl-3 col-lg-4 col-md-6" id="sidebar">
<aside>
<section>
<div class="sidebar-container">
<div class="sidebar-item ">
<div class="panel panel-default">
<div class="panel-heading">
<h4>
<i class="fa fa-user fa-lg"></i>
<a href="https://x-wei.github.io/about.html">
关于 mx
</a>
</h4>
</div>
<div class="panel-body" id="aboutme">
<a href="https://x-wei.github.io/about.html"><img width="100%" src="https://x-wei.github.io/../images/mx.jpg"/></a>
<h3 style="text-align:center">
<a href="https://github.com/x-wei" target="_blank">
<i class="fa fa-github" style="text-align:center"></i></a>
<a href="https://weibo.com/u/1817154611" target="_blank">
<i class="fa fa-weibo" style="text-align:center"></i></a>
<a href="mailto:[email protected]" target="_blank">
<i class="mdi-communication-email" style="text-align:center"></i></a>
</h3>
<h4 class="widget-title">推荐文章</h4>
<div class="textwidget">
<li class="widget-container widget_text">
<a href="https://x-wei.github.io/TeXmacs_intro.html">学术文章写作利器: TeXmacs介绍</a><br></li>
<li class="widget-container widget_text">
<a href="https://x-wei.github.io/hashcode2014-solved-by-LP.html">运筹的力量: 用线性规划解决Google 2014 HashCode问题</a><br></li>
<li class="widget-container widget_text">
<a href="https://x-wei.github.io/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F%E5%85%A5%E9%97%A8%E7%AE%80%E4%BB%8B.html">正则表达式入门简介</a><br></li>
<li class="widget-container widget_text">
<a href="https://x-wei.github.io/%E6%88%91%E7%9A%84ubuntu10.04%E9%85%8D%E7%BD%AE%E6%80%BB%E7%BB%93.html">我的ubuntu10.04配置总结</a><br></li>
<li class="widget-container widget_text">
<a href="https://x-wei.github.io/PT-summery.html">2011巴黎高科(ParisTech)申请总结</a><br></li>
<li class="widget-container widget_text">
<a href="https://x-wei.github.io/GT-summery.html">用尽量少的时间考一个够用的分数--一点Tofel/GRE备考经验</a><br></li>
<li class="widget-container widget_text">
<a href="https://x-wei.github.io/pelican_github_blog.html">用pelican在github上创建自己的博客!</a><br></li>
</div>
<br><a href="https://www.polytechnique.edu/" target="_blank">
<img src="https://x-wei.github.io/images/x-logo.png" alt="X" width="180" border="0" />
</a><br/>
<br><a href="https://www.sjtu.edu.cn/">
<img src="https://x-wei.github.io/images/ssss.jpg" width="180" border="0" alt="上海西南某高校">
</a><br/>
<br>
<h4 class="widget-title">Visitors</h4>
<script type="text/javascript" src="//rf.revolvermaps.com/0/0/1.js?i=59olkba9w7e&s=220&m=3&v=true&r=false&b=000000&n=false&c=ff0000" async="async"></script>
<!-- hitwebcounter Code START -->
<a href="https://www.hitwebcounter.com/how-to/how-to-what-is-free-blog-counter.php" target="_blank">
<img src="https://hitwebcounter.com/counter/counter.php?page=5954927&style=0036&nbdigits=5&type=ip&initCount=0" title="web counter" Alt="web counter" border="0" ></a>
<br/>
</div>
</div>
</div>
<div class="sidebar-item ">
<div class="panel panel-default">
<div class="panel-heading">
<h4>
<a href="https://x-wei.github.io/tags.html"><i class="fa fa-tags fa-lg"></i><span class="icon-label">标签云</span></a>
</h4>
</div>
<div class="panel-body">
<ul class="list-group list-inline tagcloud" id="tags">
<li class="list-group-item tag-2">
<a href="https://x-wei.github.io/tag/pelican.html">
pelican <sup> 6</sup>
</a>
</li>
<li class="list-group-item tag-2">
<a href="https://x-wei.github.io/tag/google.html">
google <sup> 6</sup>
</a>
</li>
<li class="list-group-item tag-2">
<a href="https://x-wei.github.io/tag/torch.html">
torch <sup> 6</sup>
</a>
</li>
<li class="list-group-item tag-2">
<a href="https://x-wei.github.io/tag/tex.html">
tex <sup> 4</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="https://x-wei.github.io/tag/ba-li-gao-ke.html">
巴黎高科 <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-3">
<a href="https://x-wei.github.io/tag/markdown.html">
markdown <sup> 2</sup>
</a>
</li>
<li class="list-group-item tag-3">
<a href="https://x-wei.github.io/tag/linux.html">
linux <sup> 3</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="https://x-wei.github.io/tag/inf422.html">
inf422 <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="https://x-wei.github.io/tag/toefl.html">
Toefl <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="https://x-wei.github.io/tag/regex.html">
regex <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-2">
<a href="https://x-wei.github.io/tag/git.html">
git <sup> 5</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="https://x-wei.github.io/tag/weka.html">
weka <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-1">
<a href="https://x-wei.github.io/tag/scala.html">
scala <sup> 12</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="https://x-wei.github.io/tag/opencv.html">
opencv <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="https://x-wei.github.io/tag/scrapy.html">
scrapy <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="https://x-wei.github.io/tag/gre.html">
GRE <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="https://x-wei.github.io/tag/lp.html">
LP <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-2">
<a href="https://x-wei.github.io/tag/android.html">
android <sup> 9</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="https://x-wei.github.io/tag/pandas.html">
pandas <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-3">
<a href="https://x-wei.github.io/tag/codejam.html">
codejam <sup> 2</sup>
</a>
</li>
<li class="list-group-item tag-3">
<a href="https://x-wei.github.io/tag/fr.html">
fr <sup> 2</sup>
</a>
</li>
<li class="list-group-item tag-2">
<a href="https://x-wei.github.io/tag/shell.html">
shell <sup> 4</sup>
</a>
</li>
<li class="list-group-item tag-1">
<a href="https://x-wei.github.io/tag/python.html">
python <sup> 13</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="https://x-wei.github.io/tag/simongarfunkel.html">
Simon&Garfunkel <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-3">
<a href="https://x-wei.github.io/tag/french.html">
french <sup> 2</sup>
</a>
</li>
<li class="list-group-item tag-2">
<a href="https://x-wei.github.io/tag/wai-guan.html">
外观 <sup> 4</sup>
</a>
</li>
<li class="list-group-item tag-3">
<a href="https://x-wei.github.io/tag/zhong-wen-luan-ma.html">
中文乱码 <sup> 2</sup>
</a>
</li>
<li class="list-group-item tag-3">
<a href="https://x-wei.github.io/tag/host.html">
host <sup> 3</sup>
</a>
</li>
<li class="list-group-item tag-3">
<a href="https://x-wei.github.io/tag/ssh.html">
ssh <sup> 2</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="https://x-wei.github.io/tag/ubuntu.html">
ubuntu <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-2">
<a href="https://x-wei.github.io/tag/java.html">
java <sup> 4</sup>
</a>
</li>
<li class="list-group-item tag-2">
<a href="https://x-wei.github.io/tag/spark.html">
spark <sup> 6</sup>
</a>
</li>
<li class="list-group-item tag-3">
<a href="https://x-wei.github.io/tag/tips.html">
tips <sup> 3</sup>
</a>
</li>
<li class="list-group-item tag-1">
<a href="https://x-wei.github.io/tag/deep-learning.html">
deep learning <sup> 28</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="https://x-wei.github.io/tag/c.html">
C++ <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-1">
<a href="https://x-wei.github.io/tag/algorithm.html">
algorithm <sup> 35</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="https://x-wei.github.io/tag/texmacs.html">
TeXmacs <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-2">
<a href="https://x-wei.github.io/tag/ml.html">
ml <sup> 4</sup>
</a>
</li>
<li class="list-group-item tag-2">
<a href="https://x-wei.github.io/tag/eclipse.html">
eclipse <sup> 4</sup>
</a>
</li>
<li class="list-group-item tag-2">
<a href="https://x-wei.github.io/tag/ocaml.html">
OCaml <sup> 8</sup>
</a>
</li>
<li class="list-group-item tag-2">
<a href="https://x-wei.github.io/tag/r.html">
R <sup> 4</sup>
</a>
</li>
<li class="list-group-item tag-3">
<a href="https://x-wei.github.io/tag/chu-guo.html">
出国 <sup> 2</sup>
</a>
</li>
<li class="list-group-item tag-3">
<a href="https://x-wei.github.io/tag/kuai-jie-jian.html">
快捷键 <sup> 3</sup>
</a>
</li>
</ul>
</div>
</div>
</div>
<li class="list-group-item"><h4><i class="fa fa-tags fa-list-ul"></i><span class="icon-label">Series</span></h4>
<ul class="list-group">
<li class="list-group-item">
<h5></i> Previous article</h5>
<a href="https://x-wei.github.io/algoI_week2_1.html">[Algorithms I] Week 2-1 Stacks and Queues</a>
</li>
<li class="list-group-item">
<h5>Next article</h5>
<a href="https://x-wei.github.io/algoI_week3_1.html">[Algorithms I] Week 3-1 Mergesort</a>
</li>
</ul>
</li>
<div class="sidebar-item hidden-xs">
<div class="panel panel-default">
<div class="panel-heading">
<h4>
<i class="fa fa-github fa-lg"></i><span class="icon-label">GitHub仓库</span>
</h4>
</div>
<div class="panel-body">
<div id="gh_repos">
<p class="list-group-item">Status updating...</p>
</div>
<a href="https://github.com/x-wei">@x-wei</a> on GitHub
</div>
</div>
</div>
</div>
</section>
<div class="panel panel-default hidden-xs hidden-sm" id="affix-toc">
<div class="panel-heading"><h4>
目录</h4>
</div>
<div class="panel-boy">
<div id="toc"><ul><li><a class="toc-href" href="#1-introduction" title="1. Introduction">1. Introduction</a><ul><li><a class="toc-href" href="#callback" title="callback">callback</a></li><li><a class="toc-href" href="#helper-functions" title="helper functions">helper functions</a></li></ul></li><li><a class="toc-href" href="#2-selection-sort_1" title="2. Selection Sort">2. Selection Sort</a><ul><li><a class="toc-href" href="#invariants" title="invariants">invariants</a></li><li><a class="toc-href" href="#implementation" title="implementation">implementation</a></li><li><a class="toc-href" href="#analysis" title="analysis">analysis</a></li></ul></li><li><a class="toc-href" href="#3-insertion-sort_1" title="3. Insertion sort">3. Insertion sort</a><ul><li><a class="toc-href" href="#invariants_1" title="invariants">invariants</a></li><li><a class="toc-href" href="#implementation_1" title="implementation">implementation</a></li><li><a class="toc-href" href="#analysis_1" title="analysis">analysis</a><ul><li><a class="toc-href" href="#best-case-and-worst-case" title="best case and worst case">best case and worst case</a></li><li><a class="toc-href" href="#partially-sorted-arrays" title="partially sorted arrays">partially sorted arrays</a></li></ul></li></ul></li><li><a class="toc-href" href="#4-shell-sort_2" title="4. Shell Sort">4. Shell Sort</a><ul><li><a class="toc-href" href="#implementation_2" title="implementation">implementation</a></li><li><a class="toc-href" href="#analysis_2" title="analysis">analysis</a></li></ul></li><li><a class="toc-href" href="#of-compares-cte-n-of-h-used_1" title="of compares < Cte * N * (# of h used )">of compares < Cte * N * (# of h used )</a></li><li><a class="toc-href" href="#5-shuffling" title="5. shuffling">5. shuffling</a><ul><li><a class="toc-href" href="#shuffle-array-using-sort" title="shuffle array using sort">shuffle array using sort</a></li><li><a class="toc-href" href="#knuth-shuffle" title="Knuth shuffle">Knuth shuffle</a></li><li><a class="toc-href" href="#example-online-poker" title="example: online poker">example: online poker</a></li></ul></li><li><a class="toc-href" href="#6-convex-hull_1" title="6. Convex Hull">6. Convex Hull</a><ul><li><a class="toc-href" href="#convex-hull" title="convex hull">convex hull</a></li><li><a class="toc-href" href="#algo" title="Algo">Algo</a></li><li><a class="toc-href" href="#implementation_3" title="implementation">implementation</a></li></ul></li></ul></div>
</div>
</div>
</aside>
</div>
</div>
</div>
<footer id="fcfooter">
<hr/>
<div class="container">
links :
<a href="https://farseerfc.github.com/">farseerfc</a>
<a href="https://hyhx2008.github.com/">H.Y.</a>
<a href="https://reginald1787.github.io/">reginald1787</a>
<a href="https://log.dofine.me/">dofine</a>
<div class="row">
<div class="col-md-14">
<p><small>
© 2020 mx
· 通过
<a href="https://docs.getpelican.com/" target="_blank">Pelican</a> 生成 <a rel="license" href="https://creativecommons.org/licenses/by-nc-sa/4.0/"><img alt="Creative Commons License" style="border-width:0" src="//i.creativecommons.org/l/by-nc-sa/4.0/80x15.png" /></a>
<!-- Content -->
<!-- licensed under a <a rel="license" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">Creative Commons Attribution 4.0 International License</a>, except where indicated otherwise. -->
</small></p>
</div>
</div>
</div>
<a href="#" class="btn btn-primary btn-fab btn-raised mdi-editor-vertical-align-top withripple" style="position:fixed;bottom:30px;right:30px;z-index:1000"></a>
</footer>
<script src="https://x-wei.github.io/theme/js/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="https://x-wei.github.io/theme/js/bootstrap.min.js"></script>
<!-- Enable responsive features in IE8 with Respond.js (https://github.com/scottjehl/Respond) -->
<script src="https://x-wei.github.io/theme/js/respond.min.js"></script>