Week 9 - Monday CS 113.

Slides:



Advertisements
Similar presentations
MATH 224 – Discrete Mathematics
Advertisements

Week 12 - Wednesday.  What did we talk about last time?  Hunters and prey  Class variables  Big Oh notation.
Chapter 9: Searching, Sorting, and Algorithm Analysis
CS4HS at Marquette University. a) Find the sum of 4 and 7 b) Sort a list of words in alphabetical order c) State the most beautiful phrase in the English.
Sorting Algorithms. Motivation Example: Phone Book Searching Example: Phone Book Searching If the phone book was in random order, we would probably never.
CS 206 Introduction to Computer Science II 09 / 10 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
Complexity (Running Time)
Searching/Sorting Introduction to Computing Science and Programming I.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Week 5 - Monday.  What did we talk about last time?  Linked list implementations  Stacks  Queues.
Chapter 19: Searching and Sorting Algorithms
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Ch 18 – Big-O Notation: Sorting & Searching Efficiencies Our interest in the efficiency of an algorithm is based on solving problems of large size. If.
Intro to Sorting Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use by MSU Dept. of Computer Science.
1 Algorithms CS 202 Epp section ??? Aaron Bloomfield.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
Week 12 - Friday.  What did we talk about last time?  Finished hunters and prey  Class variables  Constants  Class constants  Started Big Oh notation.
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
Searching Topics Sequential Search Binary Search.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
Search Algorithms Written by J.J. Shepherd. Sequential Search Examines each element one at a time until the item searched for is found or not found Simplest.
Sorting Algorithms Written by J.J. Shepherd. Sorting Review For each one of these sorting problems we are assuming ascending order so smallest to largest.
Sorting: why?  We do it A LOT!  Makes finding the largest and smallest value easier  Makes finding how many of a certain value are in a list easier.
Algorithm Analysis with Big Oh ©Rick Mercer. Two Searching Algorithms  Objectives  Analyze the efficiency of algorithms  Analyze two classic algorithms.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Assignment 5 is posted. Exercise 8 is very similar to what you will be doing with assignment 5. Exam.
Searching/Sorting. Searching Searching is the problem of Looking up a specific item within a collection of items. Searching is the problem of Looking.
Searching and Sorting Searching algorithms with simple arrays
UNIT - IV SORTING By B.Venkateswarlu Dept of CSE.
Growth of Functions & Algorithms
Week 13: Searching and Sorting
CMSC201 Computer Science I for Majors Lecture 23 – Sorting
Introduction to Search Algorithms
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Recitation 13 Searching and Sorting.
IGCSE 6 Cambridge Effectiveness of algorithms Computer Science
Introduction to Algorithms
COMP 53 – Week Seven Big O Sorting.
Week 2 - Friday CS221.
Week 13 - Monday CS 121.
Teach A level Computing: Algorithms and Data Structures
Enough Mathematical Appetizers!
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Sorting Algorithms Written by J.J. Shepherd.
Binary Search Back in the days when phone numbers weren’t stored in cell phones, you might have actually had to look them up in a phonebook. How did you.
Last Class We Covered Data representation Binary numbers ASCII values
Creativity in Algorithms
Algorithm design and Analysis
Winter 2018 CISC101 11/19/2018 CISC101 Reminders
Quicksort analysis Bubble sort
Chapter 8 Search and Sort
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Winter 2018 CISC101 12/2/2018 CISC101 Reminders
Last Class We Covered Dictionaries Hashing Dictionaries vs Lists
Applied Discrete Mathematics Week 6: Computation
Topic 1: Problem Solving
Search,Sort,Recursion.
Searching and Sorting Topics Sequential Search on an Unordered File
Sub-Quadratic Sorting Algorithms
Sorting "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting Hat, Harry Potter.
Search,Sort,Recursion.
Amortized Analysis and Heaps Intro
Quicksort and selection
Algorithms.
Algorithm Analysis How can we demonstrate that one algorithm is superior to another without being misled by any of the following problems: Special cases.
Presentation transcript:

Week 9 - Monday CS 113

Last time What did we talk about last time? List examples split() method for strings Lab 8

Questions?

Project 3

Algorithms

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

Peanut butter jelly time Describe an algorithm for making a peanut butter and jelly sandwich What is the right level of detail?

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?

Searching

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?

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

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?

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

Binary Search

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

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)

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?

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 = 1 n = 2 x log n = x It takes at most log n steps

What is log? The log operator is short for logarithm Taking the logarithm means de-exponentiating something log 10 7 =7 log 10 𝑥 =𝑥 What's the log 1,000,000 ?

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)

Log in arbitrary bases Log is defined for any base greater than 1 The base is usually written as a subscript log 3 3 11 =11 log 5 5 7 =7 log 7 49=2 log 13 13 𝑧 =𝑧 log 𝑏 𝑏 𝑎 =𝑎 What's the log5 125?

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

Interview Question

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

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

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

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

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.

Sorting

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…

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

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

Bubble Sort

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

Bubble Sort Activity

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]

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

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

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]

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]

Upcoming

Next time… Big Oh notation Files

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!