Lecture 12. Searching Algorithms and its analysis 1.

Slides:



Advertisements
Similar presentations
Zabin Visram Room CS115 CS126 Searching
Advertisements

Chapter 10: Applications of Arrays and Strings J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second.
CHAPTER 10 ARRAYS II Applications and Extensions.
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 10 Applications of Arrays.
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.
1 Lecture 25:Pointers Introduction to Computer Science Spring 2006.
Data Structures Using Java1 Chapter 8 Search Algorithms.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
Searching Arrays Linear search Binary search small arrays
Elementary Data Structures and Algorithms
1 Lecture 22:Applications of Arrays Introduction to Computer Science Spring 2006.
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Linear Search Binary Search -Reading p
1 Search Algorithms Sequential Search (Linear Search) Binary Search Rizwan Rehman Centre for Computer Studies Dibrugarh University.
Data Structures Using Java1 Chapter 8 Search Algorithms.
Chapter 16: Searching, Sorting, and the vector Type.
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.
Data Structures Using C++1 Chapter 9 Search Algorithms.
Data Structures Using C++1 Search Algorithms Sequential Search (Linear Search) Binary Search.
Chapter 10 Applications of Arrays and Strings. Chapter Objectives Learn how to implement the sequential search algorithm Explore how to sort an array.
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)
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.
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.
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.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
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. Linear (Sequential) Search Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and.
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.
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
Chapter 14: Searching and Sorting
Searching CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
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 & 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.
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.
Data Structures Using C++
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Searching When we maintain a collection of data,
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.
Hossain Shahriar Announcement and reminder! Tentative date for final exam shown below, please choose a time slot! December 19.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
Sorting and Searching Bubble Sort Linear Search Binary Search.
BINARY SEARCH CS16: Introduction to Data Structures & Algorithms Thursday February 12,
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 Searching algorithms with simple arrays
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
Searching Given a collection and an element (key) to find… Output
Recitation 13 Searching and Sorting.
Chapter 7 Single-Dimensional Arrays
Search Algorithms Sequential Search (Linear Search) Binary Search
Searching CSCE 121 J. Michael Moore.
Linear and Binary Search
UNIT – V PART - I Searching By B VENKATESWARLU, CSE Dept.
Data Structures: Searching
Data Structures Using C++ 2E
Presentation transcript:

Lecture 12. Searching Algorithms and its analysis 1

Recap 2 Let T(n) be a monotonically increasing function that satisfies T(n) = a T(n/b) + f(n) T(1) = c where a  1, b  2, c>0. If f(n) is  (n d ) where d  0 then if a < b d T(n) =If a = b d if a > b d

Searching Algorithms Necessary components to search a list of data – Array containing the list – Length of the list – Item for which you are searching After search completed – If item found, report “success,” return location in array – If item not found, report “not found” or “failure”

Suppose that you want to determine whether 27 is in the list First compare 27 with list[0]; that is, compare 27 with 35 Because list[0] ≠ 27, you then compare 27 with list[1] Because list[1] ≠ 27, you compare 27 with the next element in the list Because list[2] = 27, the search stops This search is successful! Searching Algorithms (Cont !!!)

Let’s now search for 10 The search starts at the first element in the list; that is, at list[0] Proceeding as before, we see that this time the search item, which is 10, is compared with every item in the list Eventually, no more data is left in the list to compare with the search item; this is an unsuccessful search Searching Algorithms (Cont’d)

Linear Search Algorithm public static int linSearch(int[] list, int listLength, int key) { int loc; boolean found = false; for(int loc = 0; loc < listLength; loc++) { if(list[loc] == key) { found = true; break; } if(found) return loc; else return -1; }

Linear Search Algorithm (Cont !!!) public static int linSearch(int[] list, int listLength, int key) { int loc; for(int loc = 0; loc < listLength; loc++) { if(list[loc] == key) return loc; } return -1; }

Using a while (or a for) loop, the definition of the method seqSearch can also be written without the break statement as: Linear Search Algorithm (Cont !!!) public static int linSearch(int[] list, int listLength, int key) { int loc = 0; boolean found = false; while(loc < listLength && !found) { if(list[loc] == key) found = true; else loc++ } if(found) return loc; else return -1; }

Suppose that the first element in the array list contains the variable key, then we have performed one comparison to find the key. Suppose that the second element in the array list contains the variable key, then we have performed two comparisons to find the key. Carry on the same analysis till the key is contained in the last element of the array list. In this case, we have performed N comparisons (N is the size of the array list) to find the key. Finally if the key is NOT in the array list, then we would have performed N comparisons and the key is NOT found and we would return -1. Performance of the Linear Search

Therefore, the best case is: 1 And, the worst case is: N The average case is: Performance of the Linear Search (Cont’d) …..+ N + N N+1 Average Number of Comparisons Best case Worst case and key found at the end of the array list! Worst case and key is NOT found! = Number of possible cases

Can only be performed on a sorted list !!! Uses divide and conquer technique to search list Binary Search Algorithm

Search item is compared with middle element of list If search item < middle element of list, search is restricted to first half of the list If search item > middle element of list, search second half of the list If search item = middle element, search is complete Binary Search Algorithm (Cont’d)

Determine whether 75 is in the list Binary Search Algorithm (Cont !!!)

Binary Search Algorithm (Cont’d) public static int binarySearch(int[] list, int listLength, int key) { int first = 0, last = listLength - 1; int mid; boolean found = false; while (first <= last && !found) { mid = (first + last) / 2; if (list[mid] == key) found = true; else if(list[mid] > key) last = mid - 1; else first = mid + 1; } if (found) return mid; else return –1; } //end binarySearch

Binary Search Algorithm (Cont !!!) key = 89key = 34

Binary Search Algorithm (Cont’d) key = 22

Performance of Binary Search Algorithm key ≠ List[499]key < List[499]

Performance of Binary Search Algorithm (Cont !!!) key > List[249]

Suppose that L is a list of size Since  = 220, it follows that the while loop in binary search will have at most 21 iterations to determine whether an element is in L Every iteration of the while loop makes two key (that is, item) comparisons Performance of Binary Search Algorithm (Cont !!!)

To determine whether an element is in L, binary search makes at most 42 item comparisons – On the other hand, on average, a linear search will make 500,000 key (item) comparisons to determine whether an element is in L In general, if L is a sorted list of size N, to determine whether an element is in L, the binary search makes at most 2log2N + 2 key (item) comparisons Performance of Binary Search Algorithm (Cont !!!)

In Linear searching, you have to scan an array from first location to Nth location till your required element found. You have three cases – Required element is at first location (Best Case) – Required element is at Last location (Worst Case) – Required element is at other than first and last location (Best Case) For binary search you will have sorted array and apply divide and conquer approach to find the location of required element For sorting you can use any one of following teachnique – Bubble – Selection – insertion Summary

In Next Lecturer In next lecture, we will discuss about sorting techniques. 23