-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinearsearch.R
More file actions
37 lines (29 loc) · 1.06 KB
/
linearsearch.R
File metadata and controls
37 lines (29 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#' ---
#' title: "Linear Search in R"
#' author: "Rahul Goswami"
#' ---
#'Linear Search is a searching algorithm that is used to find an element in a sorted array.
#'The basic idea of linear search is to search for an element in a sorted array by comparing it with each element of the array.
#'The average case time complexity of linear search is O(n)
#'The worst case time complexity of linear search is O(n)
#'The best case time complexity of linear search is O(1)
#'The space complexity of linear search is O(1) (in-place)
#''
#' ### Algorithm:
#' 1. Compare the element to be searched with each element of the array
#' 2. If the element is found, return the index of the element
#' defintion of linear search
#' @param vec Vector to be searched
#' @param element Element to be searched
#' @return Index of the element
linear.search <- function(vec, element){
for(i in 1:length(vec)){
if(vec[i] == element){
return(i)
}
}
return("Not Found")
}
#' ### Example
sorted_vec <- c(0,1,2,3,4,5,6,7,8,9)
linear.search(sorted_vec, 5)