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.

Slides:



Advertisements
Similar presentations
Recursion.
Advertisements

Zabin Visram Room CS115 CS126 Searching
Lesson 8 Searching and Sorting Arrays 1CS 1 Lesson 8 -- John Cole.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Search and Recursion CS221 – 2/23/09. List Search Algorithms Linear Search: Simple search through unsorted data. Time complexity = O(n) Binary Search:
Arrays (II) H&K Chapter 8 Instructor – Gokcen Cilingir Cpt S 121 (July 14, 2011) Washington State University.
Searching Arrays. COMP104 Lecture 22 / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and return its index if the.
Searching Algorithms. Lecture Objectives Learn how to implement the sequential search algorithm Learn how to implement the binary search algorithm To.
C++ Plus Data Structures
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 Section 3.5 Recursive Algorithms. 2 Sometimes we can reduce solution of problem to solution of same problem with set of smaller input values When such.
J. Michael Moore Searching & Sorting CSCE 110. J. Michael Moore Searching with Linear Search Many times, it is necessary to search an array to find a.
CHAPTER 11 Searching. 2 Introduction Searching is the process of finding a target element among a group of items (the search pool), or determining that.
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 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Linear Search Binary Search -Reading p
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Searching and Sorting Arrays.
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.
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.
1 Lecture 5: Part 1 Searching Arrays Searching Arrays: Linear Search and Binary Search Search array for a key value Linear search  Compare each.
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.
Starting Out with C++, 3 rd Edition 1 Searching an 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.
SEARCHING. Vocabulary List A collection of heterogeneous data (values can be different types) Dynamic in size Array A collection of homogenous data (values.
C# PROGRAMMING Searching & Sorting. Objective/Essential Standard Essential Standard 3.00 Apply Advanced Properties of Arrays Indicator 3.03 Apply procedures.
Searching Dr. Jose Annunziato. Linear Search Linear search iterates over an array sequentially searching for a matching element int linearSearch(int haystack[],
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]
LAB#7. Insertion sort In the outer for loop, out starts at 1 and moves right. It marks the leftmost unsorted data. In the inner while loop, in starts.
CSC 211 Data Structures Lecture 13
LAB#6. 2 Overview Before we go to our lesson we must know about : 1. data structure. 2.Algorithms. data structure is an arrangement of data in a computer.
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 Algorithms Sequential Search – inspects every items in a sequential manner. Example, in an array, all values in the array are checked from index.
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.
Objectives At the end of the class, students are expected to be able to do the following: Understand the searching technique concept and the purpose of.
Sorting & Searching Review. Selection Sort 1. Find the smallest element 2. Move to the front of the array (swap with front) 3. Repeat Steps 1&2, but ignoring.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Searching When we maintain a collection of data,
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.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
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.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
Hossain Shahriar Announcement and reminder! Tentative date for final exam shown below, please choose a time slot! December 19.
1 Lecture 11 b Searching b Linear Search b Binary Search recursiverecursive iterativeiterative polymorphicpolymorphic.
1 Data Structures CSCI 132, Spring 2014 Lecture23 Analyzing Search Algorithms.
 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.
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 CSCE 121 J. Michael Moore.
Adapted from Pearson Education, Inc.
Searching and Sorting Arrays
Searching: linear & binary
Topic 1: Problem Solving
Linear Search Binary Search Tree
Cs212: DataStructures Lecture 3: Searching.
Searching and Sorting Arrays
Presentation transcript:

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 is practical when the list has only a few elements, or when performing a single search in an unordered list. Look at every element : This is a very straightforward loop comparing every element in the array with the key. As soon as an equal value is found, it returns. If the loop finishes without finding a match, the search failed and -1 is returned.

int LinearSearch(const int *Array, const int Size, const int ValToSearch) { bool NotFound = true; int i = 0; while(i < Size && NotFound) { if(ValToSearch != Array[i]) i++; else NotFound = false; } if( NotFound == false ) return i; else return -1; }

Binary search algorithm Search a sorted array by repeatedly dividing the search interval in half. Begin with an interval covering the whole array. If the value of the search key is less than the item in the middle of the interval, narrow the interval to the lower half. Otherwise narrow it to the upper half. Repeatedly check until the value is found or the interval is empty. A fast way to search a sorted array is to use a binary search. The idea is to look at the element in the middle. If the key is equal to that, the search is finished. If the key is less than the middle element, do a binary search on the first half. If it's greater, do a binary search of the second half.

Algorithm Algorithm is quite simple. It can be done either recursively or iteratively: 1. Get the middle element; 2. If the middle element equals to the searched value, the algorithm stops; 3. Otherwise, two cases are possible: o searched value is less than the middle element. In this case, go to the step 1 for the part of the array before middle element. o searched value is greater than the middle element. In this case, go to the step 1 for the part of the array after middle element.

Example : we have the array X[12]: Search for b=12 mid = (0+11)/2 = 5. Compare b with X[5]: 12<20. So search in left half X[0..4] mid = (0+4)/2 = 2. Compare b with X[2]: 12 > 7. So search right half X[3..4] mid = (3+4)/2 = 3.Compare b with X[3]: b=X[3]=12. Return 3.

8 Example: Express the linear search algorithm as a recursive procedure. 8 Recursive Algorithms (the linear search algorithm ) ALGORITHM 1 A Recursive Linear Search Algorithm. procedure search(i,j,x: i,j,x integers, 1 <=i<=n, 1<=j<=n) if a i = x then location := i else if i = j then location : = 0 else search(i+1,j, x)

9 Example: Construct a recursive version of a binary search algorithm 9 ALGORITHM 2 -1 A Recursive Binary Search Algorithm. procedure binarySearch(i,j,x: i,j,x integers, 1 <= i <= n, 1<= j <= n) m := if x = a m then location := m else if (x < a m and i < m ) then binarySearch( x, i, m-1) else if (x > a m and j > m ) then binarySearch(x, m+1, j) else location := 0 Recursive Algorithms (the binary search algorithm )

Lecturer: Shaykhah 10 Example: Construct a recursive version of a binary search algorithm 10 ALGORITHM 2-2 A Recursive Binary Search Algorithm. procedure binarySearch(i,j,x: i,j,x integers, 1 <= i <= n, 1<= j <= n) m := if x = a m then location := m else if (i=j) then location := -1 else if (x < a m ) then binarySearch( x, i, m-1) else if (x > a m ) then binarySearch(x, m+1, j) Recursive Algorithms (the binary search algorithm ): another way