Linear Search Algorithm

Distribute Education like Computer Geek

Definition of Linear Search

Linear search is a straightforward searching algorithm that traverses a given list or array of elements sequentially to find a specific target element. It starts from the beginning and checks each element until a match is found or the entire list is traversed.

The linear search algorithm was introduced by Richard E. Bellman in 1963. BellmanĀ is the same scientist who created the Bellman Ford algorithm.

Algorithm of Linear Search
  1. Start at the beginning of the list.
  2. Compare the target element with the current element.
  3. If they match, the search is successful, and the element is found.
  4. If the elements do not match, move to the next element in the list.
  5. Repeat steps 2-4 until the target element is found or the end of the list is reached.
Working of linear search

Let’s consider an example to better understand linear search. Suppose we have an array of integers: [5, 8, 3, 10, 1, 12, 7, 4].

We want to find the element 12 using linear search.

linear search

Start at the beginning of the array: [5, 8, 3, 10, 1, 12, 7, 4].

  1. Compare the first element, 5, with the target element, 12.Ā 

linear search

They don’t match.

  1. Move to the next element, 8.Ā 

They still don’t match.

  1. Continue comparing with each subsequent element until we reach 12, which is a match.

linear search

They still don’t match.

linear search

They still don’t match.

Linear Search

They still don’t match.

linear search

The search is successful, and the element 12 is found and the index of 12 is 6.

Source Code for Linear Searching

Time Complexity

The time complexity of linear search is directly proportional to the size of the list or array.

  1. In the worst-case, when the target element is located at the end of the list or not present at all, linear search would iterate through all the elements, resulting in a time complexity of O(n), where n is the number of elements in the list.
  2. In the best-case, the target element is present in the array[1], resulting in a time complexity of O(1).
  3. In the average case, when the target element is present in the list, but its exact position is unknown. In this scenario, the algorithm will traverse, on average, half of the elements in the collection before finding the target element.

Average Case

In this case,

Average case = (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8)/8 = int(36/8) = 4, which is equal to n/2.

Mathematically, if we assume that there are n elements in the list, the average case time complexity of linear search can be expressed as O(n/2) or simply O(n), as we drop the constant factor.

.

Advantages of linear search
  1. Simple and easy to implement.
  2. Works well for small lists.
  3. No requirement for a sorted list.
  4. Requires minimal additional memory.
Disadvantages of linear search
  1. Inefficient for large datasets.
  2. Time complexity of O(n) in worst case & average case.
  3. May need to search through the entire list.
  4. Lack of efficiency compared to other search algorithms.
  5. Not suitable for real-time or time-critical applications.
Applications of linear search
  1. Finding an element

Whether the list is sorted or unsorted, linear search can locate the desired element by sequentially scanning through the elements until a match is found.

.

  1. Finding the maximum or minimum element

Linear search can be used to determine the maximum or minimum value within a list. By iterating through the elements and comparing them with the current maximum or minimum, you can update the values accordingly. This is done in selection sort also.

.

  1. Counting occurrences

By iterating through the list and incrementing a counter whenever a match is found, you can determine the frequency of the desired element.

.

  1. Index retrieval

By iterating through the list and comparing elements until a match is found, you can determine the index position of the desired element.

Test Yourself

Q1- Explain the working of the linear search algorithm.

The linear search algorithm sequentially checks each element in a list or array to find a specific target element. It starts from the beginning and compares the target element with each element in the list until a match is found or the end of the list is reached.

Q2- What is the time complexity of linear search in the worst-case scenario?

The worst-case time complexity of linear search is O(n), where n is the number of elements in the list. This occurs when the target element is located at the end of the list or not present at all, requiring iteration through all the elements.

Q3- How does the size of the input data affect the performance of linear search?

The performance of linear search is directly affected by the size of the input data. As the size of the input data increases, the performance of linear search tends to degrade. This is primarily because linear search involves sequentially scanning through each element in the data until the target element is found or the entire dataset is searched.

Q4- When might you choose to use linear search over other search algorithms? Provide real-world scenarios where linear search would be a suitable choice.

Linear search may not be the most efficient search algorithm in many cases, but there are situations where it can still be a suitable choice, especially when considering simplicity, ease of implementation, and specific use cases. Here are some real-world scenarios

  1. Small Datasets – When dealing with very small datasets, the overhead of implementing more complex search algorithms like binary search may not be justified. Linear search can be simpler to implement and understand in such cases.
  2. Unordered Data – If the data is unsorted, linear search is a straightforward choice since other search algorithms like binary search require sorted data to work efficiently.
  3. Early Match Importance – In scenarios where you need to find the first occurrence of a target element, linear search can be effective. You can stop the search as soon as you find the first match, potentially saving time compared to other algorithms.
  4. Search on Small Portion of Data – If you only need to search a small portion of a larger dataset, linear search might be reasonable, especially if the cost of preprocessing the data for other algorithms is higher than the savings in search time.
Q5- What is the time complexity of linear search in the average case?
  1. O(1)
  2. O(log n)
  3. O(n)
  4. O(n2)

Ans – (3)

Q6- Linear search is most suitable for which of the following scenarios?
  1. Large sorted datasets
  2. Real-time applications
  3. Small unsorted lists
  4. Time-critical applications

Ans – (3)

Q7-
What is the worst-case time complexity of linear search?
  1. O(1)
  2. O(log n)
  3. O(n^2)
  4. O(n)

Ans – (4)

Q8- Which search algorithm is more efficient than linear search for large datasets?
  1. Bubble sort
  2. Binary search
  3. Quick sort
  4. Insertion sort

Ans – (2)

Explanation – Binary search has a time complexity of O(log n), where n is the number of elements in the dataset. This logarithmic time complexity makes binary search highly efficient, especially for larger datasets, as it quickly reduces the search space by half with each comparison.

Q9- Which type of list is required for linear search?
  1. Only a sorted list
  2. Only an unsorted list
  3. Both sorted and unsorted lists
  4. None of the above

Ans – (3)

Q10- Which of the following algorithms is based on linear search?
  1. Bubble sort
  2. Insertion sort
  3. Binary search
  4. Quick sort

Ans – (2)

Explanation – Insertion sort is an algorithm that involves iterating through a list of elements and repeatedly inserting each element into its correct position within a sorted sublist. The process of finding the correct position for insertion involves comparisons, similar to a linear search. However, unlike a traditional linear search, insertion sort is used for sorting the entire list rather than searching for a specific element.

BOOKS

Algorithm T H CoremanAlgorithm by Udit AgarwalAlgorithm Ellis HorowitzData Structure & Algorithm Made Easy Narasimha

Ā