Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

linear_search.r #106

Merged
merged 2 commits into from
Oct 7, 2023
Merged
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
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")
}