KEMBAR78
Binary search | PPTX
Data Structure - 1
Now as most of you must be knowing a major drawback of
the linear search that it is extremely time consuming for large
data sets .
Consider for Example we have an array with only five numbers
[1,3,0,8,5] and we want to search the number 5 in the array.
Then the maximum time taken to search the array would be
basically five iterations.
However , consider a case where you have say 10,000
numbers in an array [i.e. numbers from 1 to 10000 in any
random fashion] and we want to search an element which is
the last number in the array.
Now as you can see here this is extremely time consuming as
it would take 10000 iterations to search the number.
This Is where we use binary search to speed up the operation
significantly.
The pre requirement for binary search is that the array should
be in a sorted order.
The following example will give you a better understanding of
the working of the binary search algorithm.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Suppose the user wants to find the number 7 in the array of 20 numbers
given below. To find 7, the number would be initially compared with middle
element of the array say 10.
Iteration no - 01
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Now as we see the array is sorted and the number 7 is less than 10 the array
after the number 10 is totally discarded thus reducing the array to be
searched by half.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Now similar process is done on the rest of the array. Now the array to be
searched is between the numbers 1 to 10. Again in a similar fashion the
middle element of the array is searched and compared with the number 7.
Again since 5 is the middle element it is not the number to be searched.
Iteration no - 02
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Now here as we see the number 7 is greater than 5 the array numbers 1 to 4
are discarded and the array to be searched is now from 5 to 10.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Again the same thing is done . The middle element of the array is found out
i.e. the middle element of the array between 5 to 10 which turns out to be 7
the number to be found out.
Iteration no - 03
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Thus using binary search we found out the number 7 from an array of 20
numbers in 3 iterations which would have taken 7 iterations using linear
search thus reducing the work by half.
Thus we see that binary search is a much more efficient
algorithm for searching in a large list then the linear search
algorithm.
The only drawback of the algorithm being the array on which
the search is being performed should be COMPULSORILY in
the sorted order.
Binary search
Binary search

Binary search

  • 1.
  • 2.
    Now as mostof you must be knowing a major drawback of the linear search that it is extremely time consuming for large data sets . Consider for Example we have an array with only five numbers [1,3,0,8,5] and we want to search the number 5 in the array. Then the maximum time taken to search the array would be basically five iterations.
  • 3.
    However , considera case where you have say 10,000 numbers in an array [i.e. numbers from 1 to 10000 in any random fashion] and we want to search an element which is the last number in the array. Now as you can see here this is extremely time consuming as it would take 10000 iterations to search the number. This Is where we use binary search to speed up the operation significantly.
  • 4.
    The pre requirementfor binary search is that the array should be in a sorted order. The following example will give you a better understanding of the working of the binary search algorithm.
  • 5.
    1 2 34 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Suppose the user wants to find the number 7 in the array of 20 numbers given below. To find 7, the number would be initially compared with middle element of the array say 10. Iteration no - 01
  • 6.
    1 2 34 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Now as we see the array is sorted and the number 7 is less than 10 the array after the number 10 is totally discarded thus reducing the array to be searched by half.
  • 7.
    1 2 34 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Now similar process is done on the rest of the array. Now the array to be searched is between the numbers 1 to 10. Again in a similar fashion the middle element of the array is searched and compared with the number 7. Again since 5 is the middle element it is not the number to be searched. Iteration no - 02
  • 8.
    1 2 34 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Now here as we see the number 7 is greater than 5 the array numbers 1 to 4 are discarded and the array to be searched is now from 5 to 10.
  • 9.
    1 2 34 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Again the same thing is done . The middle element of the array is found out i.e. the middle element of the array between 5 to 10 which turns out to be 7 the number to be found out. Iteration no - 03
  • 10.
    1 2 34 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Thus using binary search we found out the number 7 from an array of 20 numbers in 3 iterations which would have taken 7 iterations using linear search thus reducing the work by half.
  • 11.
    Thus we seethat binary search is a much more efficient algorithm for searching in a large list then the linear search algorithm. The only drawback of the algorithm being the array on which the search is being performed should be COMPULSORILY in the sorted order.