Zabin Visram Room CS115 CS126 Searching

Slides:



Advertisements
Similar presentations
Data Structures Through C
Advertisements

Chapter 9 continued: Quicksort
Topic 14 Searching and Simple Sorts "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.
Lesson 8 Searching and Sorting Arrays 1CS 1 Lesson 8 -- John Cole.
Building Java Programs Chapter 13
MATH 224 – Discrete Mathematics
HST 952 Computing for Biomedical Scientists Lecture 9.
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.
Search and Recursion CS221 – 2/23/09. List Search Algorithms Linear Search: Simple search through unsorted data. Time complexity = O(n) Binary Search:
Searching and Sorting I 1 Searching and Sorting 1.
Searching Algorithms. Lecture Objectives Learn how to implement the sequential search algorithm Learn how to implement the binary search algorithm To.
Data Structures Using Java1 Chapter 8 Search Algorithms.
1 Lecture 23:Applications of Arrays Introduction to Computer Science Spring 2006.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
1 Searching Algorithms Overview  What is Searching?  Linear Search and its Implementation.  Brief Analysis of Linear Search.  Binary Search and its.
Searching Arrays Linear search Binary search small arrays
Searching Chapter Chapter Contents The Problem Searching an Unsorted Array Iterative Sequential Search Recursive Sequential Search Efficiency of.
Searching1 Searching The truth is out there.... searching2 Serial Search Brute force algorithm: examine each array item sequentially until either: –the.
1 Search Algorithms Sequential Search (Linear Search) Binary Search Rizwan Rehman Centre for Computer Studies Dibrugarh University.
Data Structures Using Java1 Chapter 8 Search Algorithms.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
CPT: Search/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss searching: its implementation,
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
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.
Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching.
Data Structures Using C++1 Search Algorithms Sequential Search (Linear Search) Binary Search.
Computer Science 101 Fast Searching and Sorting. Improving Efficiency We got a better best case by tweaking the selection sort and the bubble sort We.
Applications of Arrays (Searching and Sorting) and Strings
Chapter 19: Searching and Sorting Algorithms
Lecture 12. Searching Algorithms and its analysis 1.
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Algorithms and Data Structures Search Algorithms Course No.:
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.
CS 162 Intro to Programming II Searching 1. Data is stored in various structures – Typically it is organized on the type of data – Optimized for retrieval.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Searching Course Lecture Slides 28 May 2010 “ Some things Man was never.
Searching. Linear (Sequential) Search Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and.
CSC 211 Data Structures Lecture 13
“Enthusiasm releases the drive to carry you over obstacles and adds significance to all you do.” – Norman Vincent Peale Thought for the Day.
Review 1 Arrays & Strings Array Array Elements Accessing array elements Declaring an array Initializing an array Two-dimensional Array Array of Structure.
Chapter 9 slide 1 Introduction to Search Algorithms Search: locate an item in a list (array, vector, table, etc.) of information Two algorithms (methods):
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Selection Sort Sorts an array by repeatedly finding the smallest.
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
CS Class 22 Today  A word from the Real World What happens when software goes bad…  Binary Search Announcements  Exam 3 – Nov. 25 th in class.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Searching & Sorting.
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.
CAPTER 6 SEARCHING ALGORITHM. WHAT IS SEARCHING Process of finding an item in an array Two ways to find an item By position / By value.
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.
Data Structures and Algorithms Searching Algorithms M. B. Fayek CUFE 2006.
Visual C++ Programming: Concepts and Projects Chapter 8A: Binary Search (Concepts)
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Chapter 3: Sorting and Searching Algorithms 3.1 Searching Algorithms.
 1 Searching. Why searching? Searching is everywhere. Searching is an essential operation for a dictionary and other data structures. Understand the.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 8A Binary Search (Concepts)
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
1 compares each element of the array with the search key. works well for small arrays or for unsorted arrays works for any table slow can put more commonly.
Searching Arrays Linear search Binary search small arrays
Searching and Sorting Searching algorithms with simple arrays
Searching and Sorting Arrays
Searching: linear & binary
Linear Search Binary Search Tree
Data Structures: Searching
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Presentation transcript:

Zabin Visram Room CS115 CS126 Searching Binary Search

