1public static int linear(int[] arr, int keyValue)
2{
3 int pos = -1;
4
5 for (int i = 0; i < arr.length; i++)
6 {
7 if (arr[i] == keyValue)
8 {
9 pos = i;
10 break;
11 }
12 }
13 return pos;
14}
1public class LinearSearchExample
2{
3 // here function returns index of element x in arrLinear
4 static int searchNumber(int[] arrLinear, int key)
5 {
6 int num = arrLinear.length;
7 for(int a = 0; a < num; a++)
8 {
9 // here we are returning the index of the element if found
10 if(arrLinear[a] == key)
11 return a;
12 }
13 // here we are returning -1 if element is not found
14 return -1;
15 }
16 public static void main(String[] args)
17 {
18 int[] arrLinear = {15, 25, 35, 55, 75, 95};
19 int key = 55;
20 int output = searchNumber(arrLinear, key);
21 if(output == -1)
22 {
23 System.out.println("Sorry!!Element is not present");
24 }
25 else
26 {
27 System.out.println("Element is present at index " + output);
28 }
29 }
30}