What happens if a binary search does not find the element?

6

What happens if a binary search does not find the element? Does it enter loop ? What is the best way to end execution?

    
asked by anonymous 23.11.2017 / 02:52

1 answer

6
  

What happens if a binary search does not find the element?

Close the search without finding the element. Nothing more than this. Pure logic.

  

Does it loop?

Why would you do this? A binary search will analyze the elements always in the half before or after the last analyzed element, quickly half of it arrives to have size of an element and it does not have more to verify. Even if you have 1 billion elements, you only need 30 analyzes. Just to get to the point of not having more to check and did not give equality in any analysis so did not find the element sought.

  

What is the best way to end execution?

It does not have the best shape, at least in general, you can have in detail. Closing should occur as soon as you find the element or have no more elements to parse, whichever comes first.

    
23.11.2017 / 04:05