Announcements: Project 5 Everything we have been learning thus far will enable us to solve interesting problems Project 5 will focus on applying the skills.

Slides:



Advertisements
Similar presentations
Recursion Chapter 14. Overview Base case and general case of recursion. A recursion is a method that calls itself. That simplifies the problem. The simpler.
Advertisements

Announcements You survived midterm 2! No Class / No Office hours Friday.
Garfield AP Computer Science
Visual C++ Programming: Concepts and Projects
Analysis And Algorithms CMSC 201. Search Sometimes, we use the location of a piece of information in a list to store information. If I have the list [4,
Computability Start complexity. Motivation by thinking about sorting. Homework: Finish examples.
CS 206 Introduction to Computer Science II 09 / 10 / 2008 Instructor: Michael Eckmann.
Chapter 11 Sorting and Searching. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Examine the linear search and.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
CS 206 Introduction to Computer Science II 09 / 05 / 2008 Instructor: Michael Eckmann.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
Data Structure Algorithm Analysis TA: Abbas Sarraf
Searching and Sorting Arrays
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
1 Section 2.3 Complexity of Algorithms. 2 Computational Complexity Measure of algorithm efficiency in terms of: –Time: how long it takes computer to solve.
Simple Sorting Algorithms. 2 Bubble sort Compare each element (except the last one) with its neighbor to the right If they are out of order, swap them.
CHAPTER 7: SORTING & SEARCHING Introduction to Computer Science Using Ruby (c) Ophir Frieder at al 2012.
Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.
Recursion, Complexity, and Searching and Sorting By Andrew Zeng.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
Chapter 19 Searching, Sorting and Big O
Recursion, Complexity, and Sorting By Andrew Zeng.
Chapter 10 Strings, Searches, Sorts, and Modifications Midterm Review By Ben Razon AP Computer Science Period 3.
 2005 Pearson Education, Inc. All rights reserved Searching and Sorting.
 Pearson Education, Inc. All rights reserved Searching and Sorting.
Searching. RHS – SOC 2 Searching A magic trick: –Let a person secretly choose a random number between 1 and 1000 –Announce that you can guess the number.
CS 61B Data Structures and Programming Methodology July 28, 2008 David Sun.
Adapted from instructor resource slides Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All.
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.
Announcements Course evaluation Your opinion matters! Attendance grades Will be posted prior to the final Project 5 grades Will be posted prior to the.
Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n )
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Searching Topics Sequential Search Binary Search.
CS1022 Computer Programming & Principles Lecture 2.2 Algorithms.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Announcements No Labs / Recitation this week On Friday we will talk about Project 3 Release late afternoon / evening tomorrow Cryptography.
Sorting Algorithms. Algorithms, revisited What is an algorithm? Wikipedia Definition: an algorithm is a definite list of well-defined instructions for.
1 CS Review, iClicker -Questions Week 15. ANY QUESTIONS? 2.
1 Algorithms Searching and Sorting Algorithm Efficiency.
1 CS Review, iClicker -Questions Week 15. Announcements 2.
Searching and Sorting Searching algorithms with simple arrays
16 Searching and Sorting.
Week 9 - Monday CS 113.
CMSC201 Computer Science I for Majors Lecture 23 – Sorting
Lecture 14 Searching and Sorting Richard Gesick.
Introduction to Search Algorithms
Algorithm Analysis The case of recursion.
Sorting Algorithms.
COMP 53 – Week Seven Big O Sorting.
Announcements Project 4 due Wed., Nov 7
Sorting by Tammy Bailey
Teach A level Computing: Algorithms and Data Structures
Algorithmic complexity: Speed of algorithms
Adapted from slides by Marty Stepp and Stuart Reges
Last Class We Covered Data representation Binary numbers ASCII values
Algorithm design and Analysis
Introduction to Programming
Quicksort analysis Bubble sort
MSIS 655 Advanced Business Applications Programming
Winter 2018 CISC101 12/2/2018 CISC101 Reminders
Lecture 6 Efficiency of Algorithms (2) (S&G, ch.3)
Lecture 11 Searching and Sorting Richard Gesick.
MSIS 655 Advanced Business Applications Programming
Last Class We Covered Dictionaries Hashing Dictionaries vs Lists
8/04/2009 Many thanks to David Sun for some of the included slides!
Simple Sorting Methods: Bubble, Selection, Insertion, Shell
Review of Searching and Sorting Algorithms
Presentation transcript:

