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 algorithm’s structure and whether the array is sorted.
Fundamentally, 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 algorithm technique, the array is repeatedly divided into two halves—either the left or the right subarray. The algorithm compares the target value with the middle element of the array. If the target value is smaller than the middle element, the search continues in the left half; if it is larger, the search proceeds in the right half. In a finite array that is continuously halved, the left and right pointers eventually converge, and the search terminates.
This method has a time complexity of O(log n) and is significantly faster than linear search. However, it requires the array to be sorted.

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 according to predefined conditions until the desired result is achieved.
For example, this technique is used to find two elements whose sum equals a target value. If the current sum is greater than the target, the right pointer is moved left; if it is smaller, the left pointer is moved right. It has a time complexity of O(n) and is more efficient than linear search for large datasets.

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. Determining the optimal search algorithm ensures efficient utilization of system resources.
Geeksforgeeks. (2024). "Binary Search Algorithm – Iterative and Recursive Implementation."Accessed Adresi
Geeksforgeeks. (2024). "Linear Search Algorithm."Accessed Adresi
Geeksforgeeks. (2024). "Searching in Array."Accessed Adresi
Geeksforgeeks. (2024). "Two Pointers Technique."Accessed Adresi

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?