# Linear Search Java

Hey!! Guys, Welcome to flower brackets blog, how are you doing….. today you will learn linear search java. Let us see what linear search in java means??

#### What is linear search?

Basically linear search or sequential search is simple and executed to check if an element exists in the given array.

Then search element is compared with every element in an array. The search continues until the element is found else the array does not contain the element.

Also Read – Binary Search Program Java

For example, let’s say, we have a key, that is, the number we are going to search in the array. Next we see if any index of the array is equal to the key.

If it is true we increment the count.

Imagine we have an array,

Now, if key is 2 the system will search from the zeroth index. Here 2 is not equal to 1 so it goes to the next number.

If the key element is found, it will return the index position of the array element else it will return minus one.

Now 2 is equal to 2, next the counter will increment 2 by 1. Now 2 is not equal to 3. At this stage system has found the number 2, one time.

Let’s see linear search algorithm with an example,

#### Example: Linear Search Java

```import java.util.Scanner;

public class LinearSearchInJava
{
public static void main(String[] args)
{
int a;
int b;
int search;
int array[];

Scanner sc = new Scanner(System.in);
System.out.println("Please enter number of elements : ");
b = sc.nextInt();
array = new int[b];

System.out.println("Please enter " + b + " integers : ");

for(a = 0;a < b;a++)
array[a] = sc.nextInt();

System.out.println("Please enter value to find : ");
search = sc.nextInt();

for(a = 0; a < b; a++)
{
if(array[a] == search)
{
System.out.println(search + " is present at " + (a + 1) + " location.");
break;
}
}
if(a == b)
System.out.println(search + " is not present in an array.");
}
}

```

Output:

Various methods : linear search in java

Method 1 : Here we will learn linear search in java using method,

```public class SearchJava
{
public static int sequentialSearch(int arrNumber[], int key)
{
int range = arrNumber.length;
for(int a = 0; a < range; a++)
{
if(arrNumber[a] == key)
{
return a;
}
}
return -1;
}

public static void main(String[] args)
{
int arrOne[] = {11, 75, 32, 36, 48, 28, 27, 63};
int searchValue = 32;
System.out.println("Sequential search : Key value - " + searchValue + ", found at index : " + sequentialSearch(arrOne, searchValue));
}
}

```

Output:

Sequential search : Key value – 32, found at index : 2

Method 2 : Here we will learn linear search algorithm java by another way,

```import java.util.Scanner;

public class LinearSearchJava
{
public static int linearSearch(int arrNum[], int key)
{
for(int x = 0; x < arrNum.length; x++)
{
if(arrNum[x] == key)
{
return x + 1;
}
}
return -1;
}

public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Linear Search Algorithm Java\n");
System.out.println("Please enter size of array : ");
int num = sc.nextInt();
int arrNumber[] = new int[num];

System.out.println("Please enter " + num + " elements : ");
for(int x = 0; x < num; x++)
{
arrNumber[x] = sc.nextInt();
}

System.out.println("Please enter key element to find : ");
int keyValue = sc.nextInt();
int position = linearSearch(arrNumber, keyValue);
if(position == -1)
{
}
else
{
System.out.println("Key element found at position : " + position);
}
sc.close();
}
}

```

Output:

Linear Search Algorithm Java

Please enter size of array : 5
Please enter 5 elements : 2 4 6 8 10
Please enter key element to find : 10
Key element found at position : 5

#### Complexity of linear search

Let us discuss about the complexity of linear search. Suppose I am going to find key element 2 in an array which is,

This is a five element array. I am going to find out or search number / key element 2 in this particular array.

It might be possible that with this array there may be two possible conditions which is, one is best case then the second is worst case.

In the best case scenario it might be possible that whichever element we are searching in an array might exist in the very first position of an array.

So, if you find out key element 2 at the very first position, at that time the time complexity will be at the order of O(1).

Because it took only 1 second just to find out key element 2 in (above) an array.

But, in case if you don’t find out key element 2 in an array throughout after traversing the array. That means, that we are traversing 1, 2, 3, 4, 5 all the five elements and still the search is unsuccessful.

In that case searching time will be at the order of O(n). It might also be possible that key element 2 maybe occurring in another position like this,

We cannot stop searching the element on this particular position. We first find out the element at the very first time.

Might be possible that this key element 2 might be occurring in other part of the array also. So we are searching like this,

2 will be compared with 1. If it is matched then ok. If it does not match then we will move to the next element.

Now, if the key element is matched then it will track the location and it will count it as 2. Then it will move to the next position, then to next and next…..

This is how we are working on linear search algorithm. But we need to search key element 2 throughout the array or traversing the whole array.

So if this (above) array of five elements and to find out key element 2 we have to compare with each and every element present in the array.

That means it is taking, say, five seconds of time.

In general we can say if we have “n” elements in an array to search an element in an array it will take order of n time, that is, O(n).

Because it is comparing single key element with each and every element in an array.

This is all about the analysis of linear search.

##### conclusion

That’s it guys. In this post you have learnt linear search algorithm in java with examples and explanation.

I hope you guys have understood linear search java. You can subscribe to my blog flower brackets if you haven’t already.