Cs212: DataStructures Lecture 3: Searching.

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.
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.
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 Searching and Sorting Linear Search Binary Search.
Searching CS 105 See Section 14.6 of Horstmann text.
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.
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.
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 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.
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.
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.
Searching Arrays Linear search Binary search small arrays
Recursive Definitions
Searching and Sorting Searching algorithms with simple arrays
Chapter 9: Sorting and Searching Arrays
Data Structures I (CPCS-204)
OBJECT ORIENTED PROGRAMMING II LECTURE 23 GEORGE KOUTSOGIANNAKIS
Searching Given a collection and an element (key) to find… Output
COP 3503 FALL 2012 Shayan Javed Lecture 15
Lecture 14 Searching and Sorting Richard Gesick.
Chapter 7 Single-Dimensional Arrays
Sorting Data are arranged according to their values.
Topics discussed in this section:
Searching CSCE 121 J. Michael Moore.
CS 3343: Analysis of Algorithms
Adapted from Pearson Education, Inc.
Searching.
CSC215 Lecture Algorithms.
Sorting Data are arranged according to their values.
Lecture 11 Searching and Sorting Richard Gesick.
Searching and Sorting Arrays
MSIS 655 Advanced Business Applications Programming
Searching and Sorting 1-D Arrays
UNIT – V PART - I Searching By B VENKATESWARLU, CSE Dept.
Searching: linear & binary
Searching CLRS, Sections 9.1 – 9.3.
Linear Search Binary Search Tree
24 Searching and Sorting.
Sequential Search slides
SORTING, SEARCHING AND HASHING TECHNIQUES
COMPUTER 2430 Object Oriented Programming and Data Structures I
Data Structures Using C++ 2E
Applications of Arrays
Presentation transcript:

Cs212: DataStructures Lecture 3: Searching

Lecture Contents searching Sequential search algorithm. Binary search algorithm.

Search Algorithms Search Algorithms Sequential Search Binary Search 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

Search Algorithms Sequential search. Binary search. It’s not requires an ordered list. Binary search. It requires an ordered list.

1/ Sequential (Linear) Search 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.

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

Sequential Search Algorithm algorithm SeqSearch (val list <array>, val last <index>, val target <keyType>) 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<integer>

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

Recursive sequential search (1) Algorithm sequentialSearch (item<integer>,list<array>, listSize<integer>) 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.

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

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

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

Recursive Binary search algorithm algorithm RecBinarySearch (val First<index>, val last <index>,val target <keyType>) 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<integer>

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

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

Binary search algorithm (iterative) Algorithm BinarySearch (list<array>, key <integer>, listSize<integer> ) 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<integer>

Binary search in iterative algorithm 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

End Of Chapter References: Text book, chapter2: Searching