Visualization
For example, let's search the number 5 in the following sorted int array.
Start a for loop and set i from 0 to the array's length - 1 with increment 1.
i = 0;
1 2 5 7 9 12
The number at index i = 0 is not the number we are looking for, increase i by 1
i = 1;
1 2 5 7 9 12
The number at index i = 1 is not the number we are looking for, increase i by 1
i = 2;
1 2 5 7 9 12
The number at index i = 2 is the number we are looking for, return i.
public int sequentialSearch(int[] arr, int needle)
{
for(int i=0; i < arr.length; i++)
{
if(arr[i] == needle)
{
return i;
}
}
return -1;
}