Linear Search Explained (2024)

/ #Algorithms
Linear Search Explained (1)

What is Linear Search?

Suppose you are given a list or an array of items. You are searching for a particular item. How do you do that?

Find the number 13 in the given list.

Linear Search Explained (2)

You just look at the list and there it is!

Linear Search Explained (3)

Now, how do you tell a computer to find it?

A computer cannot look at more than the value at a given instant of time. So it takes one item from the array and checks if it is the same as what you are looking for.

Linear Search Explained (4)

The first item did not match. So move onto the next one.

Linear Search Explained (5)

And so on…

This is done till a match is found or until all the items have been checked.

Linear Search Explained (6)

In this algorithm, you can stop when the item is found and then there is no need to look further.

So how long would it take to do the linear search operation? In the best case, you could get lucky and the item you are looking at maybe at the first position in the array!

But in the worst case, you would have to look at each and every item before you find the item at the last place or before you realize that the item is not in the array.

The complexity of linear search is therefore O(n).

If the element to be searched lived on the the first memory block then the complexity would be: O(1).

The code for a linear search function in JavaScript is shown below. This function returns the position of the item we are looking for in the array. If the item is not present in the array, the function will return null.

Example in Javascript

function linearSearch(arr, item) { // Go through all the elements of arr to look for item. for (var i = 0; i < arr.length; i++) { if (arr[i] === item) { // Found it! return i; } } // Item not found in the array. return null;}

Example in Ruby

def linear_search(target, array) counter = 0 while counter < array.length if array[counter] == target return counter else counter += 1 end end return nilend

Example in C++

int linear_search(int arr[],int n,int num){for(int i=0;i<n;i++){if(arr[i]==num)return i; } // Item not found in the array return -1; }

Example in Python

def linear_search(array, num):for i in range(len(array)):if (array[i]==num):return ireturn -1

Global Linear Search

What if you are searching the multiple occurrences of an element? For example you want to see how many 5’s are in an array.

Target = 5

Array = [ 1, 2, 3, 4, 5, 6, 5, 7, 8, 9, 5]

This array has 3 occurrences of 5s and we want to return the indexes (where they are in the array) of all of them.

This is called global linear search and you will need to adjust your code to return an array of the index points at which it finds your target element.

When you find an index element that matches your target, the index point (counter) will be added in the results array. If it doesn’t match, the code will continue to move on to the next element in the array by adding 1 to the counter.

def global_linear_search(target, array) counter = 0 results = [] while counter < array.length if array[counter] == target results << counter counter += 1 else counter += 1 end end if results.empty? return nil else return results endend

Why linear search is not efficient

There is no doubt that linear search is simple. But because it compares each element one by one, it is time consuming and therefore not very efficient. If we have to find a number from, say, 1,000,000 numbers and that number is at the last position, a linear search technique would become quite tedious.

So you should also learn about bubble sort, quick sort and other more efficient algorithms.

Other search algorithms:

ADVERTIsem*nT

ADVERTIsem*nT

ADVERTIsem*nT

If this article was helpful, .

Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started

ADVERTIsem*nT

Linear Search Explained (2024)

FAQs

Linear Search Explained? ›

Linear Search is an algorithm that iteratively checks each element in an array, starting from the first element, in a linear or sequential manner until it finds a match with the target value. If the algorithm does not find a match, it denotes that the target value is not present in the array.

How to explain linear search? ›

A linear search is the simplest approach employed to search for an element in a data set. It examines each element until it finds a match, starting at the beginning of the data set, until the end. The search is finished and terminated once the target element is located.

Why linear search is not efficient? ›

Linear search becomes inefficient for large-sized arrays or lists, as it needs to scan through each element sequentially. It has a time complexity of O(n), which means the search time grows linearly with the size of the input.

What is the main disadvantage of using a linear search algorithm? ›

Inefficient for sorted data: When the data is already sorted, linear search is not efficient because it needs to check each element one by one, even if it has already passed the target element.

What is one problem with using a linear search as an algorithm to search through a list? ›

A linear search is not very efficient. The best case for a linear search is that you find the item you are looking for with the first search, but the worst case is that you have to search every possible location. Therefore, for 100 items, on average a linear search would require: (100 + 1) = 50.5 search steps.

What is a simple explanation of linear? ›

In Mathematics, a linear function is defined as a function that has either one or two variables without exponents. It is a function that graphs to the straight line.

How does linear search work step by step? ›

Linear search
  1. Identify a search term.
  2. Look at the first item in the list.
  3. Compare the item with the search term.
  4. Is the current item the same as the search term? ...
  5. Repeat from step two until the last item in the list has been reached.

What is the greatest weakness of linear search? ›

Linear search scans each element sequentially until the computer identifies the intended element or searches the entire data set. Linear search's disadvantage is the time complexity. Because linear search scans each element starting from the beginning, it is highly inefficient.

What is more efficient than linear search? ›

Binary search is a more efficient searching algorithm compared to linear search. It works by taking advantage of a sorted array or list, repeatedly dividing the search interval in half.

What is a linear search most suitable for? ›

Simplicity: Linear Search is straightforward and easy to implement, making it a good choice for small datasets and simple search tasks. Efficiency: Linear Search can be inefficient for large datasets as it checks each element sequentially, leading to a higher time complexity (O(n)) in worst-case scenarios.

Why is binary search better than linear search? ›

So, the binary search takes less time to search an element as compared to a linear search. The one pre-requisite of binary search is that an array should be in sorted order, whereas the linear search works on both sorted and unsorted array.

What is a real life example of a linear search? ›

One of the most straightforward and elementary searches is the sequential search, also known as a linear search. As a real world example, pickup the nearest phonebook and open it to the first page of names.

Which of the following disadvantages of linear search? ›

Correct option is B. Greater time complexities compared to other searching algorithms.

What is linear search in detail with example? ›

Linear search is used on a collections of items. It relies on the technique of traversing a list from start to end by exploring properties of all the elements that are found on the way. For example, consider an array of integers of size . You should find and print the position of all the elements with value .

What is linear search with an example? ›

One of the most straightforward and elementary searches is the sequential search, also known as a linear search. As a real world example, pickup the nearest phonebook and open it to the first page of names. We're looking to find the first "Smith". Look at the first name.

What is linear search explain with its algorithm? ›

Linear search, the simplest search algorithm, is mainly used to find the element from an unordered list. It is also known by another name called sequential search algorithm. In linear search, the list is simply traversed, and each element in the list is matched with the element whose location needs to be found.

How do you explain a linear model? ›

A linear model example is a verbal scenario that can be modeled using a linear equation or vice versa. An example could be each pizza costs $10 and the delivery fee is $5, so the linear model would be y=10x+5, where y represents the total cost and x represents the number of pizzas.

Top Articles
Latest Posts
Article information

Author: Barbera Armstrong

Last Updated:

Views: 5764

Rating: 4.9 / 5 (79 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Barbera Armstrong

Birthday: 1992-09-12

Address: Suite 993 99852 Daugherty Causeway, Ritchiehaven, VT 49630

Phone: +5026838435397

Job: National Engineer

Hobby: Listening to music, Board games, Photography, Ice skating, LARPing, Kite flying, Rugby

Introduction: My name is Barbera Armstrong, I am a lovely, delightful, cooperative, funny, enchanting, vivacious, tender person who loves writing and wants to share my knowledge and understanding with you.