www hndit com Searching Algorithms IT 12112 Lecture

  • Slides: 17
Download presentation
www. hndit. com Searching Algorithms IT 12112 Lecture 08

www. hndit. com Searching Algorithms IT 12112 Lecture 08

Sequential Search www. hndit. com n Examines each element of a list in sequence

Sequential Search www. hndit. com n Examines each element of a list in sequence until it finds the target value or reaches the end of the list. Dan 0 Fay 1 Cal 2 Ben 3 Guy 4 Amy 5 Amy? Amy? Amy! Eve 6

www. hndit. com Sequential Search (cont’d) int sequential. Search(int arr [ ], int value)

www. hndit. com Sequential Search (cont’d) int sequential. Search(int arr [ ], int value) { for (int i = 0; i < length ; i++) { if (value == arr [ i ])) return i; } return -1; }

Sequential Search (cont’d) www. hndit. com n The average number of comparisons (assuming the

Sequential Search (cont’d) www. hndit. com n The average number of comparisons (assuming the target value is equal to one of the elements of the array, randomly chosen) is about n / 2 (where n = length of the aray). n Worst case: n comparisons. n Also n comparisons are needed to establish that the target value is not in the array. n We say that this is an O(n) (order of n) algorithm.

www. hndit. com Efficiency of a Sequential Search n Best case O(1) – Locate

www. hndit. com Efficiency of a Sequential Search n Best case O(1) – Locate desired item first n Worst case O(n) – Must look at all the items n Average case O(n) – Must look at half the items – O(n/2) is still O(n) 5

www. hndit. com Searching a Sorted Array n. A sequential search can be more

www. hndit. com Searching a Sorted Array n. A sequential search can be more efficient if the data is sorted 6

Binary search algorithm www. hndit. com n An algorithm that searches for a value

Binary search algorithm www. hndit. com n An algorithm that searches for a value in a sorted list by repeatedly eliminating half the list from consideration. – Can be written iteratively or recursively n The elements of the list must be arranged in ascending (or descending) order. n The target value is always compared with the middle element of the remaining search range. 7

Binary search pseudocode www. hndit. com binary search array a for value i: if

Binary search pseudocode www. hndit. com binary search array a for value i: if all elements have been searched, result is -1. examine middle element a[mid]. if a[mid] equals i, result is mid. if a[mid] is greater than i, binary search lower half of a for i. if a[mid] is less than i, binary search upper half of a for i. 8

www. hndit. com Binary Search of Sorted Array n Algorithm for a binary search

www. hndit. com Binary Search of Sorted Array n Algorithm for a binary search Algorithm binary. Search(a, first, last, desired. Item) mid = (first + last)/2 // approximate midpoint if (first > last) return false else if (desired. Item equals a[mid]) return true else if (desired. Item < a[mid]) return binary. Search(a, first, mid-1, desired. Item) else // desired. Item > a[mid] return binary. Search(a, mid+1, last, desired. Item) 9

www. hndit. com Binary search example searching for value 16 10 0 4 1

www. hndit. com Binary search example searching for value 16 10 0 4 1 7 2 16 3 20 4 37 5 38 6 43 min mid (too big!) max

www. hndit. com Binary search example searching for value 16 11 0 4 min

www. hndit. com Binary search example searching for value 16 11 0 4 min 1 7 mid (too small!) 2 16 3 20 4 37 5 38 6 43 max

www. hndit. com Binary search example searching for value 16 12 0 4 1

www. hndit. com Binary search example searching for value 16 12 0 4 1 7 2 16 3 20 4 37 5 38 6 43 min, mid, max (found it!)

Binary Search (cont’d) n Recursive www. hndit. com implementation: public int binary. Search (int

Binary Search (cont’d) n Recursive www. hndit. com implementation: public int binary. Search (int [ ] arr, int value, int left, int right) { if (right < left) return -1; // Not found int middle = (left + right) / 2; if (value == arr [middle] ) return middle; else if (value < arr[middle]) return binary. Search (arr, value, left, middle - 1); else // if ( value > arr[middle]) return binary. Search (arr, value, middle + 1, right); }

Binary Search (cont’d) n Iterative www. hndit. com implementation: public int binary. Search (int

Binary Search (cont’d) n Iterative www. hndit. com implementation: public int binary. Search (int arr [ ] , int value, int left, int right) { while (left <= right) { int middle = (left + right) / 2; if ( value == arr [middle] ) return middle; else if ( value < arr[middle] ) right = middle - 1; else // if ( value > arr[middle] ) left = middle + 1; } return -1; // Not found }

Binary Search (cont’d) www. hndit. com A “divide and conquer” algorithm. n Works very

Binary Search (cont’d) www. hndit. com A “divide and conquer” algorithm. n Works very fast: only 20 comparisons are needed for an array of 1, 000 elements; (30 comparisons can handle 1, 000, 000 elements; etc. ). n We say that this is an O(log n) algorithm. n Efficiency of a Binary Search • Best case Ø Locate desired item first • Worst case Ø O(1) O(log n) Must look at all the items • Average case O(log n)

Choosing a Search Method www. hndit. com Best Case Average Case Worst Case Sequential

Choosing a Search Method www. hndit. com Best Case Average Case Worst Case Sequential search (unsorted data) O(1) O(n) Sequential search (sorted data) O(1) O(n) Binary Search (sorted array) O(1) O(log n) n Iterative search saves time, memory over recursive search 16

Exercises on Searching www. hndit. com 1. What is a sequential search? 2. What

Exercises on Searching www. hndit. com 1. What is a sequential search? 2. What is a binary search? 3. Write a recursive binary. Search method on a double array. 4. Mention the advantages and disadvantages, if any, of binary search over sequential search. 5. Consider the following array of sorted integers: 10, 15, 25, 30, 33, 34, 46, 55, 78, 84, 96, 99 Using binary search algorithm, search for 23. Show the sequence of array elements that are compared, and for each comparison, indicate the values 17 of low and high.