CS212: DATASTRUCTURES Lecture 3: Searching 1. Lecture Contents  searching  Sequential search algorithm.  Binary search algorithm. 2.

Slides:



Advertisements
Similar presentations
Search and Recursion CS221 – 2/23/09. List Search Algorithms Linear Search: Simple search through unsorted data. Time complexity = O(n) Binary Search:
Advertisements

Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
Searching Chapter Chapter Contents The Problem Searching an Unsorted Array Iterative Sequential Search Recursive Sequential Search Efficiency of.
Monday, 12/9/02, Slide #1 CS 106 Intro to CS 1 Monday, 12/9/02  QUESTIONS??  On HW #5 (Due 5 pm today)  Today:  Recursive functions  Reading: Chapter.
Problem Solving #6: Search & Sort ICS Outline Review of Key Topics Review of Key Topics Problem 1: Recursive Binary Search … Problem 1: Recursive.
Searching Algorithms. Lecture Objectives Learn how to implement the sequential search algorithm Learn how to implement the binary search algorithm To.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
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
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Searching Chapter 18 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
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
Sorting and Searching Arrays CSC 1401: Introduction to Programming with Java Week 12 – Lectures 1 & 2 Wanda M. Kunkle.
CS212: DATASTRUCTURES Lecture 3: Searching 1. Search Algorithms Sequential Search It does not require an ordered list. Binary Search It requires an ordered.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Searching Searching: –Mainly used for: Fetching / Retrieving the Information such as, –Select query on a database. –Important thing is: Retrieval of Information.
Searching Chapter 2.
Building Java Programs Chapter 13 Searching reading: 13.3.
COSC 2006 Data Structures I Recursion II
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.
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
1 Lecture 5: Part 1 Searching Arrays Searching Arrays: Linear Search and Binary Search Search array for a key value Linear search  Compare each.
Searching Given a collection and an element (key) to find… Output –Print a message (“Found”, “Not Found) –Return a value (position of key ) Don’t modify.
Chapter 2 Array Data Structure Winter Array The Array is the most commonly used Data Storage Structure. It’s built into most Programming languages.
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.
Searching CS 105 See Section 14.6 of Horstmann text.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
 2006 Pearson Education, Inc. All rights reserved 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.
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. Linear (Sequential) Search Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and.
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
1 Section 2.1 Algorithms. 2 Algorithm A finite set of precise instructions for performing a computation or for solving a problem.
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 Algorithms Sequential Search – inspects every items in a sequential manner. Example, in an array, all values in the array are checked from index.
Searching Chapter 13 Objectives Upon completion you will be able to:
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 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.
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.
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.
Chapter 3: Sorting and Searching Algorithms 3.1 Searching Algorithms.
Searching CS 110: Data Structures and Algorithms First Semester,
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
BINARY SEARCH CS16: Introduction to Data Structures & Algorithms Thursday February 12,
Chapter 16: Searching, Sorting, and the vector Type.
Searching Given a collection and an element (key) to find… Output
Sorting Data are arranged according to their values.
Searching CSCE 121 J. Michael Moore.
Searching.
Sorting Data are arranged according to their values.
Searching: linear & binary
Cs212: DataStructures Lecture 3: Searching.
SORTING, SEARCHING AND HASHING TECHNIQUES
Presentation transcript:

CS212: DATASTRUCTURES Lecture 3: Searching 1

Lecture Contents  searching  Sequential search algorithm.  Binary search algorithm. 2

Search Algorithms  Searching, the process used to find the location of a target among a list of objects.  In this chapter, we will study searches that work with arrays Search Algorithms Sequential Search Binary Search 3

Search Algorithms 1. Sequential search. 1. It’s not requires an ordered list. 2. Binary search.  It requires an ordered list. 4

1/ Sequential (Linear) Search 5  Search an array or list by checking items one at a time.  Sequential 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 target(key).  Eighter we find it,  or we reach the end of the list!

Locating data in unordered list. 6

Sequential Search Algorithm  The searching algorithm requires three parameters: 1. The list. 2. An index to the last element in the list. 3. The target. 7

