Lecture on Binary Search and Sorting. Another Algorithm Example SEARCHING: a common problem in computer science involves storing and maintaining large.

Slides:



Advertisements
Similar presentations
Zabin Visram Room CS115 CS126 Searching
Advertisements

College of Information Technology & Design
Efficiency of Algorithms Csci 107 Lecture 6-7. Topics –Data cleanup algorithms Copy-over, shuffle-left, converging pointers –Efficiency of data cleanup.
Computers and Scientific Thinking David Reed, Creighton University Algorithms and Programming Languages 1.
Searching Algorithms Finding what you are looking for.
B+-Trees (PART 1) What is a B+ tree? Why B+ trees? Searching a B+ tree
HST 952 Computing for Biomedical Scientists Lecture 9.
SEARCHING, SORTING, TOPOLOGICAL SORTS Most real world computer applications deal with vast amounts of data. Searching for a particular data item can take.
Searching Kruse and Ryba Ch and 9.6. Problem: Search We are given a list of records. Each record has an associated key. Give efficient algorithm.
Ver. 1.0 Session 5 Data Structures and Algorithms Objectives In this session, you will learn to: Sort data by using quick sort Sort data by using merge.
© 2006 Pearson Addison-Wesley. All rights reserved10-1 Chapter 10 Algorithm Efficiency and Sorting CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck.
Chapter 3 The Efficiency of Algorithms
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
Efficiency of Algorithms Csci 107 Lecture 8. Last time –Data cleanup algorithms and analysis –  (1),  (n),  (n 2 ) Today –Binary search and analysis.
Searching/Sorting Introduction to Computing Science and Programming I.
1 Sorting/Searching and File I/O Sorting Searching Reading for this lecture: L&L
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
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.
1 CSC 222: Computer Programming II Spring 2005 Searching and efficiency  sequential search  big-Oh, rate-of-growth  binary search  example: spell checker.
1 CSC 222: Computer Programming II Spring 2004 Searching and efficiency  sequential search  big-Oh, rate-of-growth  binary search Class design  templated.
Chapter 5 Ordered List. Overview ● Linear collection of entries  All the entries are arranged in ascending or descending order of keys.
Lecture 5 Searching and Sorting Richard Gesick. The focus Searching - examining the contents of the array to see if an element exists within the array.
ITEC 2620A Introduction to Data Structures
1 CSC 222: Object-Oriented Programming Spring 2012 Searching and sorting  sequential search  algorithm analysis: big-Oh, rate-of-growth  binary search.
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
Chapter 19: Searching and Sorting Algorithms
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Lecture 4 on Data Structure Array. Prepared by, Jesmin Akhter, Lecturer, IIT, JU Searching : Linear search Searching refers to the operation of finding.
DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Searching Course Lecture Slides 28 May 2010 “ Some things Man was never.
 … we have been assuming that the data collections we have been manipulating were entirely stored in memory.
CSC 211 Data Structures Lecture 13
1 Today’s Material Iterative Sorting Algorithms –Sorting - Definitions –Bubble Sort –Selection Sort –Insertion Sort.
ITEC 2620M Introduction to Data Structures Instructor: Prof. Z. Yang Course Website: ec2620m.htm Office: Tel 3049.
Forensics and CS Philip Chan. CSI: Crime Scene Investigation high tech forensics tools DNA profiling Use.
CS261 Data Structures Ordered Bag Dynamic Array Implementation.
Data Structures Types of Data Structure Data Structure Operations Examples Choosing Data Structures Data Structures in Alice.
Review 1 Arrays & Strings Array Array Elements Accessing array elements Declaring an array Initializing an array Two-dimensional Array Array of Structure.
UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Searching & Sorting.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n )
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Invitation to Computer Science 6th Edition Chapter 3 The Efficiency of Algorithms.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Searching Topics Sequential Search Binary Search.
Today’s Material Sorting: Definitions Basic Sorting Algorithms
Unit 2 Day 11 FOCS – Human Computer Interaction. Tower Building Presentation Explain your solution.
CSCI-235 Micro-Computers in Science Algorithms Part II.
WHICH SEARCH OR SORT IS BETTER?. COMPARING ALGORITHMS Time efficiency refers to how long it takes an algorithm to run Space efficiency refers to the amount.
Algorithm Analysis with Big Oh ©Rick Mercer. Two Searching Algorithms  Objectives  Analyze the efficiency of algorithms  Analyze two classic algorithms.
Searching and Sorting Searching algorithms with simple arrays
Searching and Sorting Algorithms
CSC 222: Object-Oriented Programming
Sorting by Tammy Bailey
CSc 110, Spring 2017 Lecture 39: searching.
CSC212 Data Structure - Section RS
Binary Search and Intro to Sorting
Searching CLRS, Sections 9.1 – 9.3.
24 Searching and Sorting.
Chapter 4.
Algorithms and Programming Languages
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Computers and Scientific Thinking David Reed, Creighton University
CSC212 Data Structure - Section KL
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Core Assessments Core #1: This Friday (5/4) Core #2: Tuesday, 5/8.
Invitation to Computer Science 5th Edition
Presentation transcript:

