Back to Course
Algorithm
0% Complete
0/82 Steps
-
Getting Started with AlgorithmWhat is an Algorithm?
-
Characteristics of Algorithm1 Topic
-
Analysis Framework
-
Performance Analysis3 Topics
-
Mathematical Analysis2 Topics
-
Sorting AlgorithmSorting Algorithm10 Topics
-
Searching Algorithm6 Topics
-
Fundamental of Data StructuresStacks
-
Queues
-
Graphs
-
Trees
-
Sets
-
Dictionaries
-
Divide and ConquerGeneral Method
-
Binary Search
-
Recurrence Equation for Divide and Conquer
-
Finding the Maximum and Minimum
-
Merge Sort
-
Quick Sort
-
Stassen’s Matrix Multiplication
-
Advantages and Disadvantages of Divide and Conquer
-
Decrease and ConquerInsertion Sort
-
Topological Sort
-
Greedy MethodGeneral Method
-
Coin Change Problem
-
Knapsack Problem
-
Job Sequencing with Deadlines
-
Minimum Cost Spanning Trees2 Topics
-
Single Source Shortest Paths1 Topic
-
Optimal Tree Problem1 Topic
-
Transform and Conquer Approach1 Topic
-
Dynamic ProgrammingGeneral Method with Examples
-
Multistage Graphs
-
Transitive Closure1 Topic
-
All Pairs Shortest Paths6 Topics
-
BacktrackingGeneral Method
-
N-Queens Problem
-
Sum of Subsets problem
-
Graph Coloring
-
Hamiltonian Cycles
-
Branch and Bound2 Topics
-
0/1 Knapsack problem2 Topics
-
NP-Complete and NP-Hard Problems1 Topic
Participants2253
In Progress
Lesson 7, Topic 1
In Progress
Linear Search
Lesson Progress
0% Complete
Linear search is a very basic and simple search algorithm. In Linear search, we search an element or value in a given array by traversing the array from the starting, till the desired element or value is found.
Implementing Linear Search
Following are the steps of implementation that we will be following:
- Traverse the array using a
for
loop. - In every iteration, compare the
target
value with the current value of the array.- If the values match, return the current index of the array.
- If the values do not match, move on to the next array element.
- If no match is found, return
-1
.
To search the number 1 in the array given below, linear search will go step by step in a sequential order starting from the first element in the given array.

Start from the first element, compare k with each element x.

If x == k
, return the index.

Else, return not found.
Linear Search Algorithm
LinearSearch(array, key)
for each item in the array
if item == value
return its index
Linear Search Program in C
// Linear Search in C
#include <stdio.h>
int search(int array[], int n, int x) {
// Going through array sequencially
for (int i = 0; i < n; i++)
if (array[i] == x)
return i;
return -1;
}
int main() {
int array[] = {2, 4, 0, 1, 9};
int x = 1;
int n = sizeof(array) / sizeof(array[0]);
int result = search(array, n, x);
(result == -1) ? printf("Element not found") : printf("Element found at index: %d", result);
}
Linear Search Complexities
Time Complexity: O(n)
Space Complexity: O(1)