Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binary search, sorted array

I'm learning about binary search and the basic definition starts with an iterator to the first element and another one to the last. You also have a key, which is the element you are looking for. The key is compared to the value of the midpoint first and then upper half or lower half is eliminated depending on whether the key is larger or smaller than the value of the midpoint. And the process continues until there is a match.

Wouldn't this method require that the container you are looking through is sorted? Otherwise I don't see how the comparison between the key and values in the container to eliminate portions of the container to look through is any particular use.

like image 332
Mars Avatar asked Sep 09 '13 14:09

Mars


People also ask

Does binary search work on sorted array?

Binary Search is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(Log n).

Why does a binary search need a sorted array?

Binary search works by assuming the middle of the array contains the median value in the array. If it is not sorted, this assumption does not make sense, since the median can be anywhere and cutting the array in half could mean that you cut off the number you were searching for.

How does binary search work on sorted list?

Binary search is an efficient algorithm for finding an item from a sorted list of items. It works by repeatedly dividing in half the portion of the list that could contain the item, until you've narrowed down the possible locations to just one.

Which search is best for sorted array?

If we know nothing about the distribution of key values, then we have just proved that binary search is the best algorithm available for searching a sorted array.


2 Answers

Yes, it does.

In computer science, a binary search or half-interval search algorithm finds the position of a specified input value (the search "key") within an array sorted by key value.

Source: Wikipedia: Binary Search Algorithm, though any other decent text on the algorithm should mention the array must be sorted.

like image 50
Joni Avatar answered Oct 09 '22 23:10

Joni


The answer is yes. The binary search assume that you sort your set first. If I am not wrong, any search algorithm with performance better than O(N) required that your set is stored in some special data structure (sorted list, binary tree, red-black tree...).

When you implement a binary search for a set, you have to make sure that the set if sorted first. Usually you would sort the list first and then always add elements in the right place (to make sure that the new set is still sorted). Assuming you also use binary search to find the right the place to add, both adding and searching is O(log2(N)) in the worst case scenario.

When you consider different search algorithms, you must also consider the underlying data structure and the cost to add element into it.

like image 43
trungdinhtrong Avatar answered Oct 09 '22 22:10

trungdinhtrong