Skip to content

Commit

Permalink
Merge branch 'master' into paras-2407-patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
paras-2407 authored Oct 9, 2023
2 parents 199a2e4 + 4f35b31 commit 4743dba
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions searches/linear_search.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
linear_search<-function(vector, search_value){ #made a function named linear_search having two parameters that are an array and a value to be searched
for(i in 1:length(vector)){
if(vector[i]==search_value){ #comparing each value of array with the value to be searched
return (i)
}
}
return (-1)
}

user_vec<- c(10,20,30,40,50,60) #input array (hard code)
user_val<-30 #input value to be searched (hard code)

result<-linear_search(user_vec,user_val) #linear_seach function calling

if(result!=-1){
cat("Searched value", user_val, "found at index", result-1) #displaying the index at which value is found (if any)
}else{
cat("Searched value does not exist in array")
}

0 comments on commit 4743dba

Please sign in to comment.