Algorithms Summary - CS50 Shorts

CS50 · Intermediate ·💻 AI-Assisted Coding ·8y ago

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 Hello, World: Hadi Partovi
Hello, World: Hadi Partovi
CS50
2 Content Distribution and Archival in a Digital Age
Content Distribution and Archival in a Digital Age
CS50
3 CS50 2014 - Week 1
CS50 2014 - Week 1
CS50
4 CS50 2014 - Week 3
CS50 2014 - Week 3
CS50
5 CS50 2014 - Week 0, continued
CS50 2014 - Week 0, continued
CS50
6 CS50 2014 - Week 4
CS50 2014 - Week 4
CS50
7 Week 3, continued
Week 3, continued
CS50
8 Quiz 0 Review
Quiz 0 Review
CS50
9 CS50 2014 - Week 3, continued
CS50 2014 - Week 3, continued
CS50
10 CS50 2014 - Week 7
CS50 2014 - Week 7
CS50
11 CS50 2014 - Week 7, continued
CS50 2014 - Week 7, continued
CS50
12 Breaking Through The (Google) Glass Ceiling by Christopher Bartholomew
Breaking Through The (Google) Glass Ceiling by Christopher Bartholomew
CS50
13 Introduction to Amazon Web Services by Leo Zhadanovsky
Introduction to Amazon Web Services by Leo Zhadanovsky
CS50
14 CS50 2014 - Week 9
CS50 2014 - Week 9
CS50
15 How to Build Innovative Technologies by Abby Fichtner
How to Build Innovative Technologies by Abby Fichtner
CS50
16 Light Your World (with Hue Bulbs) by Dan Bradley
Light Your World (with Hue Bulbs) by Dan Bradley
CS50
17 Building Dynamic Web Apps with Laravel by Eric Ouyang
Building Dynamic Web Apps with Laravel by Eric Ouyang
CS50
18 CS50 2014 - CS50 Lecture by Steve Ballmer
CS50 2014 - CS50 Lecture by Steve Ballmer
CS50
19 CS50 2014 - Week 10
CS50 2014 - Week 10
CS50
20 This is CS50 with Steve Ballmer?
This is CS50 with Steve Ballmer?
CS50
21 Meteor: a better way to build apps by Roger Zurawicki
Meteor: a better way to build apps by Roger Zurawicki
CS50
22 Data Analysis in R by Dustin Tran
Data Analysis in R by Dustin Tran
CS50
23 Data Visualization and D3 by David Chouinard
Data Visualization and D3 by David Chouinard
CS50
24 CS50 2014 - Week 6
CS50 2014 - Week 6
CS50
25 Build Tomorrow's Library by Jeffrey Licht
Build Tomorrow's Library by Jeffrey Licht
CS50
26 CS50 2014 - Week 9, continued
CS50 2014 - Week 9, continued
CS50
27 Essential Scale-Out Computing by James Cuff
Essential Scale-Out Computing by James Cuff
CS50
28 iOS App Development with Swift by Dan Armendariz
iOS App Development with Swift by Dan Armendariz
CS50
29 Sam Clark Leads Yale Students on Tour to CS50 at Harvard
Sam Clark Leads Yale Students on Tour to CS50 at Harvard
CS50
30 3D Modeling and Manufacture by Ansel Duff
3D Modeling and Manufacture by Ansel Duff
CS50
31 CS50 2014 - Week 5, continued
CS50 2014 - Week 5, continued
CS50
32 hello, world
hello, world
CS50
33 CS50 2014 - Deep Thoughts - Hash Table
CS50 2014 - Deep Thoughts - Hash Table
CS50
34 CS50 2014 - Deep Thoughts - Binary Tree
CS50 2014 - Deep Thoughts - Binary Tree
CS50
35 CS50 2014 - Deep Thoughts - Scratch
CS50 2014 - Deep Thoughts - Scratch
CS50
36 CS50 2014 - Deep Thoughts - MySQL
CS50 2014 - Deep Thoughts - MySQL
CS50
37 LaunchCode Visits CS50
LaunchCode Visits CS50
CS50
38 CS50 Live, Episode 100
CS50 Live, Episode 100
CS50
39 CS50 Field Trip to Google
CS50 Field Trip to Google
CS50
40 This is CS50 AP
This is CS50 AP
CS50
41 Week 4: Monday - CS50 2011 - Harvard University
Week 4: Monday - CS50 2011 - Harvard University
CS50
42 Week 2: Wednesday - CS50 2011 - Harvard University
Week 2: Wednesday - CS50 2011 - Harvard University
CS50
43 Week 1: Wednesday - CS50 2011 - Harvard University
Week 1: Wednesday - CS50 2011 - Harvard University
CS50
44 Week 11: Monday - CS50 2011 - Harvard University
Week 11: Monday - CS50 2011 - Harvard University
CS50
45 Week 3: Wednesday - CS50 2011 - Harvard University
Week 3: Wednesday - CS50 2011 - Harvard University
CS50
46 Week 12: Monday - CS50 2011 - Harvard University
Week 12: Monday - CS50 2011 - Harvard University
CS50
47 Week 1: Friday - CS50 2011 - Harvard University
Week 1: Friday - CS50 2011 - Harvard University
CS50
48 Week 3: Monday - CS50 2011 - Harvard University
Week 3: Monday - CS50 2011 - Harvard University
CS50
49 Week 10: Wednesday - CS50 2011 - Harvard University
Week 10: Wednesday - CS50 2011 - Harvard University
CS50
50 Week 2: Monday - CS50 2011 - Harvard University
Week 2: Monday - CS50 2011 - Harvard University
CS50
51 Week 9: Monday - CS50 2011 - Harvard University
Week 9: Monday - CS50 2011 - Harvard University
CS50
52 Week 7: Monday - CS50 2011 - Harvard University
Week 7: Monday - CS50 2011 - Harvard University
CS50
53 Week 5: Monday - CS50 2011 - Harvard University
Week 5: Monday - CS50 2011 - Harvard University
CS50
54 Week 5: Wednesday - CS50 2011 - Harvard University
Week 5: Wednesday - CS50 2011 - Harvard University
CS50
55 Week 7: Wednesday - CS50 2011 - Harvard University
Week 7: Wednesday - CS50 2011 - Harvard University
CS50
56 Week 8: Monday - CS50 2011 - Harvard University
Week 8: Monday - CS50 2011 - Harvard University
CS50
57 Week 9: Wednesday - CS50 2011 - Harvard University
Week 9: Wednesday - CS50 2011 - Harvard University
CS50
58 Week 8: Wednesday - CS50 2011 - Harvard University
Week 8: Wednesday - CS50 2011 - Harvard University
CS50
59 Week 10: Monday - CS50 2011 - Harvard University
Week 10: Monday - CS50 2011 - Harvard University
CS50
60 Week 2: Wednesday - CS50 2010 - Harvard University
Week 2: Wednesday - CS50 2010 - Harvard University
CS50

Related Reads

📰
I built an open-source voice input layer that works across desktop apps
Learn how to integrate voice input into desktop apps with OpenTypeless, an open-source voice input layer
Dev.to AI
📰
I Built an AI-Powered 'Sarcastic Code Reviewer' (And how you can too!)
Learn to build an AI-powered sarcastic code reviewer by connecting a frontend webpage to an open-source AI model using JavaScript
Dev.to · Dedicated Coder
📰
The Open Source Counter-Strike: Running Local Coding Agents
Learn to run local coding agents for open-source Counter-Strike, streamlining software development with AI-powered tools
Medium · LLM
📰
AI Doesn’t Replace Creativity — It Removes Repetitive Work So Creators Can Focus on Better Ideas
AI enhances creativity by automating repetitive tasks, freeing creators to focus on innovative ideas
Medium · AI
Up next
We Studied 10,000 Devs Using AI. This Is Where They Fail.
SCALER
Watch →