Binary Search and Intro to Sorting

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

Garfield AP Computer Science
Complexity Theory  Complexity theory is a problem can be solved? Given: ◦ Limited resources: processor time and memory space.
SEARCHING, SORTING, TOPOLOGICAL SORTS Most real world computer applications deal with vast amounts of data. Searching for a particular data item can take.
CPSC 171 Introduction to Computer Science More Efficiency of Algorithms.
Algorithm An algorithm is a step-by-step set of operations to be performed. Real-life example: a recipe Computer science example: determining the mode.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
Wednesday, 11/25/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 11/25/02  QUESTIONS??  Today:  More on sorting. Advanced sorting algorithms.  Complexity:
Wednesday, 11/13/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 11/13/02  QUESTIONS??  Today:  More on searching a list; binary search  Sorting a list;
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
CS2336: Computer Science II
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.
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.
Asymptotic Notations Iterative Algorithms and their analysis
Array operations II manipulating arrays and measuring performance.
CS 2430 Day 28. Announcements We will have class in ULR 111 on Monday Exam 2 next Friday (sample exam will be distributed next week)
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
Chapter 19: Searching and Sorting Algorithms
1 Searching. 2 Searching Searching refers to the operation of finding an item from a list of items based on some key value. Two Searching Methods (1)
Algorithm Evaluation. What’s an algorithm? a clearly specified set of simple instructions to be followed to solve a problem a way of doing something What.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
SEARCHING. Vocabulary List A collection of heterogeneous data (values can be different types) Dynamic in size Array A collection of homogenous data (values.
Big Oh Algorithms are compared to each other by expressing their efficiency in big-oh notation Big O notation is used in Computer Science to describe the.
Asymptotic Notation (O, Ω, )
Intro to Sorting Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Chapter 12 Binary Search and QuickSort Fundamentals of Java.
3.3 Complexity of Algorithms
Lecture on Binary Search and Sorting. Another Algorithm Example SEARCHING: a common problem in computer science involves storing and maintaining large.
Sorting.
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.
1 Discrete Structures – CNS2300 Text Discrete Mathematics and Its Applications Kenneth H. Rosen (5 th Edition) Chapter 2 The Fundamentals: Algorithms,
Searching Topics Sequential Search Binary Search.
Searching & Sorting. Algorithms Step by step recipe to do a task…
 2006 Pearson Education, Inc. All rights reserved. 1 Searching and Sorting.
Unit 2 Day 11 FOCS – Human Computer Interaction. Tower Building Presentation Explain your solution.
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
Algorithm Analysis with Big Oh ©Rick Mercer. Two Searching Algorithms  Objectives  Analyze the efficiency of algorithms  Analyze two classic algorithms.
Chapter 16: Searching, Sorting, and the vector Type.
Growth of Functions & Algorithms
Week 13: Searching and Sorting
May 17th – Comparison Sorts
CMSC201 Computer Science I for Majors Lecture 23 – Sorting
Last Class We Covered Sorting algorithms Searching algorithms
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Big O notation Big O notation is used in Computer Science to describe the performance or complexity of an algorithm. Big O specifically describes the worst-case.
Search Lesson Outline Searching Lesson Outline
Introduction to Recursion
Introduction to Algorithms
Sorting by Tammy Bailey
Big O: Make it Simple Determine how complex the algorithm is, in relative to the size of the problem (e.g: List to be sorted) 'O' Stands for 'Order' -
Topics discussed in this section:
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
2008/12/03: Lecture 20 CMSC 104, Section 0101 John Y. Park
Complexity Present sorting methods. Binary search. Other measures.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Searching & Sorting.
Last Class We Covered Sorting algorithms Searching algorithms
Algorithmic Complexity
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Binary Search Counting
Core Assessments Core #1: This Friday (5/4) Core #2: Tuesday, 5/8.
Algorithms.
CMPT 120 Lecture 26 – Unit 5 – Internet and Big Data
Presentation transcript:

Binary Search and Intro to Sorting Intro to Computer Science CS1510 Dr. Sarah Diesburg

Last Time We looked at two basic algorithms for searching Linear search Binary search Linear search was the easiest to write But perhaps not the best from a complexity standpoint

Last Time Big “O” measures how badly the problem grows as the data set grows Study of complexity of algorithms Worst case of linear search was N, where N is the number of comparisons that we need to perform Double the number of items in list, double the amount of time needed to complete the search in the worst case

Last Time The binary search was another solution that incurred less comparisons in the worst case Only works on sorted list

Binary Search Binary search algorithm Try the guess at middle index of the range If the value we are searching for is higher than number at the index, then adjust your low range bound to be your guess+1 If the value we are searching for is lower than number at the index, then adjust your high range bound to be your guess-1 Repeat

Binary Search What is the worst-case scenario of the binary search? Thinking of a number between 1 and 100 7 guesses in total – why? 1 guesses – cut down to 50 possibilities 2 guesses – cut down to 25 3 guesses – cut down to 12 4 guesses – cut down to 6 5 guesses – cut down to 3 6 guesses – cut down to 1 7 guesses – to figure out if last guess is right

Binary Search What is the complexity of a binary search? log2(100) = x Big O value of log2 N This is “log base 2” log2(100) = x What is this saying?

Binary Search What is the complexity of a binary search? log2(100) = x Big O value of log2 N This is “log base 2” log2(100) = x What is this saying? 2x = 100 Go “to the next power” when not exact

Binary Search How does that relate to our binary search? Let’s say there are 16 items in our list. What is the worst case number of guesses? 32? 34? 64? One million?

Binary Search How does that relate to our binary search? Let’s say there are 16 items in our list. What is the worst case number of guesses? 32? 34? 64? One million? One million is about 20 guesses 2^10 = 1024 One million is 1000 squared, so twice as much

Searching So which kind of search would amazon.com use to search their databases?

Demo binarySearch() on different types of lists Ordered Odd Reverse

Demo binarySearch() on different types of lists Ordered Odd Reverse The reverse list doesn’t work because the list needs to be sorted in ascending order. How do we sort?

Intro to Sorting Let’s watch a quick video How can we know which sort is really faster? Bubble Sort Insertion Sort Quick Sort We need to use Big “O” analysis (next time…)