Presentation is loading. Please wait.

Presentation is loading. Please wait.

Searching.

Similar presentations


Presentation on theme: "Searching."— Presentation transcript:

1 Searching

2 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

3 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 linearSearch(int target, int[] a) { for (int i = 0; i < a.length; i++) { if (target == a[i]) return i; } return -1; // not a legal index }

4 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 k1 and k2, the time required to search the array is T = k1 * n + k2 for an array of size n This is a linear equation

5 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

6 Example of binary search
5 7 10 13 15 19 23 28 32 37 41 Search the following array a for 36: a 46 4. (12+12)/2=13; a[13]=32; too small; search 3. (12+15)/2=13; a[13]=37; too large; search 2. (8+15)/2=11; a[11]=32; too small; search 1. (0+15)/2=7; a[7]=19; too small; search 8..15 ...but 13>12, so quit: 36 not found

7 Binary search in Java static int binarySearch(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"

8 Recursive binary search in Java
static int binarySearch(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 binarySearch(target, a, left, middle - 1); else return binarySearch(target, a, middle + 1, right); }

9 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 = binarySearch(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 binarySearch(int target, int[] a) { return binarySearch(target, a, 0, a.length); }

10 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 log2n 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)

11 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 times faster for an array of size This is the kind of speedup that we care about when we analyze algorithms

12 The End


Download ppt "Searching."

Similar presentations


Ads by Google