Searching About sorted arrays An array is sorted
Searching
About sorted arrays • An array is sorted in ascending order if each element is no smaller than the preceding element • An array is sorted in descending order if each element is no larger than the preceding element • When we just say an array is “sorted, ” we usually mean that it is sorted in ascending order
Searching an array of integers • If an array is not sorted, there is no better algorithm than linear search for finding an element in it static int linear. Search(int target, int[] a) { for (int i = 0; i < a. length; i++) { if (target == a[i]) return i; } return -1; // not a legal index }
Linear search takes linear time • Linear search has linear time complexity: – If the target (the thing we are looking for) is not in the array, we have to look at all n elements – If the target is in the array, we have to look at n/2 elements on average – If we search an array that is twice as large, we have to look at twice as many elements – For some constants k 1 and k 2, the time required to search the array is T = k 1 * n + k 2 for an array of size n – This is a linear equation
Binary search • How do we look up a name in a phone book, or a word in a dictionary? – Look somewhere in the middle – Compare what’s there with the thing you’re looking for – Decide which half of the remaining entries to look at next – Repeat until you find the correct place – This is the binary search algorithm
Example of binary search Search the following array a for 36: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 a 5 7 10 13 13 15 19 19 23 28 28 32 32 37 41 46 1. (0+15)/2=7; a[7]=19; too small; search 8. . 15 2. (8+15)/2=11; a[11]=32; too small; search 12. . 15 3. (12+15)/2=13; a[13]=37; too large; search 12. . 12 4. (12+12)/2=13; a[13]=32; too small; search 13. . 12. . . but 13>12, so quit: 36 not found
Binary search in Java static int binary. Search(int target, int[] a, int left, int right) { while (left <= right) { int middle = (left + right) / 2; if (a[middle] == target) return middle ; // found else if (a[middle] > target) right = middle – 1; else left = middle + 1; } return -1; // -1 means "not found" }
Recursive binary search in Java static int binary. Search(int target, int[] a, int left, int right) { if (left > right) return -1; // not found int middle = (left + right) / 2; if (a[middle] == target) return middle ; // found else if (a[middle] > target) return binary. Search(target, a, left, middle - 1); else return binary. Search(target, a, middle + 1, right); }
An aside: Style • To do a binary search for n in array a, anyone who wants to use our binary search routines would have to write result = binary. Search(n, a, 0, a. length); • The last two parameters are not something the user cares about, so it is poor style to require them – These parameters can easily be removed from the iterative version of binary search – They cannot be removed from the recursive version • Solution: Write an adapter method: static int binary. Search(int target, int[] a) { return binary. Search(target, a, 0, a. length); }
Binary search takes log n time • In binary search, we choose an index that cuts the remaining portion of the array in half • We repeat this until we either find the value we are looking for, or we reach a subarray of size 1 • If we start with an array of size n, we can cut it in half log 2 n times • Hence, binary search has logarithmic (log n) time complexity • For an array of size 1000, this is 100 times faster than linear search (210 ~= 1000)
Conclusion • Linear search has linear time complexity • Binary search has logarithmic time complexity • For large arrays, binary search is far more efficient than linear search – However, binary search requires that the array be sorted – If the array is sorted, binary search is • 100 times faster for an array of size 1000 • 50 000 times faster for an array of size 1 000 • This is the kind of speedup that we care about when we analyze algorithms
The End
- Slides: 12