Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.

Slides:



Advertisements
Similar presentations
Lesson 8 Searching and Sorting Arrays 1CS 1 Lesson 8 -- John Cole.
Advertisements

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Chapter 9: Searching, Sorting, and Algorithm Analysis
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Recursion. Binary search example postponed to end of lecture.
Recursion Chapter 11 Chapter 11.
Searching Chapter Chapter Contents The Problem Searching an Unsorted Array Iterative Sequential Search Recursive Sequential Search Efficiency of.
Searching Algorithms. Lecture Objectives Learn how to implement the sequential search algorithm Learn how to implement the binary search algorithm To.
1 Lecture 23:Applications of Arrays Introduction to Computer Science Spring 2006.
Recursion.
Chapter 11 Recursion Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
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 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Linear Search Binary Search ; Reading p Selection Sort ; Reading p
Searching Arrays Linear search Binary search small arrays
Searching and Sorting Arrays
Elementary Data Structures and Algorithms
Searching Chapter Chapter Contents The Problem Searching an Unsorted Array Iterative Sequential Search Recursive Sequential Search Efficiency of.
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Linear Search Binary Search -Reading p
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 9 Searching.
1 Search Algorithms Sequential Search (Linear Search) Binary Search Rizwan Rehman Centre for Computer Studies Dibrugarh University.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Searching and Sorting Arrays.
Chapter 16: Searching, Sorting, and the vector Type.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
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
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)
Lecture 12. Searching Algorithms and its analysis 1.
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.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
1 Searching and Sorting Linear Search Binary Search.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 9 Searching Arrays.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
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.  This is a technique for searching a particular element in sequential manner until the desired element is found.  If an element is found.
L. Grewe.  An array ◦ stores several elements of the same type ◦ can be thought of as a list of elements: int a[8]
Chapter 13 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Recursive Functions for Tasks(13.1) Recursive Functions.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 14 Recursion.
CSC 211 Data Structures Lecture 13
Chapter 14: Searching and Sorting
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.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
Searching CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
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.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Chapter 10: Class Vector and String, and Enumeration Types Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting.
CS212: DATASTRUCTURES Lecture 3: Searching 1. Lecture Contents  searching  Sequential search algorithm.  Binary search algorithm. 2.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Recursion.
Chapter 16: Searching, Sorting, and the vector Type.
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 and Sorting Arrays
Introduction to Search Algorithms
Search Algorithms Sequential Search (Linear Search) Binary Search
Chapter 14 Recursion. Chapter 14 Recursion Overview 14.1 Recursive Functions for Tasks 14.2 Recursive Functions for Values 14.3 Thinking Recursively.
Searching and Sorting Linear Search Binary Search ; Reading p
Searching and Sorting Arrays
Standard Version of Starting Out with C++, 4th Edition
Searching and Sorting Arrays
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Presentation transcript:

Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari

Searching Algorithms Searching is the process of determining whether or not a given value exists in a data structure or a storage media. We will study two searching algorithms: – Linear Search – Binary Search Ms. Manal Al-Asmari

Linear Search The linear (or sequential) search algorithm on an array is: –Start from beginning of an array/list and continues until the item is found or the entire array/list has been searched. –Sequentially scan the array, comparing each array item with the searched value. –If a match is found; return the index of the matched element; otherwise return –1. Note: linear search can be applied to both sorted and unsorted arrays. Ms. Manal Al-Asmari

Linear Search int seqSearch(int[] list, int listLength, int searchItem) { int loc; boolean found = false; for (loc = 0; loc < listLength; loc++) if (list[loc] == searchItem) { found = true; break; } if (found) return loc; else return -1; } Ms. Manal Al-Asmari

Linear Search Benefits – Easy to understand – Array can be in any order Ms. Manal Al-Asmari

Linear Search Disadvantages – Inefficient for array of N elements Examines N/2 elements on average for value in array, N elements for value not in array Ms. Manal Al-Asmari

Binary Search Binary search looks for an item in an array/list using divide and conquer strategy Ms. Manal Al-Asmari

Binary Search –Binary search algorithm assumes that the items in the array being searched is sorted –The algorithm begins at the middle of the array in a binary search –If the item for which we are searching is less than the item in the middle, we know that the item won’t be in the second half of the array –Once again we examine the “middle” element –The process continues with each comparison cutting in half the portion of the array where the item might be Ms. Manal Al-Asmari

Binary Search Ms. Manal Al-Asmari Binary search uses a recursive method to search an array to find a specified value The array must be a sorted array: a[0]≤a[1]≤a[2]≤... ≤ a[finalIndex] If the value is found, its index is returned If the value is not found, -1 is returned Note: Each execution of the recursive method reduces the search space by about a half

Execution of Binary Search Ms. Manal Al-Asmari

Execution of Binary Search

Binary Search Algorithm public static int binarySearch(int[] list, int listLength,int searchItem){ int first = 0; int last = listLength - 1; int mid; boolean found = false; while (first <= last){ mid = (first + last) / 2; if (list[mid] == searchItem) found = true; else if (list[mid] > searchItem) last = mid - 1; else first = mid + 1;} if (found) return mid; else return –1; } Ms. Manal Al-Asmari

1.There is no infinite recursion On each recursive call, the value of first is increased, or the value of last is decreased If the chain of recursive calls does not end in some other way, then eventually the method will be called with first larger than last 2.Each stopping case performs the correct action for that case If first > last, there are no array elements between a[first] and a[last], so key is not in this segment of the array, and result is correctly set to -1 If key == a[mid], result is correctly set to mid Ms. Manal Al-Asmari Key Points in Binary Search

Ms. Manal Al-Asmari Key Points in Binary Search 3.For each of the cases that involve recursion, if all recursive calls perform their actions correctly, then the entire case performs correctly If key < a[mid], then key must be one of the elements a[first] through a[mid-1], or it is not in the array The method should then search only those elements, which it does The recursive call is correct, therefore the entire action is correct

Ms. Manal Al-Asmari Key Points in Binary Search If key > a[mid], then key must be one of the elements a[mid+1] through a[last], or it is not in the array The method should then search only those elements, which it does The recursive call is correct, therefore the entire action is correct The method search passes all three tests: Therefore, it is a good recursive method definition

Ms. Manal Al-Asmari Efficiency of Binary Search The binary search algorithm is extremely fast compared to an algorithm that tries all array elements in order –About half the array is eliminated from consideration right at the start –Then a quarter of the array, then an eighth of the array, and so forth

Sequential Search Analysis The statements in the for loop are repeated several times For each iteration of the loop, the search item is compared with an element in the list When analyzing a search algorithm, you count the number of comparisons Suppose that L is a list of length n The number of key comparisons depends on where ”in the list” the search item is located Ms. Manal Al-Asmari

Sequential Search Analysis (continued) Best case The item is the first element of the list You make only one key comparison Worst case The item is the last element of the list You make n key comparisons What is the average case ? Ms. Manal Al-Asmari

Sequential Search Analysis (continued) To determine the average case Consider all possible cases Find the number of comparisons for each case Add them and divide by the number of cases Ms. Manal Al-Asmari

On average, a successful sequential search searches half the list Ms. Manal Al-Asmari Sequential Search Analysis (continued) Average Case:

Ms. Manal Al-Asmari