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

Asked

Viewed 149 times

7

What happens if a binary search does not find the element? It goes into loop? What is the best way to finish the execution?

  • You need to indicate that you did not find the element. Type the same "error code" return used in linear search.

  • Related: https://answall.com/q/167105/64969

1 answer

10


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

It closes the search without finding the element. Nothing more. Pure logic.

He gets into loop?

Why would you do that? A binary search will always analyze the elements in the previous or later half of the last element analyzed, soon half comes to have the size of an element and has nothing more to check. Even if you have a billion elements, you’ll only need 30 analyses. Precisely if you arrive at the point of no longer having to verify and gave no equality in any analysis then did not find the element sought.

What is the best way to finish the execution?

It doesn’t have the best shape, at least in general, it can have in detail. Shutdown should occur as soon as you find the element or have no more elements to analyze, whichever comes first.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.