Home python Python Binary Search

Python Binary Search




I am learning algorithms and I learned that binary search applies only to a sorted list of elements, but it is not written why an unsorted one cannot be used. Can you explain why?

Answer 1, authority 100%

Binary search algorithm assumes iterative division of the list in half, where at each step the desired element is compared with the value of the middle item in the sorted list. If the desired element is less than the middle element, then we will continue to search in the first half, if more, then in the second half, if equal, then we have found the desired element.

If the list is not sorted, then it is impossible to use the half division algorithm, because the element you are looking for can appear in any half of the list.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions