Slide 1

Slide 1 text

CSE110 Principles of Programming with Java Lecture 27: Searching Algorithms Javier Gonzalez-Sanchez [email protected] javiergs.engineering.asu.edu | javiergs.com Office Hours: By appointment

Slide 2

Slide 2 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 2 Search Algorithms • Search: locate an item in an array of information (primitive type or objects) • Common searching algorithms includes: • Linear search • Binary search

Slide 3

Slide 3 text

Linear Search

Slide 4

Slide 4 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 4 Linear Search • Looks sequentially at each element of a given collection of elements until either the target is found or we come to the end of the list (not found)

Slide 5

Slide 5 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 5 Linear Search // Searches and returns the index of where the // searched element is located. // Returns -1 if the target is not found. public static int linearSearch (int[] numbers, int target) { int position = -1; //it is not found yet for (int index=0; index < numbers.length; index++) { if (numbers[index] == target) { position = index; //save the index of search element break; // OR // return index; } } return position; }

Slide 6

Slide 6 text

Binary Search

Slide 7

Slide 7 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 7 Binary Search • It uses Divide and Conquer approach. • Pre-Condition: The search pool is sorted. • Instead of starting the search at one end, we begin in the middle • Used to look up a word in a dictionary or a name in a phone book.

Slide 8

Slide 8 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 8 Binary Search 1. For example, find the number 29 in the following sorted list of numbers: 8 15 22 29 36 54 55 61 70 73 88 0 1 2 3 4 5 6 7 8 9 10

Slide 9

Slide 9 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 9 Binary Search 2. Compare the target to the middle value 54 8 15 22 29 36 54 55 61 70 73 88 0 1 2 3 4 5 6 7 8 9 10

Slide 10

Slide 10 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 10 Binary Search 3. if 29 is in the list, it is in the front half of the list since it is smaller than 54. 8 15 22 29 36 54 55 61 70 73 88 0 1 2 3 4 5 6 7 8 9 10

Slide 11

Slide 11 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 11 Binary Search 3. if 29 is in the list, it is in the front half of the list since it is smaller than 54. 4. With one comparison, we’ve eliminated half of the data 8 15 22 29 36 54 55 61 70 73 88 0 1 2 3 4 5 6 7 8 9 10

Slide 12

Slide 12 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 12 Binary Search 5. Then compare to 22, eliminating another quarter of the data 6. etc. 8 15 22 29 36 54 55 61 70 73 88 0 1 2 3 4 5 6 7 8 9 10 8 15 22 29 36 54 55 61 70 73 88 0 1 2 3 4 5 6 7 8 9 10

Slide 13

Slide 13 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 13 Binary Search 8 15 22 29 36 54 55 61 70 73 88 0 1 2 3 4 5 6 7 8 9 10 start, end, middle 0, 10, (10 + 0) / 2 = 5 0, 4, (4 + 0 ) / 2 = 2 3, 4, (4 + 3 ) / 2 = 3 3

Slide 14

Slide 14 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 14 Binary Search // Searches using a binary search // returns the index of where the searched element is located. // returns -1 if the target is not found. public static int binarySearch (int[] data, int target) { int start = 0, end = data.length-1, middle; int position = -1; //return -1 if not found boolean found = false; while (found == false && start <= end) { middle = (start + end)/2; if (data[middle] == target){ found = true; position = middle; } else if (data[middle] > target) end = middle - 1; else start = middle + 1; } return position; //return -1 if not found }

Slide 15

Slide 15 text

Binary Search Using Recursion

Slide 16

Slide 16 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 16 Recursive Binary Search Method • Assume an array a sorted in ascending order, and an item X • We want to write a method that searches for X within the array a, returning the index of X if it is found, and returning -1 if X is not in the array • A recursive strategy for searching a portion of the array from lo to hi is to set m to position of middle of portion of array:

Slide 17

Slide 17 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 17 Recursive Binary Search Method • If (a[m]== X), we found X, so return m • if (a[m] > X), recursively search a[lo … m-1] • if (a[m] < X), recursively search a[m+1 … hi]

Slide 18

Slide 18 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 18 Recursive Binary Search Method int bSearch(int[] a, int x, int lo, int hi) { int m = (lo + hi) /2; if(lo > hi) return -1; // base if(a[m] == x) return m; // base if(a[m] > x) return bSearch(a, x, lo, m-1); else return bSearch(a, x, m+1, hi); }

Slide 19

Slide 19 text

CSE110 - Principles of Programming Javier Gonzalez-Sanchez [email protected] Summer 2020 Disclaimer. These slides can only be used as study material for the class CSE110 at ASU. They cannot be distributed or used for another purpose.