Lecture on Binary Search and Sorting

Another Algorithm Example SEARCHING: a common problem in computer science involves storing and maintaining large amounts of data, and then searching the data for particular values – data storage and retrieval are key to many industry applications – search algorithms are necessary to storing and retrieving data efficiently – e.g., consider searching a large payroll database for a particular record if the computer selected entries at random, there is no assurance that the particular record will be found even if the record is found, it is likely to take a large amount of time a systematic approach assures that a given record will be found, and that it will be found more efficiently there are two commonly used algorithms for searching a list of items – sequential search – general purpose, but relatively slow – binary search – restricted use, but fast

Sequential Search sequential search is an algorithm that involves examining each list item in sequential order until the desired item is found sequential search for finding an item in a list 1.start at the beginning of the list 2.for each item in the list i.examine the item - if that item is the one you are seeking, then you are done ii.if it is not the item you are seeking, then go on to the next item in the list if you reach the end of the list and have not found the item, then it was not in the list sequential search guarantees that you will find the item if it is in the list – but it is not very practical for very large databases – worst case: you may have to look at every entry in the list

Binary Search binary search involves continually cutting the desired search list in half until the item is found – the algorithm is only applicable if the list is ordered e.g., a list of numbers in increasing order e.g., a list of words in alphabetical order binary search for finding an item in an ordered list 1.initially, the potential range in which the item could occur is the entire list 2.as long as items remain in the potential range and the desired item has not been found, repeatedly i.examine at the middle entry in the potential range ii.if the middle entry is the item you are looking for, then you are done iii.if the middle entry is greater than the desired item, then reduce the potential range to those entries left of the middle iv.if the middle entry is less than the desired item, then reduce the potential range to those entries right of the middle by repeatedly cutting the potential range in half, binary search can reach the value very quickly

Binary Search Example suppose you have a sorted list of state names, and want to find Illinois 1.start by examining the middle entry (Missouri) since Missouri comes after Illinois alphabetically, can eliminate it and all entries that appear to the right 2.next, examine the middle of the remaining entries (Florida) since Florida comes before Illinois alphabetically, can eliminate it and all entries that appear to the left 3.next, examine the middle of the remaining entries (Illinois) the desired entry is found

Search Analysis sequential search – in the worst case, the item you are looking for is in the last spot in the list (or not in the list at all) as a result, you will have to inspect and compare every entry in the list – the amount of work required is proportional to the list size  sequential search is an O(N) algorithm binary search – in the worst case, you will have to keep halving the list until it gets down to a single entry each time you inspect/compare an entry, you rule out roughly half the remaining entries – the amount of work required is proportional to the logarithm of the list size  binary search is an O(log N) algorithm imagine searching a phone book of the United States (300 million people) sequential search requires at most 300 million inspections/comparisons binary search requires at most  log 2 (300,000,000)  = 29 inspections/comparisons

The binary search algorithm in pseudocode