Skip to content

Commit

Permalink
Update Linear_search.java
Browse files Browse the repository at this point in the history
  • Loading branch information
MSufiyanAG authored Jun 29, 2020
1 parent b0814f0 commit 6b9cbf5
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions Java/Linear_search.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
public class Linear_search{
//It iterates through the array to find key .The average time complexity O(n).
static int linsearch(int[] a,int k){
for(int i=0;i<a.length;i++){
if(a[i]==k)
return i+1;//returns position
}
return -1;//returns -1 if not found
}
public static void main(String[] args){
int[] a={2,7,8,9,1,5,3,4};
int key=8;
System.out.println(linsearch(a,key));
}
public class Linear_search {
//It iterates through the array to find key .The average time complexity O(n).
static int linsearch(int[] a,int k,int length){
for(int i=0;i<length;i++){
if(a[i]==k)
return i+1;//returns position
}
return -1;//returns -1 if not found
}
public static void main(String[] args){
int[] a={2,7,8,9,1,5,3,4};
int key=8;
System.out.println(linsearch(a,key,8));
}
}
/*
output:
Expand Down

0 comments on commit 6b9cbf5

Please sign in to comment.