Algorithm


Linear search, also known as sequential search, is a simple method for finding a target value within a list or array. The basic idea is to iterate through each element in the list and compare it with the target value until a match is found or the end of the list is reached. If the target is found, the index of the element is returned; otherwise, the search indicates that the target is not present in the list.

Code Examples

#1 Linear Search implement in Python

Code - Python Programming

def linear_search(arr, target):
    """
    Perform linear search on the given list to find the target element.

    Parameters:
    - arr (list): The list to be searched.
    - target: The element to be found.

    Returns:
    - If the target is found, return the index of the target in the list.
    - If the target is not found, return -1.
    """
    for i in range(len(arr)):
        if arr[i] == target:
            return i  # Element found, return its index
    return -1  # Element not found

# Example usage:
my_list = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
target_element = 6

result = linear_search(my_list, target_element)

if result != -1:
    print(f"Element {target_element} found at index {result}.")
else:
    print(f"Element {target_element} not found in the list.")

In this example, the linear_search function takes a list (arr) and a target element (target) as parameters. It iterates through the list using a for loop and compares each element with the target. If the target is found, it returns the index of the target; otherwise, it returns -1 to indicate that the target is not in the list.

Copy The Code & Try With Live Editor

#2 Linear Search implement in Java

Code - Java Programming

public class LinearSearch {

    // Function to perform linear search
    static int linearSearch(int[] arr, int target) {
        // Iterate through the array
        for (int i = 0; i  <  arr.length; i++) {
            // Check if the current element is equal to the target
            if (arr[i] == target) {
                // Return the index if a match is found
                return i;
            }
        }
        // Return -1 if the target is not found in the array
        return -1;
    }

    public static void main(String[] args) {
        // Example usage
        int[] array = {4, 2, 7, 1, 9, 5};
        int targetElement = 7;

        // Perform linear search
        int resultIndex = linearSearch(array, targetElement);

        // Display the result
        if (resultIndex != -1) {
            System.out.println("Element " + targetElement + " found at index " + resultIndex);
        } else {
            System.out.println("Element " + targetElement + " not found in the array");
        }
    }
}

In this example, the linearSearch function takes an array and a target value as input and iterates through the array elements using a for loop. If it finds an element that matches the target value, it returns the index of that element. If no match is found, it returns -1. The main method demonstrates the usage of the linearSearch function with a sample array and target element.

Copy The Code & Try With Live Editor

#3 Linear Search implement in C

Code - C Programming

#include <stdio.h>

// Function to perform linear search
int linearSearch(int arr[], int n, int target) {
    for (int i = 0; i  <  n; i++) {
        if (arr[i] == target) {
            return i; // Return the index if target is found
        }
    }
    return -1; // Return -1 if target is not found in the array
}

int main() {
    int arr[] = {2, 5, 8, 12, 16, 23, 38, 42, 50};
    int n = sizeof(arr) / sizeof(arr[0]);

    int target = 16;

    int result = linearSearch(arr, n, target);

    if (result != -1) {
        printf("Element %d found at index %d\n", target, result);
    } else {
        printf("Element %d not found in the array\n", target);
    }

    return 0;
}

In this example, the linearSearch function takes an array arr, its size n, and a target value to search for. It iterates through the array elements using a for loop and returns the index if the target value is found, or -1 if the target value is not present in the array. In the main function, an example array arr is defined, and the linearSearch function is called to search for the target value (in this case, 16). The result is then printed to the console.

Copy The Code & Try With Live Editor

#4 Linear Search implement in C++

Code - C++ Programming

#include <iostream>

// Function to perform linear search
int linearSearch(int arr[], int n, int target) {
    for (int i = 0; i  <  n; ++i) {
        if (arr[i] == target) {
            return i; // Return the index if the target is found
        }
    }
    return -1; // Return -1 if the target is not found
}

int main() {
    const int size = 10;
    int array[size] = {5, 2, 9, 1, 5, 6, 3, 8, 7, 4};
    
    int target;
    std::cout << "Enter the element to search: ";
    std::cin >> target;

    int index = linearSearch(array, size, target);

    if (index != -1) {
        std::cout << "Element found at index " << index << std::endl;
    } else {
        std::cout << "Element not found in the array." << std::endl;
    }

    return 0;
}
 < /code>

In this example, the linearSearch function takes an array (arr), its size (n), and the target element to search for (target). It iterates through the array elements using a for loop and checks if the current element is equal to the target. If a match is found, it returns the index of the target; otherwise, it returns -1 to indicate that the target is not present in the array. The main function demonstrates the usage of this linear search function with a sample array.

Copy The Code & Try With Live Editor
Advertisements

Demonstration


Linear Search Data Structure and Algorithm