Skip to main content

Command Palette

Search for a command to run...

Linear Search in Java

Published
•1 min read•View as Markdown
K

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

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.

More from this blog

Untitled Publication

10 posts