If you like our work, please consider supporting us so we can keep doing what we do. And as a current subscriber, enjoy this nice discount!

Also: if you haven’t yet, follow us on Twitter, TikTok, or YouTube!


Discrete search algorithms are a type of algorithm used to solve problems in which the solution is not continuous, but instead consists of discrete values. These algorithms are often used to solve optimization problems, such as finding the shortest path between two points or finding the maximum value of a function. Discrete search algorithms can be either exact or heuristic; that is, they can either find the optimal solution to a problem or an approximate solution that is good enough for most purposes.

A binary search or half-interval search algorithm finds the position of a specified value (the search "key") within a sorted array. A binary search compares the search key value with the middle element of the array. If the keys match, then a matching element has been found and its index, or position, is returned. Otherwise, if the search key is less than the middle element, the algorithm repeats its action on the sub-array to the left of the middle element or, if the search key is greater, on the sub-array to the right. If the remaining array to be searched is empty, then the key cannot be found in the array, and a special "not found" indication is returned. A binary search halves the number of items to check with each iteration, so locating an item (or determining its absence) takes logarithmic time. A binary search is a dichotomic divide and conquers search algorithm.

Binary search requires a sorted array. Binary search can be done on a sorted array of integers, but the array can just as easily be sorted on other types of data, such as floats, strings, or objects with a defined compareTo() method.

Binary search is used to perform one of two basic operations: finding a given value in an array or determining the position at which a given value should be inserted into an array that must remain sorted. The search algorithm is the same in either case, but the instructions given to the algorithm differ depending on which operation is being performed.

The array to be searched is passed as a parameter to the search function. The search function accepts an array, a starting index, and an ending index. It returns a positive integer if the value is found, otherwise, it returns a negative integer. The search begins at the starting index and continues up to the ending index (the search does not include the ending index unless the ending index is equal to the length of the array).

To find a given value, the search function is called with the value to be found and the starting and ending indices of the array to be searched. The search function returns the index of the value if it is found, otherwise, it returns a negative integer.
To determine the position of a given value in an array, the search function is called with the value to be inserted and the starting and ending indices of the array to be searched. The search function returns the index of the first element in the array that is greater than or equal to the value to be inserted. If there is no such element, the search function returns a negative integer.

The time complexity of the binary search is O(log n), where n is the number of elements in the array to be searched.


A ternary search is a search algorithm that finds the position of a target value within a sorted array. It is a Divide and Conquer algorithm that is similar to binary search but with three instead of two partitions of the array.

The array is divided into three partitions: the lower third, the middle third, and the upper third. If the target value is less than the value in the middle of the lower third, then the search continues in the lower third. If the target value is greater than the value in the middle of the lower third but less than the value in the middle of the middle third, then the search continues in the middle third. Otherwise, the search continues in the upper third. This process is repeated until the target value is found or the array is exhausted.

Ternary search is more efficient than binary search for arrays with large sizes because it requires fewer comparisons. However, it is more difficult to implement than binary search.

Ternary search is a type of Divide and Conquer algorithm, which means that it breaks down a problem into smaller subproblems in order to solve the problem more efficiently. The ternary search algorithm is similar to the binary search algorithm, but with three instead of two partitions of the array.

The array is divided into three partitions: the lower third, the middle third, and the upper third. If the target value is less than the value in the middle of the lower third, then the search continues in the lower third. If the target value is greater than the value in the middle of the lower third but less than the value in the middle of the middle third, then the search continues in the middle third. Otherwise, the search continues in the upper third. This process is repeated until the target value is found or the array is exhausted.

Ternary search is more efficient than binary search for arrays with large sizes, because it requires fewer comparisons. However, it is more difficult to implement than binary search.


Do you like our work?
Consider becoming a paying subscriber to support us!