For computer science and complicated data searching, binary search is definitely a good option. Normally, it is one of the fastest searching algorithms in sorted arrays. If you want to learn more about binary search, please read this article at algo.monster. We will explain how it works with detailed information and examples.

This tutorial will mainly discuss the following questions:

What is search?

What is binary search?

How does half-interval search work?

Example of binary search.

Binary Search: Why do we need it?

Binary search vs. linear search.

**What is Search?**

Search is a method that allows its users to search files or any other data stored in a database. Search works by matching criteria to records and then displaying the results to the user. This is how the simplest search function works.

**What is binary search?**

Binary search is an advanced search algorithm that retrieves data from a sorted set of items. The core principle of a half-interval search is to split the data in the list in half. And the process repeats until the target value is located and displayed to the user as a search result. Binary search is also known as a logarithmic or half-interval search.

**The binary search works as follows**

The search begins by finding the middle element in the sorted array.

The key value and element are then compared.

For comparison and matching purposes, search engines will analyze the values of the upper elements if the key value is lower than the middle component.

If the target value is more than the middle value, then the search analyzes the lower values of the middle element. Then, it will see if they can be compared and matched.

**Example of binary search**

Let’s take the example of a dictionary. To find a word, one does not go through every word sequentially. Instead, one randomly finds the closest words to search for it.

Say, we have a sorted array:

Index from 0 to 9. Also, the array values correspond to the index values as you will see below.

Array values: 6 8 17 21 24 45 59 63 76 89

This is what the above numbers show: An array of 10 digits is available. The element 59 must be found.

The indexes for all elements range from 0 to 9. The middle of the array can now be calculated. Divide the index’s left and rightmost values by 2, and you will get the middle. We take the floor value, which is 4.5. The middle value is therefore 4.

Because 59 is greater than 24, the algorithm will drop all elements from the middle bound (4) to the lowest bound. The array now has 5 elements.

Now, 59 is higher than 45 and lower than 63. The middle is 7. The middle is 7.

You will now know that 45 is followed by 59. The left index, which has 5 points, becomes mid.

This process continues until the array becomes one element or the item to find in the middle.

**Example 2 **

Take a look at this example to see how binary search works.

[2 4 6 8 10 12 14 16 18 20]

There are a variety of values that you want to find, ranging in number from 2 to 20, and you need to locate 18.

The average of both the lower and higher limits is (l +r)/2 = 4. The value that is being searched is higher than the middle, which is 4.

Searches for array values lower than the mid-value are dropped. Values greater than the middle-value 4 can be searched.

This is a continuous dividing process that continues until the actual item is found.

**Binary search: why do we need it?**

These are the main reasons the binary search is better than any other search algorithm.

Half-interval search works on any data type, regardless of how big it is.

Binary algorithms use random access to data in order to find the element. It searches data randomly, instead of looking through it in sequential order. This makes it faster and more precise to search data.

Half-interval search is a way to compare the data sorted using an ordering principle, rather than equality comparisons. This can lead to slow results and sometimes inaccurate.

After each cycle of searching, the algorithm divides an array in half. It will not work in the remainder of the array until the next iteration.

**Binary search vs. linear search**

Except for binary, linear search is also a common searching method. Let’s look at the table below to see the difference.

Algorithms |
Binary search |
Linear search |

Approaches |
Divide and conquer. | Iterative. |

Basics |
Based on divide and conquer algorithm only for sorted array. | From the start to the end, comparing with each one sequentially. |

Time compliexy |
O(log2 n) | O(n) |

Efficiency |
Efficient. | Not efficient for large data set. |

Best case |
Matching at the first splitting as in the middle of the array. | The matching value shows up at the first position. |

Worst case |
Could come to the conclustion after only log2 N comparisons. | Need to complete N comparisons. |

Best case time |
In the centner, O(1) | The first element, O(1) |

Requirements for the array |
Sorted order. | No. |

Implementation |
Sorted array. | Linked list and array. |

Code lines |
More | Less. |

**Summary**

Search is a utility that allows users to search for files and documents. Binary search is an advanced search algorithm that retrieves data from a sorted set of items.

Binary search is also known as a half-interval or logarithmic search.

It divides the array in half when the element that is required is found.

By dividing the sums of the left- and rightmost index values by 2, the binary algorithm finds the middle of an array. Depending on which element is to be found, the algorithm will drop either the lower or the upper bound of elements from the middle of the array.

Randomly, the algorithm accesses data to locate the element. This makes search cycles faster and more precise.

Binary search compares the sorted data using an ordering principle, rather than using slow and inaccurate equality comparisons.

Unsorted data is not compatible with a half-interval search.

Binary search is normally more efficient than linear when it comes to sorted arrays in a large dataset.