## 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.
"""
for i in range(len(arr)):
if arr[i] == target:
return i  # Element found, return its index

# 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:
``````

Copy The Code &

### #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 {
}
}
}
``````

Copy The Code &

### #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 {
}

return 0;
}
``````

Copy The Code &

### #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
}
}
}

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 {
``` 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 & ```
``` Advertisements (adsbygoogle = window.adsbygoogle || []).push({}); Demonstration Linear Search Data Structure and Algorithm ```
``` ```
``` ```