Searching Algorithms. Lecture Objectives Learn how to implement the sequential search algorithm Learn how to implement the binary search algorithm To.

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 Algorithms: Merge and Quick. Lecture Objectives Learn how to implement the simple advanced sorting algorithms (merge and quick) Learn how to implement.
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.
ICS201 Lecture 20 : Searching King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
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.
Elementary Data Structures and Algorithms
Searching Chapter Chapter Contents The Problem Searching an Unsorted Array Iterative Sequential Search Recursive Sequential Search Efficiency of.
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.
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--WuChapter Chapter 10 Sorting and Searching.
CPT: Search/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss searching: its implementation,
Data Structures Using C++1 Chapter 9 Search Algorithms.
Copyright © 2013 by John Wiley & Sons. All rights reserved. SORTING AND SEARCHING CHAPTER Slides by Rick Giles 14.
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)
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 Searching and Sorting Linear Search Binary Search.
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.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArraySet and Binary Search.
CSE 143 Lecture 15 Binary Search; Comparable reading: 13.2; 10.2 slides created by Marty Stepp
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.
SortingBigOh ASFA AP Computer Science A. Big-O refers to the order of an algorithm runtime growth in relation to the number of items I. O(l) - constant.
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.
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.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Selection Sort Sorts an array by repeatedly finding the smallest.
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,
Data Structures and Algorithms Searching Algorithms M. B. Fayek CUFE 2006.
Binary search and complexity reading:
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.
Chapter 3: Sorting and Searching Algorithms 3.1 Searching Algorithms.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
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
Sorting and "Big Oh" ASFA AP Computer Science A SortingBigOh.
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
Recitation 13 Searching and Sorting.
Chapter 7 Single-Dimensional Arrays
Search Algorithms Sequential Search (Linear Search) Binary Search
Searching CSCE 121 J. Michael Moore.
Lecture 14: binary search and complexity reading:
Lecture 15: binary search reading:
Data Structures Using C++ 2E
Presentation transcript:

Searching Algorithms

Lecture Objectives Learn how to implement the sequential search algorithm Learn how to implement the binary search algorithm To learn how to estimate and compare the performance of algorithms To learn how to measure the running time of a program

Searching Algorithms Necessary components to search a list of fdata  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’d) Figure 1: Array list with seven (07) elements

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’d) 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; } The previous could be further reduced to:

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’d) 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; }

Assume that the array contains N elements. 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 …………………. 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 ……………….. to find the key. Finally if the key is NOT in the array list, then we would have performed ………………. and the key is NOT found and we would return -1. Performance of the Linear Search N comparisons two comparisons N comparisons

Therefore, the best case is: 1 And, the worst case is: N The average case is computed from adding the cost of all “different” cases divided by the number of the different cases Performance of the Linear Search (Cont’d) Average Number of Comparisons …..+ N + N N+1 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’d) Figure 2: Array list with twelve (12) elements Figure 3: Search list, list[0] … list[11]

Binary Search Algorithm (Cont’d) Figure 4: Search list, list[6] … list[11]

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’d) Figure 5: Sorted list for binary search key = 89key = 34

Binary Search Algorithm (Cont’d) key = 22 Figure 6: Sorted list for binary search

Performance of Binary Search Algorithm Figure 7: A Sorted list for binary search key ≠ List[499]key < List[499] Figure 8: Search list after first iteration

Performance of Binary Search Algorithm (Cont’d) Figure 9: Search list after second iteration 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’d)

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’d)

Searching a Sorted Array in a Program The Arrays class contains a static binarySearch() method The method returns either:  The index of the element, if element is found  Or -k - 1 where k is the position before which the element should be inserted int[] a = {1, 4, 9}; int v = 7; int pos = Arrays.binarySearch(a, v); // Returns -3; v should be inserted before position 2

Searching Real Data Arrays.binarySearch() sorts objects of classes that implement Comparable interface: The call a.compareTo(b) returns  A negative number if a should come before b  0 if a and b are the same  A positive number otherwise public interface Comparable { int compareTo(Object otherObject); }

Several classes in Java (e.g. String and Date ) implement Comparable You can implement Comparable interface for your own classes public class Coin implements Comparable {... public int compareTo(Object otherObject) { Coin other = (Coin) otherObject; if (value < other.value) return -1; if (value == other.value) return 0; return 1; }... } Searching Real Data (Cont’d)

The CompareTo() Method The implementation must define a total ordering relationship  Antisymmetric: If a.compareTo(b) = 0, then b.compareTo(a) = 0  Reflexive: a.compareTo(a) = 0

The implementation must define a total ordering relationship  Transitive: If a.compareTo(b) = 0 and b.compareTo(c) = 0, then a.compareTo(c) = 0 The compareTo() Method (Cont’d)

Once your class implements Comparable, simply use the Arrays.binarySearch() method: Coin[] coins = new Coin[n]; Coin aCoin = new Coin(…); // Add coins... Arrays.binarySearch(coins, aCoin); The compareTo() Method (Cont’d)