The Linear Search Algorithm

The linear search algorithm is one of the most simple algorithms. This is a pseudo code:

Procedure: Linear-Search(a,x)
a: the array to search in
x: the value we are searching for in a
The index position where a[i] == x or -1 if not found
1) Set result to -1
2) For each index i going from 1 to n, in order
if a[i] = x, then set result to the value of i
3) Return the value of result as the output.

The code in Java:

public static void main(String[] args) {
  int[] a = {1,2,3,4,5};
  int x = 5;
  System.out.println(linearSearch(a, x));
public static int linearSearch(int[] a, int x) {
  for (int i = 0; i < a.length; i++) {
    if (a[i] == x) return i;
  return -1;