This article was automatically translated from the original Turkish version.
+2 More

Search algorithms in arrays are systematic methods used to locate a specific element. These algorithms are designed to determine whether a particular value exists within a given array. The efficiency of the search operation depends on the structure of the algorithm and whether the array is sorted.
The three most commonly used search algorithms are Linear Search, Binary Search, and Two Pointers Technique. Each of these algorithms offers distinct advantages and disadvantages depending on the data structure and requirements.
Linear search is one of the simplest and most fundamental search algorithms. In this method, each element of the array is checked sequentially, starting from the first element, until the target element is found. If the element is found in the array, its index is returned; otherwise, a "not found" result is returned.
This algorithm has a time complexity of O(n) and can be inefficient for large datasets. It is one of the most suitable methods for unsorted arrays.

Linear Search Step 1 (Source: www.geeksforgeeks.org)

Linear Search Step 3 (Source: www.geeksforgeeks.org)
Binary search is a highly efficient search method that works only on sorted arrays. In this technique, the array is repeatedly divided into two halves—either the left or the right half—based on comparison with the middle element. The algorithm compares the target value with the middle element of the array. If the target is smaller than the middle element, the search continues in the left half; if it is larger, the search proceeds in the right half. This process of halving continues until the left and right pointers converge, at which point the search ends.
This technique has a time complexity of O(log n) and is significantly faster than linear search. However, it requires the array to be sorted beforehand.

Binary Search Initial Visualization (Source: www.geeksforgeeks.org)

Binary Search Step 1 (Source: www.geeksforgeeks.org)

Binary Search Step 2 (Source: www.geeksforgeeks.org)

Binary Search Step 3 (Source: www.geeksforgeeks.org)
The two pointers technique is particularly useful for finding a specific target value or satisfying a certain condition in sorted arrays. In this method, two distinct pointers are initialized—one at the beginning and one at the end of the array. These pointers are moved toward each other according to predefined conditions until the desired result is achieved.
For example, this technique is commonly used to find two elements whose sum equals a target value. If the sum is greater than the target, the right pointer is moved leftward; if the sum is smaller, the left pointer is moved rightward. It has a time complexity of O(n) and is more efficient than linear search for large datasets under appropriate conditions.

Two Pointers Technique Step 1 (Source: www.geeksforgeeks.org)

Two Pointers Technique Step 2 (Source: www.geeksforgeeks.org)

Two Pointers Technique Step 3 (Source: www.geeksforgeeks.org)

Two Pointers Technique Step 4 (Source: www.geeksforgeeks.org)

Two Pointers Technique Step 5 (Source: www.geeksforgeeks.org)

Two Pointers Technique Step 6 (Source: www.geeksforgeeks.org)
Search algorithms used in arrays offer different performance advantages.
The choice of the appropriate algorithm depends on the size of the data structure, whether it is sorted, and the specific processing requirements. Selecting the optimal search algorithm ensures efficient utilization of computational resources.

No Discussion Added Yet
Start discussion for "Search Algorithms Used in Series" article
Linear Search
C++ Code Example for Linear Search Algorithm
Binary Search
C++ Code Example for Binary Search Algorithm
Two Pointers Technique
C++ Code Example for Two Pointers Algorithm
Which Algorithm Should Be Used Under Which Conditions?