67.4k views
1 vote
From the binary search algorithm, it follows that every iteration of the while loop cuts the size of the search list by half.

True

False

User MassyB
by
7.3k points

1 Answer

3 votes

Answer:

True: In binary search algorithm, we follow the below steps sequentially:

Input: A sorted array B[1,2,...n] of n items and one item x to be searched.

Output: The index of x in B if exists in B, 0 otherwise.

  1. low=1
  2. high=n
  3. while( low < high )
  4. { mid=low + (high-low)/2
  5. if( B[mid]==x)
  6. {
  7. return(mid) //returns mid as the index of x
  8. }
  9. else
  10. {
  11. if( B[mid] < x) //takes only right half of the array
  12. {
  13. low=mid+1
  14. }
  15. else // takes only the left half of the array
  16. {
  17. high=mid-1
  18. }
  19. }
  20. }
  21. return( 0 )

Step-by-step explanation:

For each iteration the line number 11 or line number 15 will be executed.

Both lines, cut the array size to half of it and takes as the input for next iteration.

User Prajna
by
7.5k points