Algorithms Summary - CS50 Shorts
Key Takeaways
Summarizes algorithms
Full Transcript
So in CS50 we learn about a variety of sorting and searching algorithms and sometimes it can be a little tricky to keep track of what algorithm does what. Um we've really only skimmed the surface too. There are many other searching and sorting algorithms. So in this video let's just take a a few minutes to try and distill each algorithm down to its core elements so you can remember the most important information about them and be able to articulate the differences if necessary. The first is selection sort. The basic idea behind selection sort is find the smallest unsorted element in an array and swap it with the first unsorted element of that array. We said that the worst case run time of that was n squared. And if you want to recall why take a look at the selection sort video. The best case run time is also n squared. Bubble sort, the idea behind that one is to swap adjacent pairs. So that's the key word that helps you remember the difference here. Selection sort is so far find the smallest. Bubble sort is swap adjacent pairs. We swap adjacent pairs of elements if they are out of order which effectively bubbles larger elements to the right. And at the same time it also begins to move smaller elements to the left. The worst case run time of bubble sort is n squared. The best case run time of bubble sort is n. Because in that situation we don't actually we might not need to make any swaps at all. We only have to make one pass across all n elements. In insertion sort the basic idea here is shifting. That's the key word for insertion sort. We're going to step once through the array from left to right. And as we do we're going to shift elements we've already seen to make room for smaller ones that need to fit somewhere back in that sorted portion. So we build the sorted array one element at a time left to right and we shift things to make room. The worst case run time of insertion sort is n squared. The best-case run time is n. Merge sort, the keyword here is split and merge. We split the full array, whether it's six elements, eight elements, 10,000 elements, we split it down by half, by half, by half until we have an array of n one element arrays, a set of n one element arrays. So, we started with one a thousand element array and we get to the point where we have a thousand one element arrays. Then we begin to merge those sub arrays back together in the correct order. So, we take two one element arrays and create a two element array. We take two two element arrays and create a four element array and so on and so on until we've again rebuilt one n element array. The worst-case run time of merge sort is n times log n. We have n elements, but this recombining process is takes log n steps to get back to a full array. The best-case run time is also n log n cuz this process doesn't really care whether the array was sorted or not to start with. The process is the same. There's no way to short-circuit things. So, n log n in the worst case, n log n in the best case. We also talked about two searching algorithms. So, linear search is about iterating. We proceed across the array once from left to right trying to find the number that we're looking for. The worst-case run time is big O of n. It might take us to iterating across every single element to find the element we're looking for either in the last position or not at all, but we can't confirm that until we've looked at everything. In the best case, we find it immediately. The best-case run time of linear search is omega of one. Lastly, there was binary search which requires a sorted array. Remember, that's a very important consideration when working with binary search. It's a prerequisite to using it. The array that you're looking through must be sorted. Otherwise, the is divide and conquer. Split the array into half and eliminate half of the elements every time as you proceed through. Because of this divide and conquer and splitting things in half approach, the worst-case runtime of binary search is log n, which is substantially better than linear search's n. The best-case runtime is still one. We might find it immediately the first time we make a division. But again, remember that although binary search is substantially better than linear search by being by virtue of being log n versus n, you might have to go through the work of sorting your array first, which might make it less effective depending on the size of the array being sorted. I'm Doug Lloyd. This is CS50.
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from CS50 · CS50 · 0 of 60
← Previous
Next →
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
Hello, World: Hadi Partovi
CS50
Content Distribution and Archival in a Digital Age
CS50
CS50 2014 - Week 1
CS50
CS50 2014 - Week 3
CS50
CS50 2014 - Week 0, continued
CS50
CS50 2014 - Week 4
CS50
Week 3, continued
CS50
Quiz 0 Review
CS50
CS50 2014 - Week 3, continued
CS50
CS50 2014 - Week 7
CS50
CS50 2014 - Week 7, continued
CS50
Breaking Through The (Google) Glass Ceiling by Christopher Bartholomew
CS50
Introduction to Amazon Web Services by Leo Zhadanovsky
CS50
CS50 2014 - Week 9
CS50
How to Build Innovative Technologies by Abby Fichtner
CS50
Light Your World (with Hue Bulbs) by Dan Bradley
CS50
Building Dynamic Web Apps with Laravel by Eric Ouyang
CS50
CS50 2014 - CS50 Lecture by Steve Ballmer
CS50
CS50 2014 - Week 10
CS50
This is CS50 with Steve Ballmer?
CS50
Meteor: a better way to build apps by Roger Zurawicki
CS50
Data Analysis in R by Dustin Tran
CS50
Data Visualization and D3 by David Chouinard
CS50
CS50 2014 - Week 6
CS50
Build Tomorrow's Library by Jeffrey Licht
CS50
CS50 2014 - Week 9, continued
CS50
Essential Scale-Out Computing by James Cuff
CS50
iOS App Development with Swift by Dan Armendariz
CS50
Sam Clark Leads Yale Students on Tour to CS50 at Harvard
CS50
3D Modeling and Manufacture by Ansel Duff
CS50
CS50 2014 - Week 5, continued
CS50
hello, world
CS50
CS50 2014 - Deep Thoughts - Hash Table
CS50
CS50 2014 - Deep Thoughts - Binary Tree
CS50
CS50 2014 - Deep Thoughts - Scratch
CS50
CS50 2014 - Deep Thoughts - MySQL
CS50
LaunchCode Visits CS50
CS50
CS50 Live, Episode 100
CS50
CS50 Field Trip to Google
CS50
This is CS50 AP
CS50
Week 4: Monday - CS50 2011 - Harvard University
CS50
Week 2: Wednesday - CS50 2011 - Harvard University
CS50
Week 1: Wednesday - CS50 2011 - Harvard University
CS50
Week 11: Monday - CS50 2011 - Harvard University
CS50
Week 3: Wednesday - CS50 2011 - Harvard University
CS50
Week 12: Monday - CS50 2011 - Harvard University
CS50
Week 1: Friday - CS50 2011 - Harvard University
CS50
Week 3: Monday - CS50 2011 - Harvard University
CS50
Week 10: Wednesday - CS50 2011 - Harvard University
CS50
Week 2: Monday - CS50 2011 - Harvard University
CS50
Week 9: Monday - CS50 2011 - Harvard University
CS50
Week 7: Monday - CS50 2011 - Harvard University
CS50
Week 5: Monday - CS50 2011 - Harvard University
CS50
Week 5: Wednesday - CS50 2011 - Harvard University
CS50
Week 7: Wednesday - CS50 2011 - Harvard University
CS50
Week 8: Monday - CS50 2011 - Harvard University
CS50
Week 9: Wednesday - CS50 2011 - Harvard University
CS50
Week 8: Wednesday - CS50 2011 - Harvard University
CS50
Week 10: Monday - CS50 2011 - Harvard University
CS50
Week 2: Wednesday - CS50 2010 - Harvard University
CS50
Related Reads
📰
📰
📰
📰
I built an open-source voice input layer that works across desktop apps
Dev.to AI
I Built an AI-Powered 'Sarcastic Code Reviewer' (And how you can too!)
Dev.to · Dedicated Coder
The Open Source Counter-Strike: Running Local Coding Agents
Medium · LLM
AI Doesn’t Replace Creativity — It Removes Repetitive Work So Creators Can Focus on Better Ideas
Medium · AI
🎓
Tutor Explanation
DeepCamp AI