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 a 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 to 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;}// Driver codeint main(void){ int arr[] = { 2, 3, 4, 10, 40 }; int x = 10; int n = sizeof(arr) / sizeof(arr[0]); // Function call 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
C
// C code to 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;}// Driver codeint main(void){ int arr[] = { 2, 3, 4, 10, 40 }; int x = 10; int n = sizeof(arr) / sizeof(arr[0]); // Function call 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
Java
// Java code for linearly searching x in arr[]. If x// is present then return its location, otherwise// return -1class GFG { public static int search(int arr[], int x) { int n = arr.length; for (int i = 0; i < n; i++) { if (arr[i] == x) return i; } return -1; } // Driver code public static void main(String args[]) { int arr[] = { 2, 3, 4, 10, 40 }; int x = 10; // Function call int result = search(arr, x); if (result == -1) System.out.print( "Element is not present in array"); else System.out.print("Element is present at index " + result); }} |
chevron_right
filter_none
Python3
# Python3 code to linearly search x in arr[].# If x is present then return its location,# otherwise return -1def search(arr, n, x): for i in range(0, n): if (arr[i] == x): return i return -1# Driver Codearr = [2, 3, 4, 10, 40]x = 10n = len(arr)# Function callresult = search(arr, n, x)if(result == -1): print("Element is not present in array")else: print("Element is present at index", result) |
chevron_right
filter_none
C#
// C# code to linearly search x in arr[]. If x// is present then return its location, otherwise// return -1using System;class GFG { public static int search(int[] arr, int x) { int n = arr.Length; for (int i = 0; i < n; i++) { if (arr[i] == x) return i; } return -1; } // Driver code public static void Main() { int[] arr = { 2, 3, 4, 10, 40 }; int x = 10; // Function call int result = search(arr, x); if (result == -1) Console.WriteLine( "Element is not present in array"); else Console.WriteLine("Element is present at index " + result); }}// This code is contributed by DrRoot_ |
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, $x){ $n = sizeof($arr); for($i = 0; $i < $n; $i++) { if($arr[$i] == $x) return $i; } return -1;}// Driver Code$arr = array(2, 3, 4, 10, 40); $x = 10;// Function call$result = search($arr, $x);if($result == -1) echo "Element is not present in array";else echo "Element is present at index " , $result;// This code is contributed// by jit_t?> |
chevron_right
filter_none
Output
Element is present at index 3
The time complexity of the 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.
Improve Linear Search Worst-Case Complexity
- if element Found at last O(n) to O(1)
- if element Not found O(n) to O(n/2)
Below is the implementation:
Java
// Java program for linear searchimport java.io.*;class GFG { public static void search(int arr[], int search_Element) { int left = 0; int length = arr.length; int right = length - 1; int position = -1; // run loop from 0 to right for (left = 0; left <= right;) { // if search_element is found with left varaible if (arr[left] == search_Element) { position = left; System.out.println( "Element found in Array at " + (position + 1) + " Position with " + (left + 1) + " Attempt"); break; } // if search_element is found with right varaible if (arr[right] == search_Element) { position = right; System.out.println( "Element found in Array at " + (position + 1) + " Position with " + (length - right) + " Attempt"); break; } left++; right--; } // if element not found if (position == -1) System.out.println("Not found in Array with " + left + " Attempt"); } // Driver code public static void main(String[] args) { int arr[] = { 1, 2, 3, 4, 5 }; int search_element = 5; // Function call search(arr,search_element); }} |
chevron_right
filter_none
C#
// C# program for linear searchusing System;class GFG { public static void search(int []arr, int search_Element) { int left = 0; int length = arr.Length; int right = length - 1; int position = -1; // run loop from 0 to right for (left = 0; left <= right;) { // if search_element is found with left varaible if (arr[left] == search_Element) { position = left; Console.WriteLine( "Element found in Array at " + (position + 1) + " Position with " + (left + 1) + " Attempt"); break; } // if search_element is found with right varaible if (arr[right] == search_Element) { position = right; Console.WriteLine( "Element found in Array at " + (position + 1) + " Position with " + (length - right) + " Attempt"); break; } left++; right--; } // if element not found if (position == -1) Console.WriteLine("Not found in Array with " + left + " Attempt"); } // Driver code public static void Main(String[] args) { int []arr = { 1, 2, 3, 4, 5 }; int search_element = 5; // Function call search(arr,search_element); }}// This code is contributed by 29AjayKumar |
chevron_right
filter_none
Output
Element found in Array at 5 Position with 1 Attempt
https://youtu.be/4GPdGsB3OSc
Also See – Binary Search
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
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
- Sentinel Linear Search
- Improving Linear Search Technique
- Interpolation search vs Binary search
- Anagram Substring Search (Or Search for all permutations)
- Why is Binary Search preferred over Ternary Search?
- Sublist Search (Search a linked list in another list)
- Repeatedly search an element by doubling it after every successful search
- Meta Binary Search | One-Sided Binary Search
- Best First Search (Informed Search)
- Find a sorted subsequence of size 3 in linear time
- K'th Smallest/Largest Element in Unsorted Array | Set 2 (Expected Linear Time)
- K'th Smallest/Largest Element in Unsorted Array | Set 3 (Worst Case Linear Time)
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 1
- Find Two Missing Numbers | Set 1 (An Interesting Linear Time Solution)
- Sorted subsequence of size 3 in linear time using constant space

