Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.

Binary search

Binary searh algorithm

  • Als Erste(r) kommentieren

Binary search

  1. 1. Discrete presentation
  2. 2. introduction  binary search, also known as half-interval search, logarithmic search, or binary chop.  It is a search algorithm that finds the position of a target value within a sorted array. Binary search compares the target value to the middle element of the array; if they are unequal, the half in which the target cannot lie is eliminated and the search continues on the remaining half until it is successful. If the search ends with the remaining half being empty, the target is not in the array.
  3. 3.  Binary search runs in at worst logarithmic time making O(log n) comparisons, where n is the number of elements in the array, the O is Big O notation, and log is the logarithm.  In the best case O(1) it runs it runs.  Binary search runs in at average logarithmic time making O(log n) comparisons, where n is the number of elements in the array, the O is Big O notation, and log is the logarithm.
  4. 4. Real life applications of binary search  In library books are categorized in sorted sequence. when it comes to this algorithm is searching for a book in the library. Here our sorted list is the well-arranged books in an alphabetical order. Our target element is the book we prefer to read.  In telephone directory where numbers are arranged in a sequence.
  5. 5. Binary Search  Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists.  Invariant. Algorithm maintains a[lo]  value  a[hi].  Ex. Binary search for 33. 821 3 4 65 7 109 11 12 14130 641413 25 33 5143 53 8472 93 95 97966 lo hi
  6. 6. Binary Search  Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists.  Invariant. Algorithm maintains a[lo]  value  a[hi].  Ex. Binary search for 33. 821 3 4 65 7 109 11 12 14130 641413 25 33 5143 53 8472 93 95 97966 lo himid 53
  7. 7. Binary Search  Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists.  Invariant. Algorithm maintains a[lo]  value  a[hi].  Ex. Binary search for 33.  21 3 4 65 7 1413 25 33 51436 0 lo hi 8 109 11 12 1413 64 8472 93 95 979653
  8. 8. Binary Search  Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists.  Invariant. Algorithm maintains a[lo]  value  a[hi].  Ex. Binary search for 33. 821 3 4 65 7 109 11 12 14130 641413 25 33 5143 53 8472 93 95 97966 lo mid hi
  9. 9. Binary Search  Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists.  Invariant. Algorithm maintains a[lo]  value  a[hi].  Ex. Binary search for 33. 821 3 4 65 7 109 11 12 14130 641413 25 33 5143 53 8472 93 95 97966 lo hi
  10. 10. Binary Search  Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists.  Invariant. Algorithm maintains a[lo]  value  a[hi].  Ex. Binary search for 33. 821 3 4 65 7 109 11 12 14130 641413 25 33 5143 53 8472 93 95 97966 lo himid
  11. 11. Binary Search  Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists.  Invariant. Algorithm maintains a[lo]  value  a[hi].  Ex. Binary search for 33. 821 3 4 65 7 109 11 12 14130 641413 25 33 5143 53 8472 93 95 97966 lo hi
  12. 12. Binary Search  Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists.  Invariant. Algorithm maintains a[lo]  value  a[hi].  Ex. Binary search for 33. 821 3 4 65 7 109 11 12 14130 641413 25 33 5143 53 8472 93 95 97966 lo hi mid
  13. 13. Binary Search  Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists.  Invariant. Algorithm maintains a[lo]  value  a[hi].  Ex. Binary search for 33. 821 3 4 65 7 109 11 12 14130 641413 25 33 5143 53 8472 93 95 97966 lo hi mid
  14. 14. Algorithm of binary search Binary search(A,key) While(low<=high) do mid=(low+high)/2 If key=A[mid] Return mid If key>A[mid] Low=mid+1 else high=mid-1 Return NULL
  15. 15. Advantages  Compared to linear search (checking each element in the array starting from the first), binary search is much faster. Linear search takes, on average N/2 comparisons (where N is the number of elements in the array), and worst case N comparisons.  It’s well known and often implemented for you as a library routine.
  16. 16. Disadvantages  It’s more complicated than linear search, and is overkill for very small numbers of elements.  It works only on lists that are sorted and kept sorted. That is not always feasible, especially if elements are constantly being added to the list.  It works only on element types for which there exists a less-than relationship. Some types simply cannot be sorted (though this is rare).  There is a great lost of efficiency if the list does not support random-access. If your list is a plain array, that’s great. If it’s a linked-list, not so much.  There are even faster search methods available, such as hash lookups. However a hash lookup requires the elements to be organized in a much more complicated data structure (a hash table, not a list).

×