# What Do You Mean by Linear Search in Data Structure?

//

Larry Thompson

What Do You Mean by Linear Search in Data Structure?

When it comes to searching for an element in a list or an array, one of the simplest and most straightforward methods is known as the linear search. This algorithm is widely used due to its simplicity and ease of implementation.

## Overview

The linear search, also referred to as sequential search, involves sequentially checking each element in a list until a match is found or until the entire list has been traversed. It starts from the beginning and compares each element with the Target value until a match is found.

### Algorithm

The algorithm for linear search can be summarized as follows:

1. Start from the first element of the list.
2. Compare each element with the Target value.
3. If a match is found, return the index of the element.
4. If the entire list has been traversed without finding a match, return -1 to indicate that the Target value is not present in the list.

### Example

To better understand how linear search works, let’s consider an example. Suppose we have an array of integers: [5, 9, 2, 7, 12].

We want to find if the number 7 exists in this array using linear search. Here’s how it would be done:

1. Start from the first element: 5
2. Compare it with our Target value: 7 (not a match)
3. Move to the next element: 9
4. Compare it with our Target value: 7 (not a match)
5. Continue this process until we reach the last element: 12
6. Compare it with our Target value: 7 (not a match)

Since we have traversed the entire array without finding a match, we can conclude that the number 7 is not present in the array.

## Time Complexity

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

## Conclusion

The linear search algorithm is a simple yet effective approach for finding an element in a list or an array. It may not be as efficient as other search algorithms like binary search, but it serves as a good starting point for understanding searching concepts in data structures.

By utilizing linear search, you can easily locate specific elements within a list or an array. Remember to consider the time complexity when working with large datasets to ensure optimal performance.