Quiz Comment Box

Search

Linear Search

Linear Searchlinear search or sequential search is an algorithm that is used to find an element from a list (linked lists or arrays). This algorithm walks through every element of a list until there is a match. Problem: Given an array arr[] of n elements, write a function to search a given element x in arr[].

Input : arr[] = {10, 20, 80, 30, 60, 50,110, 100, 130, 170} x = 110; Output : 6 Element x is present at index 6

Below are the implementation of above algorithm Linear Search into programs

                        
// C code to linearly search x in arr[]. If x
// is present then return its location, otherwise
// return -1
 
#include 
 
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 code
int 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;
}                          
                               
                        
                         Code 
                    
                        
// Java code for linearly searching x in arr[]. If x
// is present then return its location, otherwise
// return -1
 
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(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);
    }
}

    
                        
                         Code 
                    
                        
# Python3 code to linearly search x in arr[].
# If x is present then return its location,
# otherwise return -1
 
 
def search(arr, n, x):
 
    for i in range(0, n):
        if (arr[i] == x):
            return i
    return -1
 
 
# Driver Code
arr = [2, 3, 4, 10, 40]
x = 10
n = len(arr)
 
# Function call
result = search(arr, n, x)
if(result == -1):
    print("Element is not present in array")
else:
    print("Element is present at index", result)                          

                        
                         Code 
                    

Sample Output

arr[] = {10, 20, 80, 30, 60, 50,110, 100, 130, 170} x = 110;

Output : 6

Element x is present at index 6

Visualized

Note: For better Visualized Understanding visit our algorithm Visualizer

Visualize
previous

Exponential Search