Presentation is loading. Please wait.

Presentation is loading. Please wait.

Zabin Visram Room CS115 CS126 Searching

Similar presentations

Presentation on theme: "Zabin Visram Room CS115 CS126 Searching"— Presentation transcript:

1 Zabin Visram Room CS115 CS126 Searching
Binary Search

2 Binary Search Sequential search is not efficient for large lists as it searches half the list, on average Another search algorithm – Binary search Very fast But can only be performed on ordered lists

3 Example If you are looking for you friends number in the phone book, you may decide to look from half way , you know the book is ordered alphabetically therefore if you decide the name is in the right half – you can disregard the left half – throw it away – just concentrate on the right half – this way your search is dramatically reduced – only have ½ book o search – do the same process again – until eventually u either find the name or decide its not there – binary search is the same

4 Divide & Conquer technique
When a list is sorted and we have random access to the list as in an array we can take advantage of this additional structure in our search methods. binary search algorithm uses the “Divide & Conquer” method to search the list

5 Divide & Conquer technique
First the search item is compared with the middle element of the list. If the search item is less than the middle element of the list, we restrict the search to the first half of the list; otherwise we search the second half of the list

6 Binary Search Consider a sorted list of length 12 4 8 19 25 34 39 45
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] 4 8 19 25 34 39 45 48 66 75 89 95 list

7 Binary Search Suppose we want to find 75
Entire list is searched – compare 75 with middle element in list, list[5] (which is 39) Because 75 Is in list [6]..list[11] we restrict search there Search list list [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] 4 8 19 25 34 39 45 48 66 75 89 95 MID

8 Binary Search The process is now repeated on the list [6]..list[11] which is a list of 6 Search list list [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] 4 8 19 25 34 39 45 48 66 75 89 95

9 Task - individually During a binary search , which elements in the array Are compared to the target when the target is a. 2 b.8 c.15

10 Answer a and 4 B. 12, 4 and 8 C. 12, 20 and 14

11 most of the array is not searched at all , saving much time - thus BS algorithm is very fast.
But how fast ? Counting the comparisons I.e each time algorithm divides the array in half - can provide us a measure of the algorithms efficiency

12 Binary search Analysis
Suppose we are working with a sorted array of n integers Initially first = 0 and (because an array index in Java starts at 0 and n denotes the number of elements in the list(length) then last = n-1 The element midway in an array indexed from 0 to n-1 is mid = (n – 1) / 2. If the target is less than the value at mid, then since the array is sorted we can be certain that the target element is not in the array from positions mid to n-1. In pseudo code the idea is:

13 mid = [(mid+1) + (n-1)] / 2 low = 0; high = n -1;
mid = (low + high) / 2; if target = array[mid] then return mid else if target < array[mid] then target is not in range mid .. n-1 discard upper half by setting high = mid –1; target is not in the 0 ..mid range; discard lower half by setting low = mid + 1; repeat with new range until possible array is of length 1.   If the lower half of the list 0..mid was discarded by the above algorithm then the next probe would be at mid = [(mid+1) + (n-1)] / 2

14 Binary search Analysis
which is the midpoint of the upper half. Continuing in this way, unless we find the target, we halve the list each time until it becomes of size one and we either have found the target or it was not present originally. The complexity of such an algorithm – the work required to complete it given a list of size n initially – is the number of probes that are required. That is, how many times can a list of size n be halved before it becomes of size one?


16 A standard binary search method can then be written
  public int binarySearch(int target) { int low = 0; int high = a.length – 1; int mid; while (low <= high) { mid = (low + high) / 2;   if (target == a[mid]) //match   return mid;   else   if (target < a[mid]) //search low end of array   high = mid – 1; else //search high end of array   low = mid + 1; } return –1;  }


18 Binary Search Vs Linear Search
A sequential search of an array looks at the first item, the second item, and so on until it either finds a particular item or determines that the item does not occur in the group A binary search of an array requires that the array be sorted . It looks first at the middle of the array to determine in which half the desired item can occur. The search repeats this strategy on only this half of the array

19 Binary Search Vs Linear Search
The benefit of binary search over linear search becomes significant for lists over about 100 elements. For smaller lists linear search may be faster because of the speed of the simple increment compared with the divisions needed in binary search. The general moral is that for large lists binary search is very much faster than linear search, but is not worth while for small lists.

20 Demonstration  in comparison – linear search

Download ppt "Zabin Visram Room CS115 CS126 Searching"

Similar presentations

Ads by Google