-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path2021-02-27_A-Quick-Guide-to-Big-O-Notation--Memoization--Tabulation--and-Sorting-Algorithms-by-Example-803ff193c522.html
More file actions
629 lines (626 loc) · 47.3 KB
/
Copy path2021-02-27_A-Quick-Guide-to-Big-O-Notation--Memoization--Tabulation--and-Sorting-Algorithms-by-Example-803ff193c522.html
File metadata and controls
629 lines (626 loc) · 47.3 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
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>A Quick Guide to Big-O Notation, Memoization, Tabulation, and Sorting Algorithms by Example</title>
<link rel="stylesheet" href="./style.css">
</head>
<body>
<article class="h-entry">
<header>
<h1 class="p-name">A Quick Guide to Big-O Notation, Memoization, Tabulation, and Sorting Algorithms by Example
</h1>
</header>
<section data-field="subtitle" class="p-summary">
Curating Complexity: A Guide to Big-O Notation
</section>
<section data-field="body" class="e-content">
<section name="ac05" class="section section--body section--first">
<div class="section-divider">
<hr class="section-divider">
</div>
<div class="section-content">
<div class="section-inner sectionLayout--insetColumn">
<h3 name="794e" id="794e" class="graf graf--h3 graf--leading graf--title">A Quick Guide to Big-O Notation,
Memoization, Tabulation, and Sorting Algorithms by Example</h3>
<figure name="33dc" id="33dc" class="graf graf--figure graf-after--h3"><img class="graf-image"
data-image-id="0*yjlSk3T9c2_14in1.png" data-width="1000" data-height="704" data-is-featured="true"
src="https://cdn-images-1.medium.com/max/800/0*yjlSk3T9c2_14in1.png"></figure>
<p name="398e" id="398e" class="graf graf--p graf-after--figure"><strong
class="markup--strong markup--p-strong"><em class="markup--em markup--p-em">Curating Complexity: A
Guide to Big-O Notation</em></strong></p>
<ul class="postList">
<li name="b70f" id="b70f" class="graf graf--li graf-after--p">Why is looking at runtime not a reliable
method of calculating time complexity?</li>
<li name="2b21" id="2b21" class="graf graf--li graf-after--li">Not all computers are made equal( some
may be stronger and therefore boost our runtime speed )</li>
<li name="1e1a" id="1e1a" class="graf graf--li graf-after--li">How many background processes ran
concurrently with our program that was being tested?</li>
<li name="1cad" id="1cad" class="graf graf--li graf-after--li">We also need to ask if our code remains
performant if we increase the size of the input.</li>
<li name="3cb7" id="3cb7" class="graf graf--li graf-after--li">The real question we need to answering
is: <code class="markup--code markup--li-code">How does our performance scale?</code>.</li>
</ul>
<h3 name="5fc8" id="5fc8" class="graf graf--h3 graf-after--li">big ‘O’ notation</h3>
<ul class="postList">
<li name="9b21" id="9b21" class="graf graf--li graf-after--h3">Big O Notation is a tool for describing
the efficiency of algorithms with respect to the size of the input arguments.</li>
<li name="c0e6" id="c0e6" class="graf graf--li graf-after--li">Since we use mathematical functions in
Big-O, there are a few big picture ideas that we’ll want to keep in mind:</li>
<li name="2e86" id="2e86" class="graf graf--li graf-after--li">The function should be defined by the
size of the input.</li>
<li name="07b0" id="07b0" class="graf graf--li graf-after--li"><code
class="markup--code markup--li-code">Smaller</code> Big O is better (lower time complexity)</li>
<li name="f1b0" id="f1b0" class="graf graf--li graf-after--li">Big O is used to describe the worst case
scenario.</li>
<li name="e11f" id="e11f" class="graf graf--li graf-after--li">Big O is simplified to show only its most
dominant mathematical term.</li>
</ul>
<h3 name="6083" id="6083" class="graf graf--h3 graf-after--li">Simplifying Math Terms</h3>
<ul class="postList">
<li name="64a4" id="64a4" class="graf graf--li graf-after--h3">We can use the following rules to
simplify the our Big O functions:</li>
<li name="a2c2" id="a2c2" class="graf graf--li graf-after--li"><code
class="markup--code markup--li-code">Simplify Products</code> : If the function is a product of many
terms, we drop the terms that don't depend on n.</li>
<li name="b058" id="b058" class="graf graf--li graf-after--li"><code
class="markup--code markup--li-code">Simplify Sums</code> : If the function is a sum of many terms,
we drop the non-dominant terms.</li>
<li name="eb32" id="eb32" class="graf graf--li graf-after--li"><code
class="markup--code markup--li-code">n</code> : size of the input</li>
<li name="c042" id="c042" class="graf graf--li graf-after--li"><code
class="markup--code markup--li-code">T(f)</code> : unsimplified math function</li>
<li name="7b41" id="7b41" class="graf graf--li graf-after--li"><code
class="markup--code markup--li-code">O(f)</code> : simplified math function.</li>
</ul>
<p name="9e91" id="9e91" class="graf graf--p graf-after--li"><code
class="markup--code markup--p-code">Putting it all together</code></p>
<figure name="6e23" id="6e23" class="graf graf--figure graf-after--p"><img class="graf-image"
data-image-id="1*TT8uuv1x3nmGUw5rvtoZ8A.png" data-width="416" data-height="723"
src="https://cdn-images-1.medium.com/max/800/1*TT8uuv1x3nmGUw5rvtoZ8A.png"></figure>
<ul class="postList">
<li name="d18b" id="d18b" class="graf graf--li graf-after--figure">First we apply the product rule to
drop all constants.</li>
<li name="4335" id="4335" class="graf graf--li graf-after--li graf--trailing">Then we apply the sum rule
to select the single most dominant term.</li>
</ul>
</div>
</div>
</section>
<section name="7935" class="section section--body">
<div class="section-divider">
<hr class="section-divider">
</div>
<div class="section-content">
<div class="section-inner sectionLayout--insetColumn">
<h3 name="915e" id="915e" class="graf graf--h3 graf--leading">Complexity Classes</h3>
<p name="2b3e" id="2b3e" class="graf graf--p graf-after--h3">Common Complexity Classes</p>
<h4 name="e9a1" id="e9a1" class="graf graf--h4 graf-after--p">There are 7 major classes in Time Complexity
</h4>
<figure name="5465" id="5465" class="graf graf--figure graf-after--h4"><img class="graf-image"
data-image-id="1*6zKhmJoHkvDbrd8jfUDf3A.png" data-width="496" data-height="321"
src="https://cdn-images-1.medium.com/max/800/1*6zKhmJoHkvDbrd8jfUDf3A.png"></figure>
<h4 name="a022" id="a022" class="graf graf--h4 graf-after--figure"><code
class="markup--code markup--h4-code">O(1) Constant</code></h4>
<blockquote name="d1f0" id="d1f0" class="graf graf--blockquote graf-after--h4"><strong
class="markup--strong markup--blockquote-strong">The algorithm takes roughly the same number of steps
for any input size.</strong></blockquote>
<figure name="e677" id="e677" class="graf graf--figure graf--iframe graf-after--blockquote">
<script src="https://gist.github.com/eengineergz/91b823971e8faac788f38ff670efc19d#file-constant-js.js">
</script>
</figure>
<h4 name="81af" id="81af" class="graf graf--h4 graf-after--figure"><code
class="markup--code markup--h4-code">O(log(n)) Logarithmic</code></h4>
<blockquote name="0f76" id="0f76" class="graf graf--blockquote graf-after--h4"><strong
class="markup--strong markup--blockquote-strong">In most cases our hidden base of Logarithmic time is
2, log complexity algorithm’s will typically display ‘halving’ the size of the input (like binary
search!)</strong></blockquote>
<figure name="0bcd" id="0bcd" class="graf graf--figure graf--iframe graf-after--blockquote">
<script src="https://gist.github.com/eengineergz/a1e6dec81f0639818db7f9a8e76b3992.js"></script>
</figure>
<h4 name="be35" id="be35" class="graf graf--h4 graf-after--figure"><code
class="markup--code markup--h4-code">O(n) Linear</code></h4>
<blockquote name="59f1" id="59f1" class="graf graf--blockquote graf-after--h4"><strong
class="markup--strong markup--blockquote-strong">Linear algorithm’s will access each item of the input
“once”.</strong></blockquote>
<figure name="5243" id="5243" class="graf graf--figure graf--iframe graf-after--blockquote">
<script src="https://gist.github.com/eengineergz/cc953ba2bd6e1d6f524a6d8b297aad5b.js"></script>
</figure>
<h3 name="b965" id="b965" class="graf graf--h3 graf-after--figure"><code
class="markup--code markup--h3-code">O(nlog(n)) Log Linear Time</code></h3>
<blockquote name="90c3" id="90c3" class="graf graf--blockquote graf-after--h3"><strong
class="markup--strong markup--blockquote-strong">Combination of linear and logarithmic behavior, we
will see features from both classes.</strong></blockquote>
<blockquote name="1c92" id="1c92" class="graf graf--blockquote graf-after--blockquote">Algorithm’s that
are log-linear will use<strong class="markup--strong markup--blockquote-strong"> both recursion AND
iteration.</strong></blockquote>
<figure name="7d2b" id="7d2b" class="graf graf--figure graf--iframe graf-after--blockquote">
<script src="https://gist.github.com/eengineergz/e9bd6337c17f1623a4da088574ed0d8e.js"></script>
</figure>
<h3 name="fb10" id="fb10" class="graf graf--h3 graf-after--figure"><code
class="markup--code markup--h3-code">O(nc) Polynomial</code></h3>
<blockquote name="820a" id="820a" class="graf graf--blockquote graf-after--h3"><strong
class="markup--strong markup--blockquote-strong">C is a fixed constant.</strong></blockquote>
<figure name="7e11" id="7e11" class="graf graf--figure graf--iframe graf-after--blockquote">
<script src="https://gist.github.com/eengineergz/3e6096e66bac80b962435b7d873cdbe9.js"></script>
</figure>
<h3 name="2736" id="2736" class="graf graf--h3 graf-after--figure"><code
class="markup--code markup--h3-code">O(c^n) Exponential</code></h3>
<blockquote name="393f" id="393f" class="graf graf--blockquote graf-after--h3"><strong
class="markup--strong markup--blockquote-strong">C is now the number of recursive calls made in each
stack frame.</strong></blockquote>
<blockquote name="78e2" id="78e2" class="graf graf--blockquote graf-after--blockquote"><strong
class="markup--strong markup--blockquote-strong">Algorithm’s with exponential time are VERY
SLOW.</strong></blockquote>
<figure name="0881" id="0881"
class="graf graf--figure graf--iframe graf-after--blockquote graf--trailing">
<script src="https://gist.github.com/eengineergz/5dec7e3736d7b5e28a5f1c85b5b50705.js"></script>
</figure>
</div>
</div>
</section>
<section name="c3ad" class="section section--body">
<div class="section-divider">
<hr class="section-divider">
</div>
<div class="section-content">
<div class="section-inner sectionLayout--insetColumn">
<h3 name="715f" id="715f" class="graf graf--h3 graf--leading">Memoization</h3>
<ul class="postList">
<li name="b3b0" id="b3b0" class="graf graf--li graf-after--h3">Memoization : a design pattern used to
reduce the overall number of calculations that can occur in algorithms that use recursive strategies
to solve.</li>
<li name="2583" id="2583" class="graf graf--li graf-after--li">MZ stores the results of the sub-problems
in some other data structure, so that we can avoid duplicate calculations and only ‘solve’ each
problem once.</li>
<li name="65c9" id="65c9" class="graf graf--li graf-after--li">Two features that comprise memoization:
</li>
</ul>
<ol class="postList">
<li name="b2d2" id="b2d2" class="graf graf--li graf-after--li">FUNCTION MUST BE RECURSIVE.</li>
<li name="91a3" id="91a3" class="graf graf--li graf-after--li">Our additional Data Structure is usually
an object (we refer to it as our memo… or sometimes cache!)</li>
</ol>
<figure name="9f52" id="9f52" class="graf graf--figure graf-after--li"><img class="graf-image"
data-image-id="1*4U79jBMjU2wKE_tyYcD_3A.png" data-width="915" data-height="571"
src="https://cdn-images-1.medium.com/max/800/1*4U79jBMjU2wKE_tyYcD_3A.png"></figure>
<figure name="1681" id="1681" class="graf graf--figure graf-after--figure"><img class="graf-image"
data-image-id="1*Qh42KZgcCxmVt6WrTasCVw.png" data-width="910" data-height="636"
src="https://cdn-images-1.medium.com/max/800/1*Qh42KZgcCxmVt6WrTasCVw.png"></figure>
<h3 name="0763" id="0763" class="graf graf--h3 graf-after--figure">Memoizing Factorial</h3>
<figure name="1a08" id="1a08" class="graf graf--figure graf--iframe graf-after--h3">
<script
src="https://gist.github.com/eengineergz/0f92023740a44e3b41a0defb227ade37#file-memoizing-factorial-js.js">
</script>
</figure>
<p name="644f" id="644f" class="graf graf--p graf-after--figure">Our memo object is <em
class="markup--em markup--p-em">mapping</em> out our arguments of factorial to it’s return value.</p>
<ul class="postList">
<li name="854a" id="854a" class="graf graf--li graf-after--p">Keep in mind we didn’t improve the speed
of our algorithm.</li>
</ul>
<h3 name="c6ac" id="c6ac" class="graf graf--h3 graf-after--li">Memoizing Fibonacci</h3>
<figure name="9950" id="9950" class="graf graf--figure graf-after--h3"><img class="graf-image"
data-image-id="0*2XaPj7UGKZYFjYhb" data-width="760" data-height="452"
src="https://cdn-images-1.medium.com/max/800/0*2XaPj7UGKZYFjYhb"></figure>
<ul class="postList">
<li name="5be6" id="5be6" class="graf graf--li graf-after--figure">Our time complexity for Fibonacci
goes from O(2^n) to O(n) after applying memoization.</li>
</ul>
<h3 name="860d" id="860d" class="graf graf--h3 graf-after--li">The Memoization Formula</h3>
<blockquote name="654d" id="654d" class="graf graf--blockquote graf-after--h3"><em
class="markup--em markup--blockquote-em">Rules:</em></blockquote>
<ol class="postList">
<li name="b3f0" id="b3f0" class="graf graf--li graf-after--blockquote"><em
class="markup--em markup--li-em">Write the unoptimized brute force recursion (make sure it
works);</em></li>
<li name="b36e" id="b36e" class="graf graf--li graf-after--li"><em class="markup--em markup--li-em">Add
memo object as an additional argument .</em></li>
<li name="f81f" id="f81f" class="graf graf--li graf-after--li"><em class="markup--em markup--li-em">Add
a base case condition that returns the stored value if the function’s argument is in the memo.</em>
</li>
<li name="1b0f" id="1b0f" class="graf graf--li graf-after--li"><em
class="markup--em markup--li-em">Before returning the result of the recursive case, store it in the
memo as a value and make the function’s argument it’s key.</em></li>
</ol>
<h4 name="dad0" id="dad0" class="graf graf--h4 graf-after--li">Things to remember</h4>
<ol class="postList">
<li name="bc4a" id="bc4a" class="graf graf--li graf-after--h4"><em class="markup--em markup--li-em">When
solving DP problems with Memoization, it is helpful to draw out the visual tree first.</em></li>
<li name="7bb1" id="7bb1" class="graf graf--li graf-after--li"><em class="markup--em markup--li-em">When
you notice duplicate sub-tree’s that means we can memoize.</em></li>
</ol>
<figure name="7877" id="7877" class="graf graf--figure graf--iframe graf-after--li graf--trailing">
<script src="https://gist.github.com/eengineergz/c15feb228a51a3543625009c8fd0b6de.js"></script>
</figure>
</div>
</div>
</section>
<section name="f862" class="section section--body">
<div class="section-divider">
<hr class="section-divider">
</div>
<div class="section-content">
<div class="section-inner sectionLayout--insetColumn">
<h3 name="c4a7" id="c4a7" class="graf graf--h3 graf--leading">Tabulation</h3>
<h4 name="d926" id="d926" class="graf graf--h4 graf-after--h3">Tabulation Strategy</h4>
<blockquote name="a701" id="a701" class="graf graf--blockquote graf-after--h4">Use When:</blockquote>
<ul class="postList">
<li name="f5b0" id="f5b0" class="graf graf--li graf-after--blockquote"><strong
class="markup--strong markup--li-strong">The function is iterative and not recursive.</strong></li>
<li name="015c" id="015c" class="graf graf--li graf-after--li"><em class="markup--em markup--li-em">The
accompanying DS is usually an array.</em></li>
</ul>
<figure name="db73" id="db73" class="graf graf--figure graf--iframe graf-after--li">
<script src="https://gist.github.com/eengineergz/a57bf449f5a8b16eedd1aa9fd71707e2.js"></script>
</figure>
<h4 name="e9d4" id="e9d4" class="graf graf--h4 graf-after--figure">Steps for tabulation</h4>
<ul class="postList">
<li name="8918" id="8918" class="graf graf--li graf-after--h4"><em
class="markup--em markup--li-em">Create a table array based off the size of the input.</em></li>
<li name="b4e7" id="b4e7" class="graf graf--li graf-after--li"><em
class="markup--em markup--li-em">Initialize some values in the table to ‘answer’ the trivially small
subproblem.</em></li>
<li name="072e" id="072e" class="graf graf--li graf-after--li"><em
class="markup--em markup--li-em">Iterate through the array and fill in the remaining entries.</em>
</li>
<li name="192e" id="192e" class="graf graf--li graf-after--li graf--trailing"><em
class="markup--em markup--li-em">Your final answer is usually the last entry in the table.</em></li>
</ul>
</div>
</div>
</section>
<section name="fc52" class="section section--body">
<div class="section-divider">
<hr class="section-divider">
</div>
<div class="section-content">
<div class="section-inner sectionLayout--insetColumn">
<h3 name="7366" id="7366" class="graf graf--h3 graf--leading">Memo and Tab Demo with Fibonacci</h3>
<blockquote name="bf6c" id="bf6c" class="graf graf--blockquote graf-after--h3"><em
class="markup--em markup--blockquote-em">Normal Recursive Fibonacci</em></blockquote>
<pre name="8ad4" id="8ad4"
class="graf graf--pre graf-after--blockquote">function fibonacci(n) {<br> if (n <= 2) return 1;<br> return fibonacci(n - 1) + fibonacci(n - 2);<br>}</pre>
<blockquote name="0fee" id="0fee" class="graf graf--blockquote graf-after--pre"><em
class="markup--em markup--blockquote-em">Memoization Fibonacci 1</em></blockquote>
<figure name="8b3d" id="8b3d" class="graf graf--figure graf--iframe graf-after--blockquote">
<script src="https://gist.github.com/eengineergz/504a9120ca40bbb4a246549937c43a12.js"></script>
</figure>
<blockquote name="9fa9" id="9fa9" class="graf graf--blockquote graf-after--figure"><em
class="markup--em markup--blockquote-em">Memoization Fibonacci 2</em></blockquote>
<figure name="9c13" id="9c13" class="graf graf--figure graf--iframe graf-after--blockquote">
<script src="https://gist.github.com/eengineergz/07d315d92b3458a8640cee31bce9c236.js"></script>
</figure>
<blockquote name="7002" id="7002" class="graf graf--blockquote graf-after--figure"><em
class="markup--em markup--blockquote-em">Tabulated Fibonacci</em></blockquote>
<figure name="4ba2" id="4ba2" class="graf graf--figure graf--iframe graf-after--blockquote">
<script src="https://gist.github.com/eengineergz/b1b1f7e259193ecdc432350b6199f2d3.js"></script>
</figure>
<h3 name="a8cd" id="a8cd" class="graf graf--h3 graf-after--figure">Example of Linear Search</h3>
<figure name="4301" id="4301" class="graf graf--figure graf--iframe graf-after--h3">
<script src="https://gist.github.com/eengineergz/e98354b287ce2f80da4ab943399eb555.js"></script>
</figure>
<ul class="postList">
<li name="84b2" id="84b2" class="graf graf--li graf-after--figure"><em
class="markup--em markup--li-em">Worst Case Scenario: The term does not even exist in the
array.</em></li>
<li name="30dc" id="30dc" class="graf graf--li graf-after--li graf--trailing"><em
class="markup--em markup--li-em">Meaning: If it doesn’t exist then our for loop would run until the
end therefore making our time complexity O(n).</em></li>
</ul>
</div>
</div>
</section>
<section name="87aa" class="section section--body section--last">
<div class="section-divider">
<hr class="section-divider">
</div>
<div class="section-content">
<div class="section-inner sectionLayout--insetColumn">
<h3 name="5bf0" id="5bf0" class="graf graf--h3 graf--leading">Sorting Algorithms</h3>
<h3 name="ad34" id="ad34" class="graf graf--h3 graf-after--h3">Bubble Sort</h3>
<p name="dfab" id="dfab" class="graf graf--p graf-after--h3"><code
class="markup--code markup--p-code">Time Complexity</code>: Quadratic O(n^2)</p>
<ul class="postList">
<li name="ce1e" id="ce1e" class="graf graf--li graf-after--p">The inner for-loop contributes to O(n),
however in a worst case scenario the while loop will need to run n times before bringing all n
elements to their final resting spot.</li>
</ul>
<p name="96e2" id="96e2" class="graf graf--p graf-after--li"><code
class="markup--code markup--p-code">Space Complexity</code>: O(1)</p>
<ul class="postList">
<li name="664f" id="664f" class="graf graf--li graf-after--p">Bubble Sort will always use the same
amount of memory regardless of n.</li>
</ul>
<figure name="24a9" id="24a9" class="graf graf--figure graf-after--li"><img class="graf-image"
data-image-id="0*Ck9aeGY-d5tbz7dT" data-width="901" data-height="535"
src="https://cdn-images-1.medium.com/max/800/0*Ck9aeGY-d5tbz7dT"></figure>
<figure name="fa7c" id="fa7c" class="graf graf--figure graf--iframe graf-after--figure">
<script src="https://gist.github.com/eengineergz/e67e56bed7c5a20a54851867ba5efef6.js"></script>
</figure>
<ul class="postList">
<li name="4115" id="4115" class="graf graf--li graf-after--figure">The first major sorting algorithm one
learns in introductory programming courses.</li>
<li name="ecd4" id="ecd4" class="graf graf--li graf-after--li">Gives an intro on how to convert unsorted
data into sorted data.</li>
</ul>
<blockquote name="a636" id="a636" class="graf graf--blockquote graf-after--li">It’s almost never used in
production code because:</blockquote>
<ul class="postList">
<li name="3cb1" id="3cb1" class="graf graf--li graf-after--blockquote"><em
class="markup--em markup--li-em">It’s not efficient</em></li>
<li name="4eac" id="4eac" class="graf graf--li graf-after--li"><em class="markup--em markup--li-em">It’s
not commonly used</em></li>
<li name="d730" id="d730" class="graf graf--li graf-after--li"><em
class="markup--em markup--li-em">There is stigma attached to it</em></li>
<li name="8da7" id="8da7" class="graf graf--li graf-after--li"><code
class="markup--code markup--li-code"><em class="markup--em markup--li-em">Bubbling Up</em></code><em
class="markup--em markup--li-em"> : Term that infers that an item is in motion, moving in some
direction, and has some final resting destination.</em></li>
<li name="8447" id="8447" class="graf graf--li graf-after--li"><em
class="markup--em markup--li-em">Bubble sort, sorts an array of integers by bubbling the largest
integer to the top.</em></li>
</ul>
<figure name="e086" id="e086" class="graf graf--figure graf--iframe graf-after--li">
<script src="https://gist.github.com/eengineergz/fd4acc0c89033bd219ebf9d3ec40b053.js"></script>
</figure>
<figure name="a10d" id="a10d" class="graf graf--figure graf--iframe graf-after--figure">
<script src="https://gist.github.com/eengineergz/80934783c628c70ac2a5a48119a82d54.js"></script>
</figure>
<ul class="postList">
<li name="dcd2" id="dcd2" class="graf graf--li graf-after--figure"><em
class="markup--em markup--li-em">Worst Case & Best Case are always the same because it makes
nested loops.</em></li>
<li name="9a6a" id="9a6a" class="graf graf--li graf-after--li"><em
class="markup--em markup--li-em">Double for loops are polynomial time complexity or more
specifically in this case Quadratic (Big O) of: O(n²)</em></li>
</ul>
<h3 name="dda6" id="dda6" class="graf graf--h3 graf-after--li">Selection Sort</h3>
<p name="f82a" id="f82a" class="graf graf--p graf-after--h3"><code
class="markup--code markup--p-code">Time Complexity</code>: Quadratic O(n^2)</p>
<ul class="postList">
<li name="646d" id="646d" class="graf graf--li graf-after--p">Our outer loop will contribute O(n) while
the inner loop will contribute O(n / 2) on average. Because our loops are nested we will get O(n²);
</li>
</ul>
<p name="89a7" id="89a7" class="graf graf--p graf-after--li"><code
class="markup--code markup--p-code">Space Complexity</code>: O(1)</p>
<ul class="postList">
<li name="45ae" id="45ae" class="graf graf--li graf-after--p">Selection Sort will always use the same
amount of memory regardless of n.</li>
</ul>
<figure name="134a" id="134a" class="graf graf--figure graf-after--li"><img class="graf-image"
data-image-id="0*AByxtBjFrPVVYmyu" data-width="604" data-height="737"
src="https://cdn-images-1.medium.com/max/800/0*AByxtBjFrPVVYmyu"></figure>
<figure name="0cd4" id="0cd4" class="graf graf--figure graf--iframe graf-after--figure">
<script src="https://gist.github.com/eengineergz/4abc0fe0bf01599b0c4104b0ba633402.js"></script>
</figure>
<ul class="postList">
<li name="c618" id="c618" class="graf graf--li graf-after--figure">Selection sort organizes the smallest
elements to the start of the array.</li>
</ul>
<figure name="3ed0" id="3ed0" class="graf graf--figure graf-after--li"><img class="graf-image"
data-image-id="0*GeYNxlRcbt2cf0rY" data-width="640" data-height="262"
src="https://cdn-images-1.medium.com/max/800/0*GeYNxlRcbt2cf0rY"></figure>
<blockquote name="fcaa" id="fcaa" class="graf graf--blockquote graf-after--figure">Summary of how
Selection Sort should work:</blockquote>
<ol class="postList">
<li name="2277" id="2277" class="graf graf--li graf-after--blockquote"><em
class="markup--em markup--li-em">Set MIN to location 0</em></li>
<li name="c76c" id="c76c" class="graf graf--li graf-after--li"><em
class="markup--em markup--li-em">Search the minimum element in the list.</em></li>
<li name="79d3" id="79d3" class="graf graf--li graf-after--li"><em class="markup--em markup--li-em">Swap
with value at location Min</em></li>
<li name="4ede" id="4ede" class="graf graf--li graf-after--li"><em
class="markup--em markup--li-em">Increment Min to point to next element.</em></li>
<li name="a649" id="a649" class="graf graf--li graf-after--li"><em
class="markup--em markup--li-em">Repeat until list is sorted.</em></li>
</ol>
<figure name="72bb" id="72bb" class="graf graf--figure graf--iframe graf-after--li">
<script src="https://gist.github.com/eengineergz/61f130c8e0097572ed908fe2629bdee0.js"></script>
</figure>
<h3 name="1ce8" id="1ce8" class="graf graf--h3 graf-after--figure">Insertion Sort</h3>
<p name="cafe" id="cafe" class="graf graf--p graf-after--h3"><code
class="markup--code markup--p-code">Time Complexity</code>: Quadratic O(n^2)</p>
<ul class="postList">
<li name="95ea" id="95ea" class="graf graf--li graf-after--p">Our outer loop will contribute O(n) while
the inner loop will contribute O(n / 2) on average. Because our loops are nested we will get O(n²);
</li>
</ul>
<p name="119f" id="119f" class="graf graf--p graf-after--li"><code
class="markup--code markup--p-code">Space Complexity</code>: O(n)</p>
<ul class="postList">
<li name="f6fa" id="f6fa" class="graf graf--li graf-after--p">Because we are creating a subArray for
each element in the original input, our Space Comlexity becomes linear.</li>
</ul>
<figure name="5242" id="5242" class="graf graf--figure graf-after--li"><img class="graf-image"
data-image-id="0*gbNU6wrszGPrfAZG" data-width="562" data-height="569"
src="https://cdn-images-1.medium.com/max/800/0*gbNU6wrszGPrfAZG"></figure>
<figure name="b8fa" id="b8fa" class="graf graf--figure graf--iframe graf-after--figure">
<script src="https://gist.github.com/eengineergz/a9f4b8596c7546ac92746db659186d8c.js"></script>
</figure>
<h3 name="377f" id="377f" class="graf graf--h3 graf-after--figure">Merge Sort</h3>
<p name="4b0d" id="4b0d" class="graf graf--p graf-after--h3"><code
class="markup--code markup--p-code">Time Complexity</code>: Log Linear O(nlog(n))</p>
<ul class="postList">
<li name="44b2" id="44b2" class="graf graf--li graf-after--p">Since our array gets split in half every
single time we contribute O(log(n)). The while loop contained in our helper merge function contributes
O(n) therefore our time complexity is O(nlog(n)); <code class="markup--code markup--li-code">Space
Complexity</code>: O(n)</li>
<li name="9a83" id="9a83" class="graf graf--li graf-after--li">We are linear O(n) time because we are
creating subArrays.</li>
</ul>
<figure name="526a" id="526a" class="graf graf--figure graf-after--li"><img class="graf-image"
data-image-id="0*GeU8YwwCoK8GiSTD" data-width="510" data-height="500"
src="https://cdn-images-1.medium.com/max/800/0*GeU8YwwCoK8GiSTD"></figure>
<figure name="cc87" id="cc87" class="graf graf--figure graf-after--figure"><img class="graf-image"
data-image-id="0*IxqGb72XDVDeeiMl" data-width="618" data-height="595"
src="https://cdn-images-1.medium.com/max/800/0*IxqGb72XDVDeeiMl"></figure>
<h3 name="3aa9" id="3aa9" class="graf graf--h3 graf-after--figure">Example of Merge Sort</h3>
<figure name="5d54" id="5d54" class="graf graf--figure graf--iframe graf-after--h3">
<script src="https://gist.github.com/eengineergz/18fbb7edc9f5c4820ccfcecacf3c5e48.js"></script>
</figure>
<figure name="157f" id="157f" class="graf graf--figure graf--iframe graf-after--figure">
<script src="https://gist.github.com/eengineergz/cbb533137a7f957d3bc4077395c1ff64.js"></script>
</figure>
<figure name="5f13" id="5f13" class="graf graf--figure graf-after--figure"><img class="graf-image"
data-image-id="0*HMCR--9niDt5zY6M" data-width="510" data-height="500"
src="https://cdn-images-1.medium.com/max/800/0*HMCR--9niDt5zY6M"></figure>
<ul class="postList">
<li name="48b4" id="48b4" class="graf graf--li graf-after--figure"><strong
class="markup--strong markup--li-strong">Merge sort is O(nlog(n)) time.</strong></li>
<li name="c598" id="c598" class="graf graf--li graf-after--li"><em class="markup--em markup--li-em">We
need a function for merging and a function for sorting.</em></li>
</ul>
<blockquote name="4eb4" id="4eb4" class="graf graf--blockquote graf-after--li">Steps:</blockquote>
<ol class="postList">
<li name="213f" id="213f" class="graf graf--li graf-after--blockquote"><em
class="markup--em markup--li-em">If there is only one element in the list, it is already sorted;
return the array.</em></li>
<li name="6214" id="6214" class="graf graf--li graf-after--li"><em
class="markup--em markup--li-em">Otherwise, divide the list recursively into two halves until it can
no longer be divided.</em></li>
<li name="3cc8" id="3cc8" class="graf graf--li graf-after--li"><em
class="markup--em markup--li-em">Merge the smallest lists into new list in a sorted order.</em></li>
</ol>
<h3 name="b50b" id="b50b" class="graf graf--h3 graf-after--li">Quick Sort</h3>
<p name="7e8d" id="7e8d" class="graf graf--p graf-after--h3"><code
class="markup--code markup--p-code">Time Complexity</code>: Quadratic O(n^2)</p>
<ul class="postList">
<li name="8e34" id="8e34" class="graf graf--li graf-after--p">Even though the average time complexity
O(nLog(n)), the worst case scenario is always quadratic.</li>
</ul>
<p name="060c" id="060c" class="graf graf--p graf-after--li"><code
class="markup--code markup--p-code">Space Complexity</code>: O(n)</p>
<ul class="postList">
<li name="626b" id="626b" class="graf graf--li graf-after--p">Our space complexity is linear O(n)
because of the partition arrays we create.</li>
<li name="7e3a" id="7e3a" class="graf graf--li graf-after--li">QS is another Divide and Conquer
strategy.</li>
<li name="233d" id="233d" class="graf graf--li graf-after--li">Some key ideas to keep in mind:</li>
<li name="1173" id="1173" class="graf graf--li graf-after--li">It is easy to sort elements of an array
relative to a particular target value.</li>
<li name="8634" id="8634" class="graf graf--li graf-after--li">An array of 0 or 1 elements is already
trivially sorted.</li>
</ul>
<figure name="4dd0" id="4dd0" class="graf graf--figure graf-after--li"><img class="graf-image"
data-image-id="0*WLl_HpdBGXYx284T" data-width="608" data-height="272"
src="https://cdn-images-1.medium.com/max/800/0*WLl_HpdBGXYx284T"></figure>
<figure name="10ba" id="10ba" class="graf graf--figure graf-after--figure"><img class="graf-image"
data-image-id="0*-LyHJXGPTYsWLDZf" data-width="757" data-height="788"
src="https://cdn-images-1.medium.com/max/800/0*-LyHJXGPTYsWLDZf"></figure>
<figure name="84ff" id="84ff" class="graf graf--figure graf--iframe graf-after--figure">
<script src="https://gist.github.com/eengineergz/24bcbc5248a8c4e1671945e9512da57e.js"></script>
</figure>
<h3 name="a2ba" id="a2ba" class="graf graf--h3 graf-after--figure">Binary Search</h3>
<p name="bd8a" id="bd8a" class="graf graf--p graf-after--h3"><code
class="markup--code markup--p-code">Time Complexity</code>: Log Time O(log(n))</p>
<p name="3071" id="3071" class="graf graf--p graf-after--p"><code
class="markup--code markup--p-code">Space Complexity</code>: O(1)</p>
<figure name="4123" id="4123" class="graf graf--figure graf-after--p"><img class="graf-image"
data-image-id="0*-naVYGTXzE2Yoali" data-width="1600" data-height="891"
src="https://cdn-images-1.medium.com/max/800/0*-naVYGTXzE2Yoali"></figure>
<blockquote name="7ab6" id="7ab6" class="graf graf--blockquote graf-after--figure"><em
class="markup--em markup--blockquote-em">Recursive Solution</em></blockquote>
<figure name="0b31" id="0b31" class="graf graf--figure graf--iframe graf-after--blockquote">
<script src="https://gist.github.com/eengineergz/c82c00a4bcba4b69b7d326d6cad3ac8c.js"></script>
</figure>
<blockquote name="e60f" id="e60f" class="graf graf--blockquote graf-after--figure"><em
class="markup--em markup--blockquote-em">Min Max Solution</em></blockquote>
<figure name="bbc5" id="bbc5" class="graf graf--figure graf--iframe graf-after--blockquote">
<script src="https://gist.github.com/eengineergz/eb8d1e1684db15cc2c8af28e13f38751.js"></script>
</figure>
<figure name="d869" id="d869" class="graf graf--figure graf--iframe graf-after--figure">
<script src="https://gist.github.com/eengineergz/bc3f576b9795ccef12a108e36f9f820a.js"></script>
</figure>
<ul class="postList">
<li name="6fb1" id="6fb1" class="graf graf--li graf-after--figure"><em
class="markup--em markup--li-em">Must be conducted on a sorted array.</em></li>
<li name="383b" id="383b" class="graf graf--li graf-after--li"><em
class="markup--em markup--li-em">Binary search is logarithmic time, not exponential b/c n is cut
down by two, not growing.</em></li>
<li name="c940" id="c940" class="graf graf--li graf-after--li"><em
class="markup--em markup--li-em">Binary Search is part of Divide and Conquer.</em></li>
</ul>
<h3 name="d7dd" id="d7dd" class="graf graf--h3 graf-after--li">Insertion Sort</h3>
<ul class="postList">
<li name="26b7" id="26b7" class="graf graf--li graf-after--h3"><strong
class="markup--strong markup--li-strong">Works by building a larger and larger sorted region at the
left-most end of the array.</strong></li>
</ul>
<blockquote name="0f8a" id="0f8a" class="graf graf--blockquote graf-after--li">Steps:</blockquote>
<ol class="postList">
<li name="8c1f" id="8c1f" class="graf graf--li graf-after--blockquote"><em
class="markup--em markup--li-em">If it is the first element, and it is already sorted; return
1.</em></li>
<li name="1451" id="1451" class="graf graf--li graf-after--li"><em class="markup--em markup--li-em">Pick
next element.</em></li>
<li name="0f8b" id="0f8b" class="graf graf--li graf-after--li"><em
class="markup--em markup--li-em">Compare with all elements in the sorted sub list</em></li>
<li name="4d78" id="4d78" class="graf graf--li graf-after--li"><em
class="markup--em markup--li-em">Shift all the elements in the sorted sub list that is greater than
the value to be sorted.</em></li>
<li name="9131" id="9131" class="graf graf--li graf-after--li"><em
class="markup--em markup--li-em">Insert the value</em></li>
<li name="6c8a" id="6c8a" class="graf graf--li graf-after--li"><em
class="markup--em markup--li-em">Repeat until list is sorted.</em></li>
</ol>
<figure name="4938" id="4938" class="graf graf--figure graf--iframe graf-after--li">
<script src="https://gist.github.com/eengineergz/ffead1de0836c4bcc6445780a604f617.js"></script>
</figure>
<h3 name="3695" id="3695" class="graf graf--h3 graf-after--figure">If you found this guide helpful feel
free to checkout my GitHub/gists where I host similar content:</h3>
<div name="1c2f" id="1c2f" class="graf graf--mixtapeEmbed graf-after--h3"><a
href="https://gist.github.com/bgoonz" data-href="https://gist.github.com/bgoonz"
class="markup--anchor markup--mixtapeEmbed-anchor" title="https://gist.github.com/bgoonz"><strong
class="markup--strong markup--mixtapeEmbed-strong">bgoonz’s gists</strong><br><em
class="markup--em markup--mixtapeEmbed-em">Instantly share code, notes, and snippets. Web Developer,
Electrical Engineer JavaScript | CSS | Bootstrap | Python |…</em>gist.github.com</a><a
href="https://gist.github.com/bgoonz" class="js-mixtapeImage mixtapeImage u-ignoreBlock"
data-media-id="ab25adbb500306703daab23d08a7739a" data-thumbnail-img-id="0*3O67jrqm3EHjTK2H"
style="background-image: url(https://cdn-images-1.medium.com/fit/c/160/160/0*3O67jrqm3EHjTK2H);"></a>
</div>
<div name="3585" id="3585" class="graf graf--mixtapeEmbed graf-after--mixtapeEmbed"><a
href="https://github.com/bgoonz" data-href="https://github.com/bgoonz"
class="markup--anchor markup--mixtapeEmbed-anchor" title="https://github.com/bgoonz"><strong
class="markup--strong markup--mixtapeEmbed-strong">bgoonz — Overview</strong><br><em
class="markup--em markup--mixtapeEmbed-em">Web Developer, Electrical Engineer JavaScript | CSS |
Bootstrap | Python | React | Node.js | Express | Sequelize…</em>github.com</a><a
href="https://github.com/bgoonz" class="js-mixtapeImage mixtapeImage u-ignoreBlock"
data-media-id="6ee74d5200d495ddc7ddad0c92bd6dce" data-thumbnail-img-id="0*Udg3rbeFyslZ9dyl"
style="background-image: url(https://cdn-images-1.medium.com/fit/c/160/160/0*Udg3rbeFyslZ9dyl);"></a>
</div>
<h3 name="cb1a" id="cb1a" class="graf graf--h3 graf-after--mixtapeEmbed">Or Checkout my personal
Resource Site:</h3>
<div name="8a50" id="8a50" class="graf graf--mixtapeEmbed graf-after--h3"><a
href="https://web-dev-resource-hub.netlify.app/" data-href="https://web-dev-resource-hub.netlify.app/"
class="markup--anchor markup--mixtapeEmbed-anchor"
title="https://web-dev-resource-hub.netlify.app/"><strong
class="markup--strong markup--mixtapeEmbed-strong">a/A-Student-Resources</strong><br><em
class="markup--em markup--mixtapeEmbed-em">Edit
description</em>web-dev-resource-hub.netlify.app</a><a
href="https://web-dev-resource-hub.netlify.app/"
class="js-mixtapeImage mixtapeImage mixtapeImage--empty u-ignoreBlock"
data-media-id="142b348a1c3b7cab095decda3afd6236"></a></div>
<figure name="c5db" id="c5db" class="graf graf--figure graf-after--mixtapeEmbed"><img class="graf-image"
data-image-id="1*VCmj_H9AHs41oC9Yx1hZFQ.png" data-width="1228" data-height="674"
src="https://cdn-images-1.medium.com/max/800/1*VCmj_H9AHs41oC9Yx1hZFQ.png"></figure>
<figure name="df8c" id="df8c" class="graf graf--figure graf--iframe graf-after--figure graf--trailing">
<script src="https://gist.github.com/bgoonz/af844eda5a20b0fdc0b813304401602b.js"></script>
</figure>
</div>
</div>
</section>
</section>
<footer>
<p>By <a href="https://medium.com/@bryanguner" class="p-author h-card">Bryan Guner</a> on <a
href="https://medium.com/p/803ff193c522"><time class="dt-published"
datetime="2021-02-27T05:38:43.274Z">February 27, 2021</time></a>.</p>
<p><a
href="https://medium.com/@bryanguner/a-quick-guide-to-big-o-notation-memoization-tabulation-and-sorting-algorithms-by-example-803ff193c522"
class="p-canonical">Canonical link</a></p>
<p>Exported from <a href="https://medium.com">Medium</a> on April 3, 2021.</p>
</footer>
</article>
</body>
</html>