forked from Ayush7-BYTE/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Linear.java
40 lines (34 loc) · 832 Bytes
/
Linear.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Java Recursive Code For Linear Search
import java.io.*;
class Test {
static int arr[] = { 5, 15, 6, 9, 4 };
// Recursive Method to search key in the array
static int linearsearch(int arr[], int size, int key)
{
if (size == 0) {
return -1;
}
else if (arr[size - 1] == key) {
// Return the index of found key.
return size - 1;
}
else {
return linearsearch(arr, size - 1, key);
}
}
// Driver method
public static void main(String[] args)
{
int key = 4;
// Function call to find key
int index = linearsearch(arr, arr.length, key);
if (index != -1)
System.out.println(
"The element " + key + " is found at "
+ index + " index of the given array.");
else
System.out.println("The element " + key
+ " is not found.");
}
}
// This Code is submitted by Susobhan Akhuli