linear searching in java code example
Example: Linear search in java
public class LinearSearchExample
{
static int searchNumber(int[] arrLinear, int key)
{
int num = arrLinear.length;
for(int a = 0; a < num; a++)
{
if(arrLinear[a] == key)
return a;
}
return -1;
}
public static void main(String[] args)
{
int[] arrLinear = {15, 25, 35, 55, 75, 95};
int key = 55;
int output = searchNumber(arrLinear, key);
if(output == -1)
{
System.out.println("Sorry!!Element is not present");
}
else
{
System.out.println("Element is present at index " + output);
}
}
}