×

Search anything:

#### Linear Search Algorithms Indian Technical Authorship Contest starts on 1st July 2023. Stay tuned.

R is an open-source programming language for statistical computing and graphics. It is widely used for Data Science.

1. Approach
2. R implementation for Single-threaded Linear Search
3. R implementation for Multi-threaded Linear Search
4. Run the code

## Approach

For Linear Search in R, we define a data array as:

``````data <- c(2, 5, 7, 1, 9, 3, 6, 8)
``````

and loop through it as:

``````for (i in seq_along(data)) {
# ...
}
``````

In the process of looping through each element, compare each element with target element for Linear Search.

### Approach for Multi-thread Linear Search

• Import the library future.apply
• Split data into smaller chunks
``````data_chunks <- future.apply::future_chunk(as.list(data), num_threads)
``````
• Create each thread for each chunk and apply Linear Search for that chunk of data
``````futures <- future.apply::future_lapply(data_chunks, linear_search_worker, search_key=search_key)
``````
``````results <- future.apply::future_get(futures)
``````
• Get the data for each thread and compute the final result accordingly
``````for (i in seq_along(results)) {
if (results[[i]] != -1) {
return((i - 1) * length(data_chunks[]) + results[[i]])
}
}
``````

## R implementation for Single-threaded Linear Search

Following is the single-threaded program in R for Linear Search:

``````# Define a function to perform linear search on a vector of integers.
linear_search <- function(data, search_key) {
# Loop through the vector and compare each element to the search key.
for (i in seq_along(data)) {
if (data[i] == search_key) {
# If the search key is found, return its index.
return(i)
}
}

return(-1)
}

# Define some sample data and a search key.
data <- c(2, 5, 7, 1, 9, 3, 6, 8)
search_key <- 3

# Call the linear search function with the sample data and search key.
result <- linear_search(data, search_key)

# Print the result of the linear search.
if (result == -1) {
} else {
cat("Found element", search_key, "at index", result, "\n")
}
``````

## R implementation for Multi-threaded Linear Search

Following is the multi-threaded program in R for Linear Search:

``````library(future.apply)

# Define a function to perform linear search on a subset of data.
linear_search_worker <- function(data, search_key) {
# Loop through the subset of data and compare each element to the search key.
for (i in seq_along(data)) {
if (data[i] == search_key) {
# If the search key is found, return its index.
return(i)
}
}

return(-1)
}

# Define a function to perform linear search with multiple threads.
linear_search_parallel <- function(data, search_key, num_threads) {
# Split the data into equal-sized chunks for each thread.

# Create a list of futures for each thread to perform linear search on each data chunk.
futures <- future.apply::future_lapply(data_chunks, linear_search_worker, search_key=search_key)

# Wait for all futures to complete and get their results.
results <- future.apply::future_get(futures)

# Loop through the results and return the index of the search key if found.
for (i in seq_along(results)) {
if (results[[i]] != -1) {
return((i - 1) * length(data_chunks[]) + results[[i]])
}
}

return(-1)
}

# Define some sample data and a search key.
data <- c(2, 5, 7, 1, 9, 3, 6, 8)
search_key <- 1

# Call the linear search function with multiple threads using the sample data and search key.

# Print the result of the linear search.
if (result == -1) {
} else {
cat("Found element", search_key, "at index", result, "\n")
}
``````

## Run the code

Save the code in a file named as "linearSearch.R" and use the following command to run it from R console:

``````source("linearSearch.R")
``````

If you are using an IDE like RStudio, you can load and run the code using "Source" button.

The output will be as follows:

``````Found element 1 at index 3
``````

With this article at OpenGenus, you must have the complete idea of how to implement Linear Search in R Programming Language using both single and multi-threads.

#### Rahul Reddy

M.Tech in Computer Science and Engineering from IIT Bhubaneswar | SDE Intern at Flipkart