-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
1346 lines (1172 loc) · 49.3 KB
/
index.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="en">
<head>
<meta charset="utf-8">
<title>Intro to Algorithms& Intro to Responsive Design ♥ Girl Develop It Chicago</title>
<meta name="description" content="">
<meta name="author" content="Pamela Fox">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<link rel="stylesheet" href="../common/css/reveal.css">
<!-- FOR LIGHT -->
<link rel="stylesheet" href="../common/css/theme/gdilight.css" id="theme">
<link rel="stylesheet" href="../common/lib/css/light.css">
<style>
.highlight { color: #2a4758 !important; }
</style>
<!-- FOR DARK -->
<!--
<link rel="stylesheet" href="../common/css/theme/gdidefault.css" id="theme">
<link rel="stylesheet" href="../common/lib/css/dark.css">
<style>
table {background-color: black !important;}
.highlight { color: #d8ae64;}
</style>
-->
<link rel="stylesheet" href="css/slides.css">
<!-- If the query includes 'print-pdf', include the PDF print sheet -->
<link rel="stylesheet" href="css/algorithms.css">
<script>
if (window.location.search.match(/print-pdf/gi)) {
var link = document.createElement('link');
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = 'reveal/css/print/pdf.css';
document.getElementsByTagName('head')[0].appendChild(link);
}
</script>
<link rel="icon" type="image/x-icon" href="reveal/favicon.ico" />
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<style>
.left { text-align: left; }
</style>
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<!-- Opening -->
<section>
<h1>Intro to Algorithms</h1>
</section>
<section>
<h3>Schedule</h3>
<pre><code>
10:00 - 1PM Introduction
Defining an algorithm
Data Structures
Goal of algorithms
Time/space complexity
1:00 - 2:00 Lunch
2:00 - 5:00 Searching
Sorting
Binary Search
Algorithms in Real Life
Conclusion
</pre></code>
</section>
<section>
<h2>What we'll be covering in this class</h2>
<ul>
<li>what is an algorithm?</li>
<li>intro to algorithmic complexity
<br>(how good is an algorithm)</li>
<li>demonstrate sort and search algorithms
<br>(the classics)</li>
</ul>
</section>
<section>
<h2>Expectations</h2> <br>
<ul>
<li>Particpation</li>
<li>Keep coming up with more solutions </li>
<li>Postivity/Energy</li>
</ul>
<br><br>
<h4>Not expecting:</h4>
<ul>
<li>Syntax</li>
<li>Knowing all the things</li>
</ul>
</section>
<section>
<h1>Algorithms</h1>
<p><em>"A repeatable process for determining the solution to a problem."</em></p>
</section>
<section>
<h3>Algorithms in Daily Life</h3>
<ul>
<li class="fragment">Finding a fruit in the grocery store.</li>
<li class="fragment">Alphabetizing name tags.</li>
<li class="fragment">Re-organizing your kitchen to make finding stuff easier.</li>
<li class="fragment">Finding keys that you lost.</li>
<li class="fragment">Finding something good to watch on TV.</li>
<li class="fragment">Washing your car windows.</li>
</ul>
</section>
<section>
<h3>Finding something to watch on TV v1</h3>
<ol class="left">
<li>Turn on TV</li>
<li>Watch what is on</li>
</ol>
<br><br>
<img src="images/tv_1_code.png" style="width:50%;">
</section>
<section>
<h3>Finding something to watch on TV v2</h3>
<ol class="left">
<li>Turn on TV</li>
<li>Flip through every channel and rate what is on</li>
<li>Find the highest rated channel</li>
<li>Watch</li>
</ol>
</section>
<section>
<img src="images/tv_2_code.png" style="width:80%;">
</section>
<section>
<h3>Finding something to watch on TV v3</h3>
<ol class="left">
<li>Turn on TV</li>
<li>Check 5 favorite channels and rate what is on</li>
<li>Find the highest rated channel</li>
<li>Watch</li>
</ol>
</section>
<section>
<img src="images/tv_3_code.png" style="width:80%;">
</section>
<section>
<h3>Which version is best?</h3>
<br>
<span class="">
<p class="">Turn on the TV</p>
<p class="">Rate all channels</p>
OR
<p class="">Rate top channels</p>
</span>
</section>
<!-- START OF DATA STRUCTURE -->
<section>
<h1>Data Structures</h1>
<br>
<p>A way to store data <br>(so that it can be used as desired)</p>
</section>
<section>
<h2>Data Structures + Algorithms</h2>
<br>
<p>A good way to approach an algorithm is to think about the data structures that you know.</p>
<br>
<p>What are their properties, and how can they be used to your advantage?</p>
</section>
<section>
<h2>Example: Arrays/List</h2>
<img src="images/list.png">
</section>
<section>
<h2>Example: Sets</h2>
<img src="images/sets.png">
<p> Like a list but:
<ul>
<li> Not ordered </li>
<li> Unique Items</li>
</p>
</section>
<section>
<h2>Example: Dictionaries/ Objects/ Hash Maps/ Maps</h2>
<img src="images/hash-table.png">
<p> key : value </p>
</section>
<!-- START OF GOAL OF ALGORITHMS -->
<section>
<h2>Goal of Algorithms</h2>
<br>
<ul>
<li class="fragment">Solve a problem</li>
<li class="fragment">in a repeatable way</li>
</ul>
</section>
<section>
<h2>Goal of Whiteboarding Interviews</h2>
<ul>
<li>Show your thought process</li>
<li>Solve the most obvious (to you!) solution first</li>
<li>Improve it</li>
</ul><br>
</section>
<section>
<h3 class='highlight'>What is an algorithm you use?</h3>
<br>
<p>Write down steps for several versions of an algorithm to solve an everyday problem</p>
<br>
<p class="left pink">Ideas:</p>
<ul>
<li>Organizing email</li>
<li>Swiping left or right on tinder</li>
<li>Deciding what restaurant to eat at</li>
<li>Finding a paper on your desk</li>
</ul>
</section>
<!-- TIME COMPLEXITY -->
<section>
<h2>Optimizations</h2>
<br>
<p>Designing algorithms is about making decisions. Decisions have tradeoffs.</p>
<br>
<p>You may want to optimize to complete the task:</p>
<ul>
<li class="fragment">in the shortest time</li>
<li class="fragment">using the least amount of space</li>
</ul>
</section>
<section>
<h2>Complexity</h2>
<p class="red">How do you know if your algorithm is good?</p>
<p class="orange">How do you compare algorithms?</p>
<br>
<ul class="fragment">
<li >Time Complexity: How long does the algorithm take</li>
<li > Space Complexity: How much space does the algorithm use</li>
</ul>
<br>
<p class="fragment">The less complex the better!</p>
</section>
<section>
<h3>Time Efficient Grocery Shopping</h2>
<br>
<ol class="fragment">
<li>Drive large car to grocery store</li>
<li>Buy every thing you need</li>
</ol>
<br>
<div class="fragment">
<p class="red">Saves time, but requires you to have a large car</p>
<p>Lower time complexity</p>
<p>Higher space complexity</p>
</div>
</section>
<section>
<h3>Space Efficient Grocery Shopping</h2>
<br>
<ol class="fragment">
<li>Walk to grocery store with tote bag</li>
<li>Buy one item</li>
<li>Walk home and drop off the item</li>
<li>Repeat until done</li>
</ol>
<br>
<div class="fragment">
<p class="red">Takes a long time, but doesn't require a car</p>
<p>Higher time complexity</p>
<p>Lower space complexity</p>
</div>
</section>
<section>
<h2>Time Complexity</h2>
<br>
<p>How long does an algorithm take?</p>
</section>
<section>
<h2>Hanging up Laundry v1</h2>
<br>
<ol>
<li class="fragment">Dump laundry on floor
</ol>
<br>
<pre class="fragment"><code>
laundry.drop()
</code></pre>
<br>
<p class="red fragment">How long does the algorithm take?</p>
</section>
<section>
<h2>Laundry v1</h2>
<p class="red">How long does the algorithm take?</p>
<p class="fragment">
Dumping out my laundry takes 2 seconds
</p>
<div class="fragment">
<hr>
<table style="margin:auto; background:white; padding: 5px;">
<thead>
<tr class="orange">
<th>#items</th>
<th>#seconds</th>
</tr>
</thead>
<tbody>
<tr>
<td>4</td>
<td>2</td>
</tr>
<tr>
<td>8</td>
<td>2</td>
</tr>
<tr>
<td>16</td>
<td>2</td>
</tr>
</tbody>
</table>
<hr>
</div>
<p class="fragment">If N = items of clothing,<br> it takes (N*0 + 2) => O(2) => O(1).<br>
</p>
</section>
<section>
<img src="images/big-o-graph.png">
</section>
<section>
<h2>Laundry v2</h2>
<ol>
<li>Dump laundry on bed.
<li>Pick up each piece of clothing, put in a pile for that type ("shirts", "underwear", "socks", etc.)
<li>After all are in piles, go through each item in each pile, and hang up each one.
</ol>
</section>
<section>
<h2>Laundry v2 Code</h2>
<pre><code>
piles = {'shirts': [], 'socks': []}
for clothing_item in laundry:
piles[clothing_item.type].append(clothing_item)
for pile in piles:
for clothing_item in pile:
clothing_item.hang_up()
</code></pre>
</section>
<section>
<h2>Laundry v2</h2>
<p class="red">How long does the algorithm take?</p>
<p class="fragment">
Putting an item in a pile takes 10 seconds.
<br>
Hanging an item up takes 30 seconds.
</p>
<div class="fragment">
<hr>
<table style="margin:auto; background:white; padding: 5px;">
<thead>
<tr class="orange">
<th>#items</th>
<th>#seconds</th>
</tr>
</thead>
<tbody>
<tr>
<td>4</td>
<td>(4*10 + 4*30) = 160</td>
</tr>
<tr>
<td>8</td>
<td>(8*10 + 8*30) = 320</td>
</tr>
<tr>
<td>16</td>
<td>(16*10 + 16*30) = 640</td>
</tr>
</tbody>
</table>
<hr>
</div>
<p class="fragment">If N = items of clothing,<br> it takes (N*10 + N*30) => O(N).<br>
</p>
</section>
<section>
<img src="images/big-o-graph.png">
</section>
<section>
<h2>Laundry v3</h2>
<p><span class="red">Addition</span>: We sort our clothes as we hang them up in the closet.</p>
<pre><code>
piles = {'shirts': [], 'socks': []}
closet = {'sock_drawer': [], 'shirt_section': []}
for clothing_item in laundry:
piles[clothing_item.type].append(clothing_item)
for pile in piles:
for clothing_item in pile:
section = closet[clothing_item.closet_section]
for hung_clothing in section:
if clothing_item.color > hung_clothing.color:
clothing_item.hang_up()
</code></pre>
</section>
<section>
<img style="background-color:white;" src="images/clothing_sorting_example.svg">
</section>
<section>
<h2>Laundry v3</h2>
<p>Now we must loop through closet section items
<strong class="red">each time</strong> we hang up an item.<br>Each look at an item takes 2 seconds.
</p>
<table class="fragment" style="margin:auto; background:white; padding: 5px; font-size:smaller;">
<thead>
<tr>
<th>items</th>
<th>section</th>
<th>items</th>
<th>seconds</th>
</tr>
</thead>
<tbody>
<tr class="fragment">
<td>4</td>
<td>4</td>
<td>(4*10 + 4*30 + 4*4*2)</td>
<td>192</td>
</tr>
<tr class="fragment">
<td>8</td>
<td>8</td>
<td>(8*10 + 8*30 + 8*8*2)</td>
<td>448</td>
</tr>
<tr class="fragment">
<td>1024</td>
<td>1024</td>
<td>(1024*10 + 1024*30 + 1024*1024*2)</td>
<td>2138112</td>
</tr>
</tbody>
</table>
<br>
<p class="fragment">
If N = # items of clothing and # items in section,
<br>it takes (N*10 + N*30 + N*N*2) => O(N^2)
</p>
</section>
<section>
<img src="images/big-o-graph.png">
</section>
<section>
<h2>Time Complexity</h2>
<pre><table style="margin:auto; background:white; padding: 5px; font-size:smaller;">
<thead>
<tr>
<th>Order of growth</th>
<th>Name</th>
<th>Description</th>
<th>Example</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>constant</td>
<td>statement</td>
<td>add two numbers</td>
</tr>
<tr>
<td>logN</td>
<td>logarithmic</td>
<td>divide in half</td>
<td>binary search</td>
</tr>
<tr>
<td>N</td>
<td>linear</td>
<td>loop</td>
<td>find the maximum</td>
</tr>
<tr>
<td>NlogN</td>
<td>linearithmic</td>
<td>divide + conquer</td>
<td>merge sort</td>
</tr>
<tr>
<td>N<sup>2</sup></td>
<td>quadratic</td>
<td>double loop</td>
<td>check all pairs</td>
</tr>
<tr>
<td>N<sup>3</sup></td>
<td>cubic</td>
<td>triple loop</td>
<td>check all triples</td>
</tr>
<tr>
<td>2<sup>N</sup></td>
<td>exponential</td>
<td>exhaustive search</td>
<td>check all subsets</td>
</tr>
</table></pre>
</section>
<section>
<h1>Quiz Time!</h1>
</section>
<section>
<h3>What is the time complexity of...</h3>
<br>
<p>skipping to the front of a line</p>
<br>
<p>O(1) - Constant</p>
<p>O(n) - Linear</p>
<p>O(n^2) - Quadratic</p>
</section>
<section>
<h3>What is the time complexity of...</h3>
<br>
<p>waiting in a line</p>
<br>
<p>O(1) - Constant</p>
<p>O(n) - Linear</p>
<p>O(n^2) - Quadratic</p>
</section>
<section>
<h3>What is the time complexity of...</h3>
<br>
<p>skipping half of a line</p>
<br>
<p>O(1) - Constant</p>
<p>O(n) - Linear</p>
<p>O(n^2) - Quadratic</p>
</section>
<section>
<h3>What is the time complexity of...</h3>
<br>
<p>sorting your books in alphabetical order</p>
<br>
<p>O(1) - Constant</p>
<p>O(n) - Linear</p>
<p>O(n^2) - Quadratic</p>
</section>
<section>
<h3>What is the time complexity of...</h3>
<br>
<p>looking up an element in an array</p>
<pre><code>var things = ["raindrops", "roses", "whiskers", "kittens"]
things[2]
</code></pre>
<p>O(1) - Constant</p>
<p>O(n) - Linear</p>
<p>O(n^2) - Quadratic</p>
</section>
<section>
<h3>What is the time complexity of...</h3>
<br>
<p>iterating over an array</p>
<pre><code>var things = ["raindrops", "roses", "whiskers", "kittens"]
for (var i = 0; i < things.length; i++) {
things[i]
}
</code></pre>
<p>O(1) - Constant</p>
<p>O(n) - Linear</p>
<p>O(n^2) - Quadratic</p>
</section>
<section>
<h3>What is the time complexity of...</h3>
<br>
<p>making every pair combination of items in array</p>
<pre><code>var things = ["raindrops", "roses", "whiskers", "kittens"]
for (var i = 0; i < things.length; i++) {
for (var j = i + 1; j < things.length; j++) {
things[i] + things[j]
}
}
// raindrops + roses
// raindrops + whiskers
// raindrops + kittens
// roses + whiskers
// roses + kittens
// whiskers + kittens
</code></pre>
<p>O(1) - Constant</p>
<p>O(n) - Linear</p>
<p>O(n^2) - Quadratic</p>
</section>
<!-- END OF TIME COMPLEXITY -->
<!-- START OF SPACE COMPLEXITY -->
<section>
<h2>Space Complexity</h2>
</section>
<section>
<h3>Time Efficient Grocery Shopping</h3>
<br>
<ol>
<li>Drive car to grocery store</li>
<li>Get cart</li>
<li>Put everything in the cart</li>
<li>Buy items and drive home</li>
</ol>
</section>
<section>
<img style='width:60%;' src="images/time-grocery.png">
</section>
<section>
<img class='left' style='width:40%;' src="images/time-grocery.png">
<p class=''>The size of the cart grows as the number of items on the list grows.<br>
</p>
<p class='fragment'>If N is the number of items on the list, then the cart array needs to be size N.
</p>
<p class='fragment'>The space complexity is O(N)
</p>
<img class='fragment' src="images/time-grocery-results.png">
</section>
<section>
<h3>Space Efficient Grocery Shopping</h3>
<br>
<ol>
<li>Walk to grocery store</li>
<li>Buy first item on list</li>
<li>Walk home and drop off the item</li>
<li>Repeat until done</li>
</ol>
</section>
<section>
<img style='width:60%;' src="images/space-grocery.png">
</section>
<section>
<img class='left' style='width:40%;' src="images/space-grocery.png">
<img style='width:32%;' src="images/space-grocery-result.png">
<br>
<p class="fragment">If N is the number of items on the list, then the cart array needs to be size 1.
</p>
<p class="fragment">The space complexity is O(1) or constant.
</p>
</section>
<section>
<h4>What's more important? Time or space?</h4>
<img class="fragment" src="images/old-apple-computer.jpg">
</section>
<!-- Algorithms from the beginning of class!-->
<section>
<h2 class='highlight'>Discuss!</h2>
<p>What's the time requirement of your algorithm?</p>
<p>What's the space requirement of your algorithm?</p>
</section>
<section>
<h2 class='highlight'>Making Crepes</h2>
<p>Write two algorithms for making crepes, one that is time efficient and one that is space efficient. <br><br>Pseudocode first then whiteboard code if you have time.</p>
</section>
<section>
<img style="width:60%;" src="images/crepes-time.png">
<img style="width:40%;" src="images/crepes-time-results.png">
</section>
<section>
<img class='left' style="width:60%;" src="images/crepes-space.png">
<img style="width:30%;" src="images/crepes-space-results.png">
</section>
<!-- END OF SPACE COMPLEXITY -->
<!-- START OF SEARCHING -->
<section>
<h2>Searching Algorithms</h2>
<br>
<p>Find an item with a particular value in a sorted sequence.</p>
<br>
<p class="fragment">
Find 'J' in sequence:<br>
<code>[4, 5, J, 10, Q, K, A]</code>
</p>
<br>
<p class="fragment">
J is the 3rd element (index 2).
</p>
</section>
<section>
<h2>Binary Search</h2><br>
<p>Find an item by repeatedly halving the search space.</p><br>
<iframe width="420" height="315" src="http://www.youtube.com/embed/ube5EYFlFR0" frameborder="0" allowfullscreen></iframe>
</section>
<section>
<h2>Binary Search: Steps</h2><br>
<!-- todo wikipedia animation? -->
<p>To find the index of element <em>e</em> with a certain value:</p>
<ul>
<li>Start with an array sorted in descending order.
<li>In each step:
<ul>
<li>Pick the middle element of the array <em>m</em> and compare it to <em>e</em>.
<li>If element values are equal, then return index of <em>m</em>.
<li>If <em>e</em> is greater than <em>m</em>, then <em>e</em> must be in left subarray. If <em>m</em> is greater than <em>e</em>, then <em>e</em> must be in the right subarray.
</ul>
<li>Repeat those steps on new subarray.</li>
</ul>
</section>
<section>
<h2>Exercise:</h2>
<h3>Binary Search Simulation</h3>
<br>
<ul>
<li>Sort your cards by number.
<li>Now let's find a card with a particular number using binary search.
</ul>
</section>
<section>
<h2>Binary Search:</h2>
<h3>Pseudocode</h3>
<br>
<pre style="padding:10px;"><code>
def BinarySearch(array, value):
"""returns bool if value is in array"""
low = 0
high = len(array) - 1
while low < high:
mid = (low + high) / 2
if array[mid] > value:
high = mid - 1
elif array[mid] < value:
low = mid + 1
else:
return True
</code></pre>
python code
</section>
<section>
<h2>Binary Search:</h2>
<h3>Time/Space Complexity</h3>
<br>
<p class="fragment">What factors determine time?</p>
<br>
<p class="fragment">N = number of items in sequence.</p>
<br>
<p class="fragment">Since algorithm splits array in half every time,
at most <a href="http://stackoverflow.com/questions/8185079/how-to-calculate-binary-search-complexity">log<sub>2</sub>N</a> steps are performed.
</p>
</section>
<section>
<h2>Binary Search:</h2>
<h3>Visualized</h3>
<img src="images/binary-search.jpg">
</section>
<section>
<h2>Other Searching Algorithms</h2>
<br>
<p>Performance varies depending on sequence characteristics (distribution)
</p>
<br>
<ul>
<li><a href="http://en.wikipedia.org/wiki/Linear_search">Linear</a>
<li><a href="http://en.wikipedia.org/wiki/Interpolation_search">Interpolation search</a>
</ul>
</section>
<section>
<h2>Exercise Time!</h2>
<br>
<ul>
<li>Pick a search algorithm.</li>
<li>Play the number guessing game (higher/lower).</li>
<li>Try and guess the number in the smallest number of tries using
the principles of binary search.</li>
<li>Keep track of the number of guesses it takes you.</li>
<li>If you have time, repeat for different ranges (0-500, 0-1000, etc).</li>
</ul>
</section>
<!-- END OF SEARCHING -->
<!-- START OF SORTING ALGORITHMS -->
<section>
<h2>Sorting</h2>
</section>
<section>
<h2>Sorting</h2>
<span class='left'>
<p>Why so important?</p>
<br>
<p>You do it all the time in real life </p>
<ul>
<li class='fragment'>The way your clothes are organized</li>
<li class='fragment'>Where your dishes are in the kitchen</li>
<li class='fragment'>Your books on your bookshelf</li>
</ul></br></br>
<p class='fragment'>They're not in perfect order, but in a way that makes it easier for you to search for things</p>
</span>
</section>
<section>
<h3>Sorting</h3>
<span class='left'>
<p>Many sorting algorithms for many types of data</p><br>
<ul>
<li class='fragment'>I sort my dishes by size and shape, not alphabetically</li>
<li class='fragment'>I sort my books alphabetically, not by color</li>
<li class='fragment'>I sort post-its in reverse chronological order</li>
<li class='fragment'>When I sort my yarn I dump them all over the ground</li>
<li class='fragment'>When I sort my dishes I pull them out of the diswasher one at a time</li>
</ul></br></br>
<p class='fragment'>There are lots of different types of data computers need to sort, just like in the real world</p>
</span>
</section>
<section>
<img src='images/ruby-sort.png'>
</section>
<section>
<h2>Selection Sort</h2>
<ol>
<li>Iterate over the unsorted array, keeping track of the minimum value as you go</li><br>
<li>When you get to the end of the array, you know which element is the minimum</li><br>
<li>Swap the minimum element and the first element in the unsorted array</li><br>
<li>The first element is now considered sorted</li><br>
<li>Repeat until the rest of the array is sorted</li>
</ol>
</section>
<section>
<ol class='left'>
<li>Find minimum</li>
<li>Swap in front</li>
<li>Repeat with unsorted</li>
</ol>
<img style="border:none !important;width:95%;" src='images/cards0.png'>
</section>
<section>
<ol class='left'>
<li>Find minimum</li>
<li>Swap in front</li>
<li>Repeat with unsorted</li>
</ol>
<img style="border:none !important;width:95%;" src='images/cards1.png'>
</section>
<section>
<ol class='left'>
<li>Find minimum</li>
<li>Swap in front</li>
<li>Repeat with unsorted</li>
</ol>
<img style="border:none !important;width:95%;" src='images/cards2.png'>
</section>
<section>
<ol class='left'>
<li>Find minimum</li>
<li>Swap in front</li>
<li>Repeat with unsorted</li>
</ol>
<img style="border:none !important;width:95%;" src='images/cards3.png'>
</section>
<section>
<ol class='left'>
<li>Find minimum</li>
<li>Swap in front</li>
<li>Repeat with unsorted</li>
</ol>
<img style="border:none !important;width:95%;" src='images/cards4.png'>
</section>
<section>
<ol class='left'>
<li>Find minimum</li>
<li>Swap in front</li>
<li>Repeat with unsorted</li>
</ol>
<img style="border:none !important;width:95%;" src='images/cards5.png'>
</section>
<section>
<ol class='left'>
<li>Find minimum</li>
<li>Swap in front</li>
<li>Repeat with unsorted</li>
</ol><br>
<img style="border:none !important;width:95%;" src='images/cards6.png'>
</section>
<section>
<h3 class='highlight'> Selection Sort Simulation</h3>
<br>
<ul>
<li>Line up randomly.</li>
<li>Sort by [something secret] using selection sort.</li>
</ul>
</section>
<section>
<h3>Selection Sort:</h3>
<h3>Time Complexity</h3><br>
<span class='left'>
<p>What is the best case?</p>
<p>What is the worst case?</p><br>
<p class='fragment'>They are the same! No matter what, selection sort has a time complexity of O(N^2)</p>
</span>
</section>