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.

Slides:



Advertisements
Similar presentations
Zabin Visram Room CS115 CS126 Searching
Advertisements

Lesson 8 Searching and Sorting Arrays 1CS 1 Lesson 8 -- John Cole.
CSE Lecture 3 – Algorithms I
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Insertion sort, Merge sort COMP171 Fall Sorting I / Slide 2 Insertion sort 1) Initially p = 1 2) Let the first p elements be sorted. 3) Insert the.
Search and Recursion CS221 – 2/23/09. List Search Algorithms Linear Search: Simple search through unsorted data. Time complexity = O(n) Binary Search:
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 Arrays Linear search Binary search small arrays
Searching and Sorting Arrays
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.
Sorting and Searching Arrays CSC 1401: Introduction to Programming with Java Week 12 – Lectures 1 & 2 Wanda M. Kunkle.
The Binary Search Textbook Authors: Ken Lambert & Doug Nance PowerPoint Lecture by Dave Clausen
Chapter 8 ARRAYS Continued
Searching Searching: –Mainly used for: Fetching / Retrieving the Information such as, –Select query on a database. –Important thing is: Retrieval of Information.
Asymptotic Notations Iterative Algorithms and their analysis
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Searching and Sorting Arrays.
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.
Iterative Algorithm Analysis & Asymptotic Notations
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)
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 Searching and Sorting Linear Search Binary Search.
SEARCHING (Linear/Binary). Searching Algorithms  method of locating a specific item of information in a larger collection of data.  two popular 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.
Searching. RHS – SOC 2 Searching A magic trick: –Let a person secretly choose a random number between 1 and 1000 –Announce that you can guess the number.
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.
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.
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.
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.
CSE 2341 Honors Professor Mark Fontenot Southern Methodist University Note Set 03.
CSCI 130 Array Searching. Methods of searching arrays Linear –sequential - one at the time –set does not have to be ordered Binary –continuously cutting.
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.
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.
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 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.
1 Asymptotic Notations Iterative Algorithms and their analysis Asymptotic Notations –Big O,  Notations Review of Discrete Math –Summations –Logarithms.
Sorting and Searching. Selection Sort  “Search-and-Swap” algorithm 1) Find the smallest element in the array and exchange it with a[0], the first element.
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
Catie Welsh April 20,  Program 4 due Wed, April 27 th by 11:59pm  Final exam, comprehensive ◦ Friday, May 6th, 12pm  No class Friday - Holiday.
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.
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.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
Chapter 3: Sorting and Searching Algorithms 3.1 Searching 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.
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 Arrays
Searching and Sorting Arrays
Searching and Sorting Arrays
Binary Search Binary Search Algorithm
Presentation transcript:

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

Application Phone Number in List Word in Dictionary Accessing Website

LINEAR SEARCH

DEFINITION Sequential SearchLook for successive elementsStops when found the valueStops when no value has been foundGood for small arrays

Ad/Disad-Vantages Advantages Simple Does not necessarily need to be in order Good for item located in front list Disadvantages Not so efficient Looking at beginning of the list only Only efficient if the data is sorted

Algorithm in General For all the items in the list Compare the item with the desired item If the item was found Return the index value of the current item Endif EndFor Return -1 because the item was not found

Code in C++ #include using namespace std; const int NUMEL=10; int linearSearch(int[], int, int); int main() { int nums[NUMEL]={5,10,22,32,45,67,73,98,99,101}; int item,location; cout<<"Enter the item you are searching for: "; cin>>item; location=linearSearch(nums,NUMEL,item); if(location> -1) cout<<"The item was found at index location "<<location<<endl; else cout<<"The item was not found in the array\n"; return 0; }

Code in C++ (Cont.) //this function returns the location of key in the list //a -1 is returned if the value is not found int linearSearch(int list[ ],int size,int key) { int i; for(i=0;i<size;i++) { if (list[i]==key) return i; } return -1; }

Example Default Try to find “32” “5” with “32” = NO “10” with “32” = NO “22” with “32” = NO “32” with “32” = YES

BINARY SEARCH

DEFINITION Efficient if sortedAll must be sorted firstDivide the array into half

Process of Binary Search 1 st. Look at the middle element2 nd. If equal, then finish3 rd. If less, do left binary searching 4 th. If greater, do right binary searching

Algorithm Set the lower index to 0 Set the upper index to one less than the size of the list Begin with the first item in the list While the lower index is less than or equal to the upper index Set the midpoint index to the integer average of the lower and upper index values Compare the desired item to the midpoint element If the desired element equals the midpoint element Return the index value of the current item Else if the desired element is greater than the midpoint element Set the lower index value to the midpoint value plus 1 Else if the desired element is less than the midpoint element Set the upper index value to the midpoint value minus1 Endif EndWhile Return -1 because the item was not found

Code in C++ #include using namespace std; const int NUMEL=10; int binarySearch(int[], int, int); int main() { int nums[NUMEL]={5,10,22,32,45,67,73,98,99,101}; int item,location; cout<<"Enter the item you are searching for: "; cin>>item; location=binarySearch(nums,NUMEL,item); if(location> -1) cout<<"The item was found at index location "<<location<<endl; else cout<<"The item was not found in the array\n"; return 0; }

Code in C++ //this function returns the location of key in the list //a -1 is returned if the value is not found int binarySearch(int list[],int size,int key) { int left,right,midpt; left=0;//set the lower index to 0 right=size-1;//set the upper index to one less than the size of the list while(left<=right) { midpt=(int)((left+right)/2);//set the midpoint index if(key==list[midpt])//compare the desired item to the midpoint element { return midpt; } else if(key>list[midpt]) left=midpt+1;//set the lower index value to midpoint value plus 1 else right=midpt-1;//set the upper index value to midpoint value minus 1 } return -1;//return -1 if the item was not found }

Example Binary 1 Default st pass nd pass10 Find item “10” 1 st pass : Left = 0, Right = 9, Mid = 4, List[4]=45. Thus Key=10<List[4]. Right=3 2 nd pass : Left = 0, Right = 3, Mid = 1, List[1]=10. Thus Key=10==List[1]. Done

Example Binary 2 Find item “32” 1 st pass : Left = 0, Right = 9, Mid = 4, List[4]=45. Thus Key=32<List[4]. Right=3 Default st pass nd pass rd pass th pass32

Example Binary 2(Cont.) Find item “32” 2 nd pass : Left = 0, Right = 3, Mid = 1, List[1]=10. Thus Key=32>List[1]. Left = 2 Default st pass nd pass rd pass th pass32

Example Binary 2(Cont.) Find item “32” 3 rd pass : Left = 2, Right = 3, Mid = 2, List[2]=22.Thus Key=32>List[2]. Left = 3 Default st pass nd pass rd pass th pass32

Example Binary 2(Cont.) Find item “32” 4 th pass : Left = 3, Right =3, Mid = 3, List[3]=32. Thus Key=32==List[3]. Done Default st pass nd pass rd pass th pass32

COMPARISON LINEAR/BINARY LINEAR SEARCH O(n) Best for unsorted array (sequential access) Needs more space and time complexity. Not suitable for large set of data BINARY SEARCH O(nlog2n) Best for sorted array (liner access) Best practice in application since most data is sorted in real world application Suitable for large set of data