Se ha denunciado esta presentación.
Utilizamos tu perfil de LinkedIn y tus datos de actividad para personalizar los anuncios y mostrarte publicidad más relevante. Puedes cambiar tus preferencias de publicidad en cualquier momento.
BINARY
SEARCH
Prepared by :
Dimpy (1833)
Drishti (1838)
INTODUCTION
A Binary search algorithm finds the position of a specified input value
(the search "key") within a sorted arr...
Example :
Step 1 :
Step 2:
Step 3:
ALGORITHM (Recursive)
recursivebinarysearch(int A[], int first, int last, int key)
if last<first
index=-1
else
int mid = (...
BINARY SEARCH ALGORITHM
Algorithm is quite simple. It can be done either recursively or
iteratively:
 Get the middle elem...
ANALYSIS OF BINARY SEARCH
To analyze the binary search algorithm, we need to recall that each
comparison eliminates about ...
CORRECTNESS OF BINARY SEARCH
 Lemma: For all n>=0, if BinarySearch(E, first , last, k) is called, and
the size is (last-f...
 For n>0, the algorithm first calculates mid = (first +
last) /2 thus line 1 is false, as mid is within the
search range ...
THANK YOU !
Próxima SlideShare
Cargando en…5
×
Próximo SlideShare
Binary search
Siguiente
Descargar para leer sin conexión y ver en pantalla completa.

Compartir

Binary Search - Design & Analysis of Algorithms

Descargar para leer sin conexión

Binary Search - Intro , Algorithm and Analysis

Binary Search - Design & Analysis of Algorithms

  1. 1. BINARY SEARCH Prepared by : Dimpy (1833) Drishti (1838)
  2. 2. INTODUCTION A Binary search algorithm finds the position of a specified input value (the search "key") within a sorted array . For binary search, the array should be arranged in ascending or descending order. Why Binary Search Is Better Than Linear Search ? A linear search works by looking at each element in a list of data until it either finds the target or reaches the end. This results in O(n) performance on a given list. A binary search comes with the prerequisite that the data must be sorted. We can use this information to decrease the number of items we need to look at to find our target. That is , Binary Search is fast as compared to linear search because of less number of computational operations .
  3. 3. Example :
  4. 4. Step 1 :
  5. 5. Step 2:
  6. 6. Step 3:
  7. 7. ALGORITHM (Recursive) recursivebinarysearch(int A[], int first, int last, int key) if last<first index=-1 else int mid = (first + last) / 2 if key=A[mid] index=mid else if key<A[mid] index= recursivebinarysearch(A, first, mid – 1, key) else index= recursivebinarysearch(A, mid + 1, last, key) return index
  8. 8. BINARY SEARCH ALGORITHM Algorithm is quite simple. It can be done either recursively or iteratively:  Get the middle element;  If the middle element equals to the searched value, the algorithm stops;  Otherwise, two cases are possible:  Searched value is less, than the middle element. In this case, go to the step 1 for the part of the array, before middle element.  Searched value is greater, than the middle element. In this case, go to the step 1 for the part of the array, after middle element.  Now, The iterations should stop when searched element is found & when sub array has no elements. In this case, we can conclude, that searched value is not present in the array.
  9. 9. ANALYSIS OF BINARY SEARCH To analyze the binary search algorithm, we need to recall that each comparison eliminates about half of the remaining items from consideration. If we start with n items, about n/2 items will be left after the first comparison. After the second comparison, there will be about n/4. Then n/8, n/16, and so on. When we split the list enough times, we end up with a list that has just one item. Either that is the item we are looking for or it is not. Either way, we are done. The number of comparisons necessary to get to this point is i where n/2^i=1. Solving for i gives us i=(log n). The maximum number of comparisons is log n. Therefore, the complexity of binary search is O(log n).
  10. 10. CORRECTNESS OF BINARY SEARCH  Lemma: For all n>=0, if BinarySearch(E, first , last, k) is called, and the size is (last-first+1)=n and E[first], …E[last] are in ascending order, then it returns -1 if k doesn’t occur in E within the range first, … last and it returns index such that k=E[index] otherwise PROOF BY INDUCTION Base case: n = last − first + 1 = 0  The array is empty, so first = last + 1  Line 1 i.e. (first > last) is true , line 2 is reached and the algorithm correctly returns -1.  Therefore, the element is not present in the array. Inductive step: For n>0, assume that the binary search(E, first, last ,k) satisfies the lemma on size k such that 0<=k<n, and first and the last are any indexes such that k=last-first+1
  11. 11.  For n>0, the algorithm first calculates mid = (first + last) /2 thus line 1 is false, as mid is within the search range (first<=mid<=last)  If line 5 is true, the procedure terminates with index=mid  If line 5 is false, from (first<=mid<=last) and def of n, (mid -1)- first +1<=(n -1) and last –(mid+1)+1<=(n-1)  If line 7 is true, then line 8 is executed ,we can assure that the call accomplishes the objective of binary search. If line 8 returns a positive index then our procedure is finished, and if it returns -1, this implies that k is not present in E in the range first,…. mid-1. Also since line 7 is true , k is not in E in range mid,... last , so returning -1 is correct.  If line 7 is false, then line 10 is executed and the argument is similar.
  12. 12. THANK YOU !
  • shaikhsuhaan

    Feb. 21, 2021
  • RafathFatima2

    Feb. 3, 2020
  • ChUzair3

    Dec. 30, 2019
  • Shilpa1210

    Sep. 16, 2019
  • ShivaniTiwari53

    Apr. 28, 2019
  • VaishnaviPatil55

    Apr. 25, 2019
  • Archanafornotes

    Mar. 29, 2019
  • SachinKanasiya

    Nov. 19, 2017
  • KashmalaAmjad1

    Jul. 12, 2017
  • Jubayer_Hasan

    Jan. 18, 2017
  • ravinasahani

    Sep. 13, 2016

Binary Search - Intro , Algorithm and Analysis

Vistas

Total de vistas

11.459

En Slideshare

0

De embebidos

0

Número de embebidos

1

Acciones

Descargas

455

Compartidos

0

Comentarios

0

Me gusta

11

×