What Is Linear Search in Data Structure?


Larry Thompson

Linear search is a basic searching algorithm used in data structures. It is also known as sequential search, as it checks each element one by one until the desired element is found or the end of the list is reached. In this article, we will explore what linear search is, how it works, and its time complexity.

How does linear search work?

Linear search operates on a collection of elements, such as an array or a linked list. It starts from the beginning of the collection and compares each element with the Target value until a match is found or the end of the collection is reached.

To illustrate this process, let’s consider an array containing integers:

int[] numbers = {4, 7, 2, 9, 1};

If we want to find the index of the number 9 in this array using linear search, we would start from the first element and compare it with our Target value:

int Target = 9;
int index = -1; // initialize with -1 (not found)

// Linear search algorithm
for (int i = 0; i < numbers.length; i++) {
    if (numbers[i] == Target) {
        index = i;

The value of 'index' will be 3 because numbers[3] equals 9.

As you can see, the linear search algorithm iterates through each element in the array until it finds a match. Once a match is found, the index of that element is stored in a variable, 'index' in this case, and the loop is terminated using the break statement.

Time Complexity of Linear Search

The time complexity of linear search is O(n), where 'n' represents the number of elements in the collection being searched. This means that as the size of the collection increases, the time taken to perform a linear search also increases linearly.

In the worst-case scenario, when the Target value is not present in the collection or it appears at the very end, linear search will have to compare each element with the Target value until reaching the end. In such cases, it will perform 'n' comparisons.

Advantages and Disadvantages of Linear Search

Linear search has some advantages and disadvantages that are worth considering:

  • Advantages:
    • Simple and easy to understand.
    • No requirements for data to be sorted.
    • Suitable for small collections or unsorted data.
  • Disadvantages:
    • Inefficient for large collections or sorted data.
    • The time taken increases linearly with collection size.
    • If multiple occurrences of a Target value exist, only the first occurrence will be found.

In conclusion, linear search is a basic but useful searching algorithm in data structures. It sequentially checks each element in a collection until finding the desired element or reaching the end. While it may not be the most efficient algorithm for large or sorted data, it is easy to understand and suitable for small collections or unsorted data.

By using the proper HTML styling elements such as bold text, underlined text,

    unordered lists

, and

  • list items
  • , we can make our content visually engaging and organized, enhancing the reader's experience.

    Discord Server - Web Server - Private Server - DNS Server - Object-Oriented Programming - Scripting - Data Types - Data Structures

    Privacy Policy