Linear Search in Java

Kushagra SharmaKushagra Sharma
1 min read

Searching:

It is a process of finding a given value position in a list of values

Linear / Sequential Search:

It is a basic and simple search algorithm.

In a sequential search, we compare the target value with all the elements given in the list

eg:-

int[] arr = {0,1,2,3,4,5,6,7,8,9};

Check if input exists in arr.

In the above example, the input value is compared with all the elements in the array in a sequential/linear way.

Time complexity:

Best case: 0(1) --> constant

how many checks will the loop make in the best case?

The element will be found at 0 index and only one comparison will be made in the best case.

Worst case: 0(n)

The worst case here will be to go through every element and then it says element not found.

0
Subscribe to my newsletter

Read articles from Kushagra Sharma directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Kushagra Sharma
Kushagra Sharma

Hello, I am Kushagra, an undergraduate who is working on devloping new skills.