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
31 changes: 31 additions & 0 deletions Java/Algorithms/LinearSearch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.company;

public class LinearSearch {
public static void main(String[] args) {
int[] nums ={23, 45, 1, 2, 8, 19, -3, 16, -11, 28};
int target = 19;
int ans = linearSearch(nums,target);
System.out.println(ans);
}

//search in the array : return the index of the item found
//otherwise if item not found return -1

static int linearSearch(int arr[], int target){
if(arr.length ==0){
return -1;
}

// run a for loop
for(int index =0; index< arr.length; index++){
//check for element at every index if it is equal to target
int element = arr[index];
if(element == target){
return index;
}
}
// This line will execute if none of the return statements above have executed
//Hence the target not found
return -1;
}
}