Sequential search is defined as a technique in which the elements from start to end are compared with the desired element. Sequential searching ends when the desired element gets matched with the elements that are available in the list.
In this article we will discuss the following in detail:
- Algorithm of sequential search.
- Pictorial Example.
- Code implementation.
How to Perform/Execute Sequential Search in Java?
To perform a sequential search in Java, the desired element is compared with every element in the list linearly. This search starts from the first element in the list until the match is found till the list ends. If the desired element is found the results are printed accordingly otherwise the output becomes “There is no matching element in the list”. Therefore, this search is also referred to as Linear Search.
Let us take a look into the algorithm of Sequential search.
Algorithm
The algorithm below will help to understand sequential searching in more detail:
1- Intilialize the array as x=0
2- If x>0(Print no element found).
3- If array[x]= desired element(print element found, Step number 7)
4- Now, set the value of x as x=x+1.
5- Move back to step 2.
6- Now, Print the desired element that is found at the specific index of the list and move to step number 8.
7- Print “ The element is not found”.
8- Stop.
Pictorial Example
Let us consider the example below to fully grasp the concept of sequential search in Java.
The array is declared below:

Now, let us consider that the Key Value is 14. To find the key value, we will compare the desired value from elements in the list from start to end.
1- Comparing the first element which is 12.

The desired element or the key value is not matched with the element at index 0.
2- Now the iterator will move to the next element in the list which is 13.

The desired element or the key value is not matched with the element at index 1.
3- The key value is still not found, now the iterator will move to the next value in the list.

The key value here matches with the element in the list at the second index. Therefore, the iteration will stop here.
The algorithm and pictorial representation described above have made it quite clear to understand the concept of Sequential search in Java.
Let us move to code, which will help in a complete understanding of the sequential search.
Example 1:
The following example performs a sequential search to print the index number of the key matched.
public class sequentialsearch{
public static int searchlinear(int myarray[], int keyvalue){
for(int x=0;x<myarray.length;x++){ //If the element in the array has a key value return the element
if(myarray[x] == keyvalue){
return x;
}
} //If the value is not in the array return -1
return -1;
}
public static void main(String s1[]){ //Declare values in the array
int[] s= {12,13,14,17,19,20,30};
int keyvalue = 14; //Search for the key value in the array
int result = searchlinear(s, keyvalue);//If the key value is not in the array
if (result == -1)
System.out.print(keyvalue+ " is not present in array");
else
System.out.println(keyvalue+" is present at index number: "+searchlinear(s, keyvalue));
}
}
In the code above:
- A for loop is declared, followed by an if statement.
- If the array contains the key value then, the index that contains that value is returned.
- Otherwise, the iterator moves on each element linearly until the desired key is found.
- The output appears as the element is not present in the array if the desired key doesn’t get matched.
Output

The above output shows that the key value that was declared as 14 in the above code is present at index number 2 of the list.
Example 2:
The below-mentioned piece of code depicts a scenario where the key is not available in the array.
public class sequentialsearch{
public static int searchlinear(int myarray[], int keyvalue){
for(int x=0;x<myarray.length;x++){ //If the element in the array has a key value return the element
if(myarray[x] == keyvalue){
return x;
}
} //If the value is not in the array return -1
return -1;
}
public static void main(String s1[]){ //Declare values in the array
int[] s= {12,13,14,17,19,20,30};//10 is not present i the array
int keyvalue = 10; //Search for the key value in the array
int result = searchlinear(s, keyvalue);//If the key value is not in the array
if (result == -1)
System.out.print(keyvalue+ " is not present in array");
else
System.out.println(keyvalue+" is present at index number: "+searchlinear(s, keyvalue));
}
}
Output

The output above shows that the desired value which is 10, is not available in the list.
Example 3:
The below-mentioned piece of code asks for input from the user to perform sequential search:
//Import util.Scanner for user inputimport java. util.Scanner;public class user input {
private static Scanner user;
public static void main(String[] args) {
int x, y;
user = new Scanner(System.in);//User enters the size of array.
System.out.print("Enter size of Array = ");
x = user.nextInt();
int[] array = new int[x];//User enters the number of elements in the array.
System.out.format("Enter number of elements you want in Array = ",x);
for(y = 0; y < x; y++) {
array[y] = user.nextInt();
}//User enters the key value.
System.out.print("Enter keyvalue = ");
int digit = user.nextInt();
for(y = 0; y < array.length; y++)
{
if(array[y] == digit)
{//Index is printed for the value.
System.out.println("Index of " + digit + " is " + y);
break;
}
}
if(y == array.length)
{//If the number entered is not present in the array.
System.out.println("The number is not present in the Array");
}
}
}
The above code works in the same manner as the previous one. The difference in the above code occurs where the user has been asked for the input.
Output

The output shows that the key value is present at the second location.
Disadvantage
The main disadvantage of using sequential searches tend to be hectic for large arrays.
This sums up the process of performing sequential searches in Java.
Conclusion
To perform a sequential search in Java, the desired element is compared with every element in the list linearly. The main advantage of using sequential search is that the searching happens even when the elements in the array are not sorted. This write-up has effectively demonstrated the operating mechanism of sequential searches in Java. Therefore, sequential search helps in finding the key value efficiently.