8000 Merge pull request #83 from nishantmovaliya/master · AllAlgorithms/java@c856a8e · GitHub
[go: up one dir, main page]

Skip to content

Commit c856a8e

Browse files
authored
Merge pull request #83 from nishantmovaliya/master
Update LinearSearch.java
2 parents 6ad1141 + 0411985 commit c856a8e

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

algorithms/searches/LinearSearch.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
class LinearSearch {
66
// This function returns index of element x in arr[]
7-
static int search(int arr[], int n, int x) {
7+
int search(int arr[], int n, int x) {
88
for (int i = 0; i < n; i++) {
99
// Return the index of the element if the element
1010
// is found
@@ -16,4 +16,18 @@ static int search(int arr[], int n, int x) {
1616
return -1;
1717
}
1818

19+
20+
21+
public static void main(String args[]) {
22+
LinearSearch ob = new LinearSearch();
23+
int arr[] = { 2, 3, 4, 10, 40 };
24+
int n = arr.length;
25+
int x = 10;
26+
int result = ob.search(arr,n, x);
27+
28+
if (result == -1)
29+
System.out.println("Element not present");
30+
else
31+
System.out.println("Element found at " + "index " + result);
32+
}
1933
}

0 commit comments

Comments
 (0)
0