The Wayback Machine - https://web.archive.org/web/20240725063314/https://www.geeksforgeeks.org/time-and-space-complexity-of-linear-search-algorithm/
Open In App

Time and Space Complexity of Linear Search Algorithm

Last Updated : 18 Mar, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

The time complexity of the Linear Search algorithm is O(n), where n is the number of elements in the array. The space complexity is O(1) as it requires a constant amount of extra space regardless of the input size.

Linear Search Algorithm

Aspect Complexity
Time Complexity O(n)
Space Complexity O(1)

Let’s explore the detailed time and space complexity of the Linear Search Algorithm:

Time Complexity of Linear Search Algorithm:

Best Case Time Complexity of Linear Search Algorithm: O(1)

Best case is when the list or array’s first element matches the target element. The time complexity is O(1) because there is just one comparison made. So the best case complexity is O(1).

Average Case Time Complexity of Linear Search Algorithm: O(n)

The average case time complexity of the linear search algorithm is O(n), where n is the number of elements in the array being searched.

Worst Case Time Complexity of Linear Search Algorithm: O(n)

The worst-case will be when the target element is absent from the list or array or is located at the end of the list. O(n) time complexity results from the necessity to traverse the complete list and the n comparisons that are required.

Auxiliary Space Complexity of Linear Search Algorithm:

The auxiliary space complexity of the linear search algorithm is O(1), which means it uses a constant amount of additional space regardless of the size of the input array.


Similar Reads

Time Complexity and Space Complexity
Generally, there is always more than one way to solve a problem in computer science with different algorithms. Therefore, it is highly required to use a method to compare the solutions in order to judge which one is more optimal. The method must be: Independent of the machine and its configuration, on which the algorithm is running on.Shows a direc
14 min read
Time and Space Complexity Analysis of Binary Search Algorithm
Time complexity of Binary Search is O(log n), where n is the number of elements in the array. It divides the array in half at each step. Space complexity is O(1) as it uses a constant amount of extra space. AspectComplexityTime ComplexityO(log n)Space ComplexityO(1)The time and space complexities of the binary search algorithm are mentioned below.
3 min read
Time and Space Complexity of DFS and BFS Algorithm
The time complexity of both Depth-First Search (DFS) and Breadth-First Search (BFS) algorithms is O(V + E), where V is the number of vertices and E is the number of edges in the graph. The space complexity of DFS is O(V), where V represents the number of vertices in the graph, and for BFS, it is O(V), where V represents the number of vertices in th
2 min read
Time and Space Complexity of Breadth First Search (BFS)
What is Breadth First Search?The Breadth First Search (BFS) algorithm is used to search a graph data structure for a node that meets a set of criteria. It starts at the root of the graph and visits all nodes at the current depth level before moving on to the nodes at the next depth level. Although there are other methods for traversing the graph, B
3 min read
Time and Space Complexity of Ternary Search
The time complexity of Ternary Search is O(log3 N), where N is the size of the array. In terms of space complexity, ternary search requires only O(1) auxiliary space, as it operates directly on the given array without creating any additional data structures. Feature Ternary Search Time Complexity O(log3N) Auxiliary Space O(log3 N) Let's explore the
2 min read
Time and Space Complexity of Depth First Search (DFS)
The time complexity of Depth First Search (DFS) is O(V + E), where V is the number of vertices and E is the number of edges. The space complexity is O(V) for a recursive implementation. Below are the detailed explanation of Time and Space Complexity of Depth First Search (DFS): Time Complexity of Depth First Search (DFS):Best Case of Depth First Se
2 min read
Time and Space Complexity of Floyd Warshall Algorithm
The Floyd Warshall Algorithm has a time complexity of O(V3) and a space complexity of O(V2), where V represents the number of vertices in the graph. This algorithm computes the shortest paths between all pairs of vertices in a weighted graph. The time complexity arises from the triple nested loops used to update the shortest path matrix, while the
3 min read
Time and Space Complexity of Bellman–Ford Algorithm
The Bellman-Ford algorithm has a time complexity of O(V*E), where V is the number of vertices and E is the number of edges in the graph. In the worst-case scenario, the algorithm needs to iterate through all edges for each vertex, resulting in this time complexity. The space complexity of the Bellman-Ford algorithm is O(V), where V is the number of
2 min read
Time and Space complexity of Radix Sort Algorithm
The Radix Sort Algorithm has a time complexity of O(n*d), where n is the number of elements in the input array and d is the number of digits in the largest number. The space complexity of Radix Sort is O(n + k), where n is the number of elements in the input array and k is the range of the input. This algorithm is efficient for sorting integers, es
2 min read
Time and Space Complexity of Dijkstra’s Algorithm
The time complexity of Dijkstra's Algorithm is typically O(V2) when using a simple array implementation or O((V + E) log V) with a priority queue, where V represents the number of vertices and E represents the number of edges in the graph. The space complexity of the algorithm is O(V) for storing the distances and predecessors for each node, along
3 min read
three90RightbarBannerImg