Last Class We Covered Sorting algorithms Searching algorithms

Slides:



Advertisements
Similar presentations
Order of complexity. Consider four algorithms 1.The naïve way of adding the numbers up to n 2.The smart way of adding the numbers up to n 3.A binary search.
Advertisements

Sorting CMSC 201. Sorting In computer science, there is often more than one way to do something. Sorting is a good example of this!
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,
Data Structures Data Structures Topic #13. Today’s Agenda Sorting Algorithms: Recursive –mergesort –quicksort As we learn about each sorting algorithm,
CS 206 Introduction to Computer Science II 09 / 10 / 2008 Instructor: Michael Eckmann.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
Search Lesson CS1313 Spring Search Lesson Outline 1.Searching Lesson Outline 2.How to Find a Value in an Array? 3.Linear Search 4.Linear Search.
Time Complexity Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Chapter 19 Searching, Sorting and Big O
Searching and Sorting Topics Sequential Search on an Unordered File
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
1 Algorithms  Algorithms are simply a list of steps required to solve some particular problem  They are designed as abstractions of processes carried.
Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n )
CS 150: Analysis of Algorithms. Goals for this Unit Begin a focus on data structures and algorithms Understand the nature of the performance of algorithms.
Searching Topics Sequential Search Binary Search.
Week 13 - Wednesday.  What did we talk about last time?  NP-completeness.
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.
Algorithm Analysis with Big Oh ©Rick Mercer. Two Searching Algorithms  Objectives  Analyze the efficiency of algorithms  Analyze two classic algorithms.
Intro to Data Structures Concepts ● We've been working with classes and structures to form linked lists – a linked list is an example of something known.
CMSC 104, Version 8/061L24Searching&Sorting.ppt Searching and Sorting Topics Sequential Search on an Unordered File Sequential Search on an Ordered File.
CMSC201 Computer Science I for Majors Lecture 23 – Algorithms and Analysis Prof. Katherine Gibson Based on slides from previous iterations.
Design and Analysis of Algorithms
19 Searching and Sorting.
Sorting and "Big Oh" ASFA AP Computer Science A SortingBigOh.
Analysis of Algorithms
Week 9 - Monday CS 113.
Week 13: Searching and Sorting
CMSC201 Computer Science I for Majors Lecture 23 – Sorting
Algorithmic complexity: Speed of algorithms
Last Class We Covered Sorting algorithms Searching algorithms
Last Class We Covered Sorting algorithms “Run” time Selection Sort
Big-O notation.
Search Lesson Outline Searching Lesson Outline
Sorting by Tammy Bailey
Week 12 - Wednesday CS221.
CMSC201 Computer Science I for Majors Lecture 24 – Sorting
Teach A level Computing: Algorithms and Data Structures
Computation.
Efficiency (Chapter 2).
Sorting Algorithms Written by J.J. Shepherd.
Last Class We Covered Data representation Binary numbers ASCII values
Algorithm design and Analysis
2008/12/03: Lecture 20 CMSC 104, Section 0101 John Y. Park
Complexity Present sorting methods. Binary search. Other measures.
Binary Search and Intro to Sorting
Searching and Sorting Topics Sequential Search on an Unordered File
Big O Notation.
Data Structures Review Session
Last Class We Covered Dictionaries Hashing Dictionaries vs Lists
8/04/2009 Many thanks to David Sun for some of the included slides!
QuickSort Previous slides based on ones by Ethan Apter & Marty Stepp
UMBC CMSC 104 – Section 01, Fall 2016
Searching and Sorting Topics Sequential Search on an Unordered File
Last Class We Covered Asymptotic Analysis Run “time” Big O
Algorithmic complexity: Speed of algorithms
Searching, Sorting, and Asymptotic Complexity
Prof. Katherine Gibson Prof. Jeremy Dixon
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CMSC201 Computer Science I for Majors Final Exam Information
Algorithmic complexity: Speed of algorithms
CMSC201 Computer Science I for Majors Lecture 24 – Sorting
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
David Kauchak cs161 Summer 2009
Core Assessments Core #1: This Friday (5/4) Core #2: Tuesday, 5/8.
Quicksort and selection
Analysis of Algorithms CS 477/677
Last Class We Covered Sorting algorithms Searching algorithms
Data Structures and Algorithms CS 244
Sorting and selection Prof. Noah Snavely CS1114
Presentation transcript:

CMSC201 Computer Science I for Majors Lecture 24 – Algorithmic Analysis

Last Class We Covered Sorting algorithms Searching algorithms Bubble Sort Selection Sort Quicksort Searching algorithms Linear search Binary search

Any Questions from Last Time?

Today’s Objectives To learn about asymptotic analysis What it is Why it’s important How to calculate it To discuss “run time” of algorithms Why one algorithm is “better” than another

Alphabetizing a Bookshelf Video from https://www.youtube.com/watch?v=WaNLJf8xzC4

Run Time

Run Time An algorithm’s run time is the amount of “time” it takes for that algorithm to run “Time” normally means number of operations or something similar, and not seconds or minutes Run time is shown as an expression, which updates based on how large the problem is Run time shows how an algorithm scales, or changes with the size of the problem

Example: Fibonacci Recursion Ideally, we want an algorithm that runs in a reasonable amount of time, no matter how large the problem Remember the recursive Fibonacci program? It runs within one second for smaller positions But the larger the position we ask for, the longer and longer it takes

Fibonacci Recursion python fibEx.py (with position < 30): < 1 second python fibEx.py (with position = 30): 2 seconds python fibEx.py (with position = 35): 8 seconds python fibEx.py (with position = 40): 76 seconds