Sequential Search Algorithm algorithm SeqSearch (val list, val last, val target ) Locate the target in an unordered list of size elements. PRE list must contain at least one element. last is index to last element in the list. target contains the data to be located. POST if found – matching index stored in Location. if not found – (-1) stored in Location RETURN Location 8

Sequential Search Algorithm looker = 0 loop (looker < last AND target not equal list(looker)) looker = looker + 1 if (target == list[looker] ) Location= looker else Location = -1 End If return Location end SeqSearch 9

Recursive Sequential Search Algorithm algorithm RecSeqsearch (val I ), val J ), val target ) Locate the target in an unordered list of size elements. PRE I is is index to first element in the list. last is index to last element in the list. target contains the data to be located. POST if found – matching index stored in Location. if not found – (-1) stored in Location RETURN Location 10

Recursive Sequential Search Algorithm 11 if (A[I] == target) location = I else if (I == J) location = -1 else location = RecSeqsearch(I+1,J, target) End If Return location End RecSeqsearch

Recursive sequential search (2) 12 Algorithm sequentialSearch (item,list, listSize ) Pre item contains a value, listSize contains the actual size of the array list Post find the location of item in list Return either the item found and its location is returned or not and -1 returned if (listSize == 0) return -1 if (list[listSize-1] == item) return listSize-1 else return sequentialSearch (item, list, listSize-1) End sequentialSearch

2/ Binary search algorithm  Search a sorted array by repeatedly dividing the search interval in half.  A fast way to search a sorted array is to use a binary search. 13

Binary search algorithm Test the data in the element at the middle of the array. it is in the first half before middle it is in the second half after middle Test the data in the element at the middle of the array. it is in the second half! it is in the second half! it is in the second half! it is in the second half! it is in the first half! it is in the first half! it is in the first half! it is in the first half! Calculate the middle element If the middle element equals to the Target, the algorithm stops Target < middle element Target > middle element Target < middle Target > middle Calculate the middle element 14

target > A[mid] first = mid target == A[mid] target < A[mid] last = mid -1 mid=(first+last)/2

16 target < A[mid] last = mid -1 target > A[mid] first = mid +1 target < A[mid] last = first  not found  stop target > A[mid] first = mid +1

Recursive Binary search algorithm algorithm RecBinarySearch (val First, val last,val target ) Locate the target in an ordered list of size elements. PRE list must contain at least one element. First is index to first element in the list. last is index to last element in the list. target contains the data to be located. POST if found – matching index stored in Location if not found – (-1) stored in Location RETURN Location 17

Recursive search algorithm m := if target = a m then Location= m else if (first=last) then Location= -1 else if (target < a m ) then Location =binarySearch(first, m-1, target) else if (target > a m ) then Location=binarySearch(m+1, last, target ) Return Location End RecBinarySearch base cases recursive calls 18

Example 19 [4][3][2][1][0] BinarySearch (0,4,20) M=0+4/2=2 20 >7 then binarySearch(3, 4,20) BinarySearch (0,4,20) M=0+4/2=2 20 >7 then binarySearch(3, 4,20) BinarySearch (3,4,20) M=3+4/2=3 20 >11 then binarySearch(4, 4,20) BinarySearch (3,4,20) M=3+4/2=3 20 >11 then binarySearch(4, 4,20) BinarySearch (4,4,20) M=4+4/2=2 20 == 20 BinarySearch (4,4,20) M=4+4/2=2 20 == 20 Return 4 Recursive call

Binary search algorithm (iterative) 20 Algorithm BinarySearch (list, key, listSize ) Search an ordered list using Binary Search PRE list must contain at least one element. listSize is the actual size of the list. key contains the data to be located. POST if found – matching index stored in Location if not found (-1) stored in Location RETURN Location

Binary search in iterative algorithm 21 first=0 last=listSize-1 while (first<= last) { mid = if ( list [mid] == key) return mid else if (list[mid] < key) first = mid + 1 else last = mid - 1 } return -1 End BinarySearch

References: Text book, chapter2: Searching End Of Chapter 22