Binary Search Sequential search is not efficient for large lists as it searches half the list, on average Another search algorithm – Binary search Very fast But can only be performed on ordered lists

Example If you are looking for you friends number in the phone book, you may decide to look from half way , you know the book is ordered alphabetically therefore if you decide the name is in the right half – you can disregard the left half – throw it away – just concentrate on the right half – this way your search is dramatically reduced – only have ½ book o search – do the same process again – until eventually u either find the name or decide its not there – binary search is the same

Divide & Conquer technique When a list is sorted and we have random access to the list as in an array we can take advantage of this additional structure in our search methods. binary search algorithm uses the “Divide & Conquer” method to search the list

Divide & Conquer technique First the search item is compared with the middle element of the list. If the search item is less than the middle element of the list, we restrict the search to the first half of the list; otherwise we search the second half of the list

Binary Search Consider a sorted list of length 12 4 8 19 25 34 39 45 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] 4 8 19 25 34 39 45 48 66 75 89 95 list

Binary Search Suppose we want to find 75 Entire list is searched – compare 75 with middle element in list, list[5] (which is 39) Because 75 Is in list [6]..list[11] we restrict search there Search list list [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] 4 8 19 25 34 39 45 48 66 75 89 95 MID

Binary Search The process is now repeated on the list [6]..list[11] which is a list of 6 Search list list [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] 4 8 19 25 34 39 45 48 66 75 89 95

Task - individually During a binary search , which elements in the array 4 8 12 14 20 24 Are compared to the target when the target is a. 2 b.8 c.15

Answer a. 12 and 4 B. 12, 4 and 8 C. 12, 20 and 14

most of the array is not searched at all , saving much time - thus BS algorithm is very fast. But how fast ? Counting the comparisons I.e each time algorithm divides the array in half - can provide us a measure of the algorithms efficiency

Binary search Analysis Suppose we are working with a sorted array of n integers Initially first = 0 and (because an array index in Java starts at 0 and n denotes the number of elements in the list(length) then last = n-1 The element midway in an array indexed from 0 to n-1 is mid = (n – 1) / 2. If the target is less than the value at mid, then since the array is sorted we can be certain that the target element is not in the array from positions mid to n-1. In pseudo code the idea is:

mid = [(mid+1) + (n-1)] / 2 low = 0; high = n -1; mid = (low + high) / 2; if target = array[mid] then return mid else if target < array[mid] then target is not in range mid .. n-1 discard upper half by setting high = mid –1; target is not in the 0 ..mid range; discard lower half by setting low = mid + 1; repeat with new range until possible array is of length 1.     If the lower half of the list 0..mid was discarded by the above algorithm then the next probe would be at mid = [(mid+1) + (n-1)] / 2

Binary search Analysis which is the midpoint of the upper half. Continuing in this way, unless we find the target, we halve the list each time until it becomes of size one and we either have found the target or it was not present originally. The complexity of such an algorithm – the work required to complete it given a list of size n initially – is the number of probes that are required. That is, how many times can a list of size n be halved before it becomes of size one?

A standard binary search method can then be written   public int binarySearch(int target) { int low = 0; int high = a.length – 1; int mid; while (low <= high) { mid = (low + high) / 2;   if (target == a[mid]) //match   return mid;   else   if (target < a[mid]) //search low end of array   high = mid – 1; else //search high end of array   low = mid + 1; } return –1;  }  

Binary Search Vs Linear Search A sequential search of an array looks at the first item, the second item, and so on until it either finds a particular item or determines that the item does not occur in the group A binary search of an array requires that the array be sorted . It looks first at the middle of the array to determine in which half the desired item can occur. The search repeats this strategy on only this half of the array

Binary Search Vs Linear Search The benefit of binary search over linear search becomes significant for lists over about 100 elements. For smaller lists linear search may be faster because of the speed of the simple increment compared with the divisions needed in binary search. The general moral is that for large lists binary search is very much faster than linear search, but is not worth while for small lists.

Demonstration http://www.cosc.canterbury.ac.nz/people/mukundan/dsal/BSearch.html  in comparison – linear search http://www.cosc.canterbury.ac.nz/people/mukundan/dsal/LSearch.html