badge icon

This article was automatically translated from the original Turkish version.

Article

Search Algorithms Used in Series

Math

+2 More

dogrusalarama.webp
Linear Search

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

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)


C++ Code Example for Linear Search Algorithm

Binary Search

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)


C++ Code Example for Binary Search Algorithm

Two Pointers Technique

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)


C++ Code Example for Two Pointers Algorithm


Which Algorithm Should Be Used Under Which Conditions?

Search algorithms used in arrays offer different performance advantages.

  • Linear search is a simple and straightforward method for unsorted arrays but becomes inefficient with large datasets.
  • Binary search is highly efficient for sorted arrays and is better suited for large datasets.
  • Two pointers technique provides additional efficiency for problems requiring specific conditions to be met.


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.

Author Information

Avatar
AuthorAbdulsamet EkinciDecember 24, 2025 at 6:12 AM

Tags

Discussions

No Discussion Added Yet

Start discussion for "Search Algorithms Used in Series" article

View Discussions

Contents

  • 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?

Ask to Küre