Fibonacci Recursion python fibEx.py (with position = 50): Guess! 9,493 seconds 2 hours, 38 minutes, 13 seconds!!!

Run Time for Linear Search Say we have a list that does not contain what we’re looking for. How many things in the list does linear search have to look at for it to figure out the item’s not there for a list of 8 things? 16 things? 32 things?

Run Time for Binary Search Say we have a list that does not contain what we’re looking for. What about for binary search? How many things does it have to look at to figure out the item’s not there for a list of 8 things? 16 things? 32 things? Notice anything different?

Different Run Times These algorithms scale differently! Linear search does an amount of work equal to the number of items in the list Binary search does an amount of work equal to the log2 of the numbers in the list! By the way, log2(x) is basically asking “2 to what power equals x?” (normally shown as lg(x) ) This is the same as saying, “how many times must we divide x in half before we hit 1?”

Bubble Sort Run Time For a list of size N, how much work do we do for a single pass? N How many passes will we have to do? What is the run time of Bubble Sort? N2

Selection Sort Run Time What is the run time of finding the lowest number in a list? For a list of size N, how many elements do you have to look through to find the min? N

Selection Sort Run Time For a list of size N, how many times would we have to find the min to sort the list? N What is the run time of this sorting algorithm? N2

Quicksort Run Time For a list of size N, how many steps does it take to move everything less than the “pivot” to the left and everything greater than the “pivot” to the right? N

Quicksort Run Time How many times will the algorithm divide the list in half? lg(N) What is the run time of Quicksort? N * lg(N)

Different Run Times As our list gets bigger and bigger, which of the search algorithms is faster? Linear or binary search? How much faster is binary search? A lot! But exactly how much is “a lot”?

Asymptotic Analysis

What is “Big O” Notation? Big O notation is a concept in Computer Science Used to describe the complexity (or performance) of an algorithm Big O describes the worst-case scenario Big Omega (Ω) describes the best-case Big Theta (Θ) is used when the best and worst case scenarios are the same

A Simple Example Say we write an algorithm that takes in an list of numbers and returns the maximum What is the absolute fastest it can run? Linear time – Ω(N) What is the absolute slowest it can run? Linear time – O(N) Are these two values the same? YES – so we can also say it’s Θ(N)

Simplification We are only interested in the growth rate as an “order of magnitude” As the problem grows really, really, really large We are not concerned with the fine details Constant multipliers are dropped So O(3 * N2) becomes simply O(N2) Lower order terms are dropped So O(N3 + 4N2) becomes simply O(N3)

Asymptotic Analysis For a list of size N, linear search does N operations. So we say it is O(N) (pronounced “big Oh of n”) For a list of size N, binary search does lg(N) operations, so we say it is O(lg(N)) The function inside the O() parentheses indicates how fast the algorithm scales

Worst Case vs Best Case Why differentiate between the two? Think back to selection sort What is the best case for run time? What is the worst case for run time? They’re the same! Always have to find each minimum by looking through the entire list every time – Θ(N2)

Bubble Sort Run Times What about bubble sort? Very different! What is the best case for run time? What is the worst case for run time? Very different! Best case, everything is already sorted – Ω( N ) Worst case, it’s completely backwards – O( N2 )

Quicksort Run Times What about quicksort? Depends on what the “hinge” or “pivot” is This determines how many times we split But each split, we’ll need to compare each item to the hinge in their respective part: O( N ) Best case, pivot is exact center – Ω( N*lgN ) Worst case, it’s an “edge” item – O( N2 )

Worst-case vs Best-case This is why, even though all three sorting algorithms have the same run times... Quicksort often runs very, very quickly Bubble Sort often runs much faster than Selection How does this apply to linear search and binary search? What are the best and worst run times for these?

Search Run Times Linear search: Binary search: Best case: Ω( 1 ) Worst case: O( N ) Binary search: Worst case: O( lg(N) )

Why Care?

Why Care?

Why Care?

Why Care? 19,311,800

Why Care? 337,407,000,000,000,000

Why Care? For large problems, there’s a huge difference! If we can do 1,000,000 operations per second, and the list is 337.4 quadrillion items Binary search takes 0.000058 seconds Linear search takes 337,407,000,000 seconds 5,623,450,000 minutes 93,724,166 hours 3,905,173 days 10,699 years

Daily CS History Hedy Lamarr Film star in 1930s - 1950s Patented a frequency-hopping system that would make radio-guided torpedoes hard to detect or jam during World War II Technologies like Bluetooth and Wi-Fi use similar methods

Announcements Project 3 is due on Friday, December 8th Survey #3 due on Tuesday, December 12th If completed, will receive an email with responses SEEQs are out now – link in your email/on BB Final exam is when? Friday, December 15th from 6 to 8 PM

Completing SEEQs Please take the time now, if you haven’t already, to complete the SEEQ online You can access it via the link in your email, or via Blackboard This is the part  I will get to see

Final Exam Locations Find your room ahead of time! ITE Building 102 - Sections 22, 28, 32 ITE Building 104 - Sections 2, 3, 4, 5, 6 Meyerhoff 030 - Sections 8, 9, 10, 11, 12, 14, 17, 18, 20 Performing Arts 132 - Sections 15, 16, 31 Sherman 003 - Sections 23, 26, 29, 30 Public Policy 105 - Sections 21, 24, 27

Image Sources Alphabetizing a Bookshelf video screenshot: https://www.youtube.com/watch?v=WaNLJf8xzC4 Graphs of x and log2(x) courtesy of Google equation grapher Hedy Lamarr: https://commons.wikimedia.org/wiki/File:Hedy_lamarr_-_1940.jpg