Announcements: Project 5 Everything we have been learning thus far will enable us to solve interesting problems Project 5 will focus on applying the skills we have learned on a problem from biology, specifically computational biology your group / group requests by Friday Project 5 will be released tomorrow late afternoon

Announcements Midterm 2: Same curve as Midterm 1 Pre Lab 15 will be released next Friday Stale version of week 12 slides was accidently uploaded to the wiki (correct slides were presented in class) – this has been fixed, please re download

O(1) Example def isOdd(list): return (len(list)%2 == 1) >>> isOdd([0]) True >>> isOdd([0,1]) False

Clicker Question def getFirst(list): if len(list) == 0: return -1 return (list[0]) A: O(n) B: O(n 2 ) C: O(1) >>> getFirst([]) >>> getFirst([0,1,2,3]) 0 >>> getFirst(["a", "b", "c"]) 'a’

Building the Intuition Logic Puzzle: You have 9 marbles. 8 marbles weigh 1 ounce each, & one marble weighs 1.5 ounces. You are unable to determine which is the heavier marble by looking at them. How do you find the marble which weighs more?

Solution 1: Weigh one marble vs another What is the complexity of this solution?

Finding the complexity Step 1: What is our input? The marbles Step 2: How much work do we do per marble? We weight each marble once (except one) Step 3: What is the total work we did? 8 measurements What if we had 100 marbles or 1000?

Clicker Question: What is the complexity of this algorithm? A: O(n) B: O(n 2 ) C: O(1) D: O(log n)

We can do better! Lets pull some intuition from our search algorithm that was O(log n) We want a way to eliminated ½ (or more) of the marbles with each measurement How might we do this? What about weighing multiple marbles at once?

The Optimal Solution Split the marbles into three groups We can then weigh two of the groups

Finding the complexity of the optimal solution Step 1: What is our input? The marbles Step 2: How much work do we do per marble? Logarithmic Step 3: What is the total work we did? 2 measurements What if we had 100 marbles or 1000?

What happens at each step? We eliminated 2/3rds of the marbles

Clicker Question: What is the complexity of this algorithm? A: O(n) B: O(n 2 ) C: O(1) D: O(log n)

Sorting Motivation We can answer questions like min/max very efficiently We can search very efficiently What if we need to search many many times Many algorithms require their input to be sorted

Intuition behind bubble sort Background reading (homework): Bubble sort takes a list and returns a sorted list Compare each pair of adjacent items and swap them if the one to the right is smaller Assumption: we want our list sorted from smallest to greatest

Intuition behind bubble sort [5, 7, 9, 0, 3, 5, 6] [5, 7, 0, 9, 3, 5, 6] [5, 7, 0, 3, 9, 5, 6] [5, 7, 0, 3, 5, 9, 6] [5, 7, 0, 3, 5, 6, 9]

Intuition behind bubble sort [5, 7, 0, 3, 5, 6, 9] [5, 0, 7, 3, 5, 6, 9] [5, 0, 3, 7, 5, 6, 9] [5, 0, 3, 5, 7, 6, 9] [5, 0, 3, 5, 6, 7, 9]

Intuition behind bubble sort How many passes do we have to do before we are guaranteed the list is sorted? n passes, where n is the length of the list In each pass we do how much work? n-1 comparisons What is the total work? Complexity?

Changing our Intuition into Code def BubbleSort(myList): swapped = True while swapped: swapped = False for i in range(len(myList)-1): if myList[i] > myList[i+1]: temp = myList[i] myList[i] = myList[i+1] myList[i+1] = temp swapped = True return myList The main loop Keep executing the loop IF we do a swap

Changing our Intuition into Code def BubbleSort(myList): swapped = True while swapped: swapped = False for i in range(len(myList)-1): if myList[i] > myList[i+1]: temp = myList[i] myList[i] = myList[i+1] myList[i+1] = temp swapped = True return myList The loop that executes the swaps

Changing our Intuition into Code def BubbleSort(myList): swapped = True while swapped: swapped = False for i in range(len(myList)-1): if myList[i] > myList[i+1]: temp = myList[i] myList[i] = myList[i+1] myList[i+1] = temp swapped = True return myList Check if the two numbers should be swapped

Changing our Intuition into Code def BubbleSort(myList): swapped = True while swapped: swapped = False for i in range(len(myList)-1): if myList[i] > myList[i+1]: temp = myList[i] myList[i] = myList[i+1] myList[i+1] = temp swapped = True return myList Swap!

Fast Swapping of Two Variables Python provides us the ability to perform the swap in a much more efficient manner >>> a = 5 >>> b = 7 >>> a, b = b, a >>> print a 7 >>> print b 5 variable1, variable 2 = variable2, variable1

Changing our Intuition into Code def BubbleSort(myList): swapped = True while swapped: swapped = False for i in range(len(myList)-1): if myList[i] > myList[i+1]: myList[i], myList[i+1] = myList[i+1], myList[i] swapped = True return myList Swap!

Homework Reach Chapter 11 from the text book

Can we sort faster? Bubble sort certainly will sort our data for us Unfortunately it simply is not fast enough We can sort faster! There are algorithms which sort in O(n log n) or log linear time Lets reason why this is the case

Observation 1: We can merge two sorted lists in linear time What is in the input? Both the lists, n = total amount of elements Why is the complexity linear? We must examine each element in each of the lists Its linear in the total amount of elements O(len(list1) + len(list2)) = O(n)

Observation 1: We can merge two sorted lists in linear time [5,9,10, 100, 555] [3,4,12, 88, 535] [3]

Observation 1: We can merge two sorted lists in linear time [5,9,10, 100, 555] [3,4,12, 88, 535] [3, 4]

Observation 1: We can merge two sorted lists in linear time [5,9,10, 100, 555] [3,4,12, 88, 535] [3,4,5]

Observation 1: We can merge two sorted lists in linear time [5,9,10, 100, 555] [3,4,12, 88, 535] [3,4,5,9]

Observation 1: We can merge two sorted lists in linear time [5,9,10, 100, 555] [3,4,12, 88, 535] [3,4,5,9,10]

Observation 1: We can merge two sorted lists in linear time [5,9,10, 100, 555] [3,4,12, 88, 535] [3,4,5,9,10,12]

Observation 1: We can merge two sorted lists in linear time [5,9,10, 100, 555] [3,4,12, 88, 535] [3,4,5,9,10,12, 88]

Observation 2 Notice that merging two lists of length one ends up producing a sorted list of length two [5] [3] [5] [3] [3,5] [5] [3] [3,5]

Lets build the intuition for Merge-Sort We know we can merge sorted lists in linear time We know that merging two lists of length one results in a sorted list of length two Lets split our unsorted list into a bunch of lists of length one and merge them into progressively bigger lists! We split a list into two smaller lists of equal parts Keep splitting until we have lists of length one

Visual Representation log(n) n elements merged

Putting it all together We know that there are log(n) splits At each “level” we split each list in two We know that we need to merge a total of n elements at each “level” n * log(n) thus O(n log n)

Synopsis Took a look at the code for bubble sort We learned an efficient way to swap the contents of two variables (or list locations) We built the intuition as to why we can sort faster than quadratic time Introduced the concept of merge sort

Homework Start working on Project 5 Play around with the concepts presented in recitation as well as the pre lab They will help both with the lab AND project 5 Review the project 4 solution Many of the same concepts will be used in project 5