Problem: Given an array arr[] of n elements, write a function to search a given element x in arr[].
Examples :
Input : arr[] = {10, 20, 80, 30, 60, 50,
110, 100, 130, 170}
x = 110;
Output : 6
Element x is present at index 6
Input : arr[] = {10, 20, 80, 30, 60, 50,
110, 100, 130, 170}
x = 175;
Output : -1
Element x is not present in arr[].
A simple approach is to do linear search, i.e
- Start from the leftmost element of arr[] and one by one compare x with each element of arr[]
- If x matches with an element, return the index.
- If x doesn’t match with any of elements, return -1.

Example:
C
// C++ code for linearly search x in arr[]. If x // is present then return its location, otherwise // return -1 #include <stdio.h> int search(int arr[], int n, int x) { int i; for (i = 0; i < n; i++) if (arr[i] == x) return i; return -1; } int main(void) { int arr[] = { 2, 3, 4, 10, 40 }; int x = 10; int n = sizeof(arr) / sizeof(arr[0]); int result = search(arr, n, x); (result == -1) ? printf("Element is not present in array") : printf("Element is present at index %d", result); return 0; } |
chevron_right
filter_none
C++
// C++ code for linearly search x in arr[]. If x // is present then return its location, otherwise // return -1 #include <iostream> using namespace std; int search(int arr[], int n, int x) { int i; for (i = 0; i < n; i++) if (arr[i] == x) return i; return -1; } int main(void) { int arr[] = { 2, 3, 4, 10, 40 }; int x = 10; int n = sizeof(arr) / sizeof(arr[0]); int result = search(arr, n, x); (result == -1)? cout<<"Element is not present in array" : cout<<"Element is present at index " <<result; return 0; } |
chevron_right
filter_none
Java
// Java code for linearly search x in arr[]. If x // is present then return its location, otherwise // return -1 class LinearSearch { // This function returns index of element x in arr[] static int search(int arr[], int n, int x) { for (int i = 0; i < n; i++) { // Return the index of the element if the element // is found if (arr[i] == x) return i; } // return -1 if the element is not found return -1; } } |
chevron_right
filter_none
Python
# Python code for linearly search x in arr[]. If x # is present then return its location, otherwise # return -1 def search(arr, x): for i in range(len(arr)): if arr[i] == x: return i return -1 |
chevron_right
filter_none
PHP
<?php // PHP code for linearly search // x in arr[]. If x is present // then return its location, // otherwise return -1 function search($arr, $n, $x) { $i; for ($i = 0; $i < $n; $i++) if ($arr[$i] == $x) return $i; return -1; } // This code is contributed // by jit_t ?> |
chevron_right
filter_none
Output:
Element is present at index 3
The time complexity of above algorithm is O(n).
Linear search is rarely used practically because other search algorithms such as the binary search algorithm and hash tables allow significantly faster searching comparison to Linear search.
Also See – Binary Search
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Linear Search vs Binary Search
- C/C++ Program for Linear Search
- Java Program for Linear Search
- Linear search using Multi-threading
- Number of comparisons in each direction for m queries in linear search
- Repeatedly search an element by doubling it after every successful search
- Anagram Substring Search (Or Search for all permutations)
- Why is Binary Search preferred over Ternary Search?
- Interpolation search vs Binary search
- Best First Search (Informed Search)
- Median of two sorted arrays of different sizes | Set 1 (Linear)
- Find a sorted subsequence of size 3 in linear time
- Sublist Search (Search a linked list in another list)
- Meta Binary Search | One-Sided Binary Search
- Sorted subsequence of size 3 in linear time using constant space

