Skip to content

Commit

Permalink
Merge pull request #169 from MSufiyanAG/master
Browse files Browse the repository at this point in the history
linear search in java #12
  • Loading branch information
Ricardo Prins authored Jun 29, 2020
2 parents 731d366 + 6b9cbf5 commit c759695
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Java/Linear_search.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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:
3
*/
1 change: 1 addition & 0 deletions Java/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* [Segment Tree](SegmentTree.java)

## Searching
* [Linear Search](Linear_search.java)
* [Binary Search](Binary_search.java)

## Sorting
Expand Down

0 comments on commit c759695

Please sign in to comment.