Linear Search

Sequential Search, also known as Linear Search, is a simple search algorithm primarily used to find a specified element within an unsorted data set. The basic idea of sequential search is to scan the data set element by element starting from the first one, continuing until the target element is found or the entire data set has been scanned.

In terms of implementation, the process begins by comparing each element in the data set with the target element one by one. If the target element is found, the index of that element is returned; if the target element is not found after traversing the entire data set, a special value (such as -1) is returned to indicate that the element was not found. The time complexity of sequential search is O(n), where n is the size of the data set. Therefore, it is less efficient for large data sets but remains a simple and effective search algorithm for small data sets.

Sequential search is suitable for scenarios involving small data sets or cases where the data set is already sorted but requires only a few lookup operations. In practical applications, sequential search is often used in conjunction with element deletion or insertion operations within a data set because these operations require checking whether an element exists first. If the data set is already sorted, more efficient algorithms such as Binary Search may be considered.

Code

::: code-tabs
@tab Java

public class SequentialSearch {
    public static int sequentialSearch(int[] arr, int target) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == target) {
                return i; // Return the index of the target element
            }
        }
        return -1; // The target element does not exist in the array
    }
    public static void main(String[] args) {
        int[] arr = {5, 8, 2, 10, 3};
        int target = 2;
        int index = sequentialSearch(arr, target);
        if (index != -1) {
            System.out.println("The index position of the target element " + target + " in the array is: " + index);
        } else {
            System.out.println("The target element " + target + " does not exist in the array");
        }
    }
}

@tab JavaScript

function sequentialSearch(arr, target) {
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] === target) {
      return i; // Return the index of the target element
    }
  }
  return -1; // The target element does not exist in the array
}
const arr = [5, 8, 2, 10, 3];
const target = 2;
const index = sequentialSearch(arr, target);
if (index !== -1) {
  console.log(`The index position of the target element ${target} in the array is: ${index}`);
} else {
  console.log(`The target element ${target} does not exist in the array`);
}

@tab Python

def sequential_search(lst, target):
    for i in range(len(lst)):
        if lst[i] == target:
            return i  # Return the index of the target element
    return -1  # The target element does not exist in the list
lst = [5, 8, 2, 10, 3]
target = 2
index = sequential_search(lst, target)
if index != -1:
    print(f"The index position of the target element {target} in the list is: {index}")
else:
    print(f"The target element {target} does not exist in the list")

@tab Go

package main
import "fmt"
func sequentialSearch(slice []int, target int) int {
    for i, num := range slice {
        if num == target {
            return i // Return the index of the target element
        }
    }
    return -1 // The target element does not exist in the slice
}
func main() {
    slice := []int{5, 8, 2, 10, 3}
    target := 2
    index := sequentialSearch(slice, target)
    if index != -1 {
        fmt.Printf("The index position of the target element %d in the slice is: %d\n", target, index)
    } else {
        fmt.Printf("The target element %d does not exist in the slice\n", target)
    }
}

@tab C

#include <stdio.h>
int sequentialSearch(int arr[], int size, int target) {
    for (int i = 0; i < size; i++) {
        if (arr[i] == target) {
            return i; // Return the index of the target element
        }
    }
    return -1; // The target element does not exist in the array
}
int main() {
    int arr[] = {5, 8, 2, 10, 3};
    int size = sizeof(arr) / sizeof(arr[0]);
    int target = 2;
    int index = sequentialSearch(arr, size, target);
    if (index != -1) {
        printf("The index position of the target element %d in the array is: %d\n", target, index);
    } else {
        printf("The target element %d does not exist in the array\n", target);
    }
    return 0;
}

@tab C++

#include <iostream>
using namespace std;
int sequentialSearch(int arr[], int size, int target) {
    for (int i = 0; i < size; i++) {
        if (arr[i] == target) {
            return i; // Return the index of the target element
        }
    }
    return -1; // The target element does not exist in the array
}
int main() {
    int arr[] = {5, 8, 2, 10, 3};
    int size = sizeof(arr) / sizeof(arr[0]);
    int target = 2;
    int index = sequentialSearch(arr, size, target);
    if (index != -1) {
        cout << "The index position of the target element " << target << " in the array is: " << index << endl;
    } else {
        cout << "The target element " << target << " does not exist in the array" << endl;
    }
    return 0;
}

@tab PHP

<?php
function sequentialSearch($arr, $target) {
    $length = count($arr);
    for ($i = 0; $i < $length; $i++) {
        if ($arr[$i] === $target) {
            return $i; // Return the index of the target element
        }
    }
    return -1; // The target element does not exist in the array
}
$arr = [5, 8, 2, 10, 3];
$target = 2;
$index = sequentialSearch($arr, $target);
if ($index !== -1) {
    echo "The index position of the target element $target in the array is: $index";
} else {
    echo "The target element $target does not exist in the array";
}
?>

:::