Download presentation
Presentation is loading. Please wait.
1
Week 9 - Monday CS 113
2
Last time What did we talk about last time? List examples
split() method for strings Lab 8
3
Questions?
4
Project 3
5
Algorithms
6
What is an algorithm? An algorithm is a finite sequence of steps you can follow to solve a problem Long division is a good example You can follow the steps and divide even very large numbers Algorithms are independent of programming languages One algorithm could be implemented in many different languages
7
Peanut butter jelly time
Describe an algorithm for making a peanut butter and jelly sandwich What is the right level of detail?
8
Theoretical computer science
The theoretical branch of computer science worries a lot about algorithms How can we find an algorithm that solves a problem? How do we know if it's the best one? Will the algorithm run in a reasonable amount of time on current hardware?
9
Searching
10
Searching for a number Lets say that I give you a list of numbers, and I ask you, “Is 37 on this list?” As a human, you have no problem answering this question, as long as the list is reasonably short What if you have to write a Python function to find some number?
11
Search algorithm Easy! We just look through every element in the list until we find it or run out If we find it, we return True, otherwise we return False def find( haystack, needle ): for i in haystack: if i == needle: return True return False
12
How long does it take? Measuring in seconds isn't really helpful
It depends on the length of the list Let's say that the length of the list is n How many numbers do we have to check in the worst possible case?
13
Can we do better? Is there any way to check fewer than n numbers?
Well, on average, we only need to check half the numbers, that’s n/2 Still, it would be nice if we could do better
14
Binary Search
15
We can’t do better unless…
We can do better with more information For example, if the list is sorted, then we can use that information somehow How? We can play a High-Low game
16
Binary search Repeatedly divide the search space in half
We’re looking for 37, let’s say 23 54 31 37 Check the middle Check the middle Check the middle Check the middle (Too low) (Too low) (Found it!) (Too high)
17
So, is that faster than linear search?
How long can it take? What if you never find what you’re looking for? Well, then, you’ve narrowed it down to a single spot in the list that doesn’t have what you want And what’s the maximum amount of time that could have taken?
18
Running time for binary search
We cut the search space in half every time At worst, we keep cutting n in half until we get 1 Let’s say x is the number of times we look: 1 2 x n = n = 2 x log n = x It takes at most log n steps
19
What is log? The log operator is short for logarithm
Taking the logarithm means de-exponentiating something log =7 log 10 𝑥 =𝑥 What's the log 1,000,000 ?
20
Log base 2 In the normal world, when you see a log without a subscript, it means the logarithm base 10 "What power do you have to raise 10 to to get this number?" In computer science, a log without a subscript usually means the logarithm base 2 "What power do you have to raise 2 to to get this number?" log 2 8 =8 log 2 𝑦 =𝑦 What's the log 2048? (Assuming log base 2)
21
Log in arbitrary bases Log is defined for any base greater than 1
The base is usually written as a subscript log =11 log =7 log =2 log 𝑧 =𝑧 log 𝑏 𝑏 𝑎 =𝑎 What's the log5 125?
22
Log is awesome The logarithm of the number is related to the number of digits you need to write it That means that the log of a very large number is pretty small An algorithm that runs in log n time is very fast Number log10 log2 1,000 3 10 1,000,000 6 20 1,000,000,000 9 30 1,000,000,000,000 12 40
23
Interview Question
24
Interview question This is a classic interview question asked by Microsoft, Amazon, and similar companies Imagine that you have 9 red balls One of them is just slightly heavier than the others, but so slightly that you can’t feel it You have a very accurate two pan balance you can use to compare balls Find the heaviest ball in the smallest number of weighings
25
What’s the smallest possible number?
It’s got to be 8 or fewer We could easily test one ball against every other ball There must be some cleverer way to divide them up Something that is related somehow to binary search
26
That’s it! We can divide the balls in half each time
If those all balance, it must be the one we left out to begin with
27
Nope, we can do better How?
They key is that you can actually cut the number of balls into three parts each time We weigh 3 against 3, if they balance, then we know the 3 left out have the heavy ball When it’s down to 3, weigh 1 against 1, again knowing that it’s the one left out that’s heavy if they balance
28
Thinking outside the box, er, ball
The cool thing is… Yes, this is “cool” in the CS sense, not in the real sense Anyway, the cool thing is that we are trisecting the search space each time This means that it takes log3 n weighings to find the heaviest ball We could do 27 balls in 3 weighings, 81 balls in 4 weighings, etc.
29
Sorting
30
Sorting The importance of sorting should be evident to you by now
Applications: Sorting a column in Excel Organizing your iTunes playlists by artist name Ranking a high school graduating class Finding a median score to report on an exam Countless others…
31
But, is it interesting? Yes! It’s tricky
No, it’s not! Give me 100 names written on 100 index cards and I can sort them, no problem One way to remind yourself that it’s tricky is by increasing the problem size What if I gave you 1,000,000 names written on 1,000,000 index cards? You might need some organizational system
32
Computers are stupid Oh, yes, and there’s that mantra of this class
A computer can’t “jump” to the M section, unless you explicitly create an M section or something For most common sorts, the computer has to compare two numbers (or strings or whatever) at a time Based on that comparison, it has to take another step in the algorithm Remember, we have to swap things around in an list
33
Bubble Sort
34
Bubble sort is a classic sorting algorithm
It is simple to understand It is simple to code It is not very fast The idea is simply to go through your list, swapping out of order elements until nothing is out of order
35
Bubble Sort Activity
36
Code for a single pass One “pass” of the bubble sort algorithm goes through the list once, swapping out of order elements for j in range( len(list) - 1): if list[j] > list[j + 1]: list[j], list[j + 1] = list[j + 1], list[j]
37
Single pass example Run through the whole list, swapping any entries that are out of order No swap 45 Swap No swap 7 45 54 37 108 51 54 37 Swap No swap 108 51 Swap
38
How many passes do we need?
How bad could it be? What if the list was in reverse-sorted order? One pass would only move the largest number to the bottom We would need n – 1 passes to sort the whole list 6 5 4 3 2 7 1 6 5 4 3 2 1 7 6 5 4 3 7 2 1 6 5 7 4 3 2 1 7 6 5 4 3 2 1 6 7 5 4 3 2 1 6 5 4 7 3 2 1
39
Full bubble sort code The full Python function for bubble sort would require us to have at least n – 1 passes We'll do n, since it's simpler def bubbleSort( list ): for i in list: #n passes for j in range( len(list) - 1): if list[j] > list[j + 1]: list[j], list[j + 1] = list[j + 1], list[j]
40
Ascending sort The bubble sort we saw sorts values in ascending order
What if you wanted to sort them in descending order? Only a single change is needed to the inner loop: def bubbleSort( list ): for i in list: #n passes for j in range( len(list) - 1): if list[j] < list[j + 1]: list[j], list[j + 1] = list[j + 1], list[j]
41
Upcoming
42
Next time… Big Oh notation Files
43
Reminders Keep working on Project 3 Keep reading Python Chapter 5
Due this Friday! Keep reading Python Chapter 5 Get your teams and your proposal ready for your final project Proposal due next Friday!
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.