Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Linear Search in java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Linear Search in Java

class LinearSearch {
public static int linearSearch(int array[], int x) {
int n = array.length;

// Going through array sequencially
for (int i = 0; i < n; i++) {
if (array[i] == x)
return i;
}
return -1;
}

public static void main(String args[]) {
int array[] = { 2, 4, 0, 1, 9 };
int x = 1;

int result = linearSearch(array, x);

if (result == -1)
System.out.print("Element not found");
else
System.out.print("Element found at index: " + result);
}
}