0
0
RubyProgramBeginner · 2 min read

Ruby Program for Linear Search with Example and Explanation

A Ruby program for linear search uses a loop to check each element in an array with each_with_index and returns the index if the element matches, like array.each_with_index { |item, index| return index if item == target }.
📋

Examples

Input[3, 5, 7, 9], target = 7
Output2
Input[10, 20, 30, 40], target = 25
Output-1
Input[], target = 1
Output-1
🧠

How to Think About It

To do a linear search, start from the first item in the list and check if it matches the target. If it does, return its position. If not, move to the next item and repeat until you find the target or reach the end of the list.
📐

Algorithm

1
Get the list of numbers and the target number to find.
2
Start from the first element in the list.
3
Compare the current element with the target.
4
If they match, return the current position (index).
5
If not, move to the next element and repeat.
6
If the end of the list is reached without a match, return -1.
💻

Code

ruby
def linear_search(array, target)
  array.each_with_index do |item, index|
    return index if item == target
  end
  -1
end

# Example usage
arr = [3, 5, 7, 9]
target = 7
puts linear_search(arr, target)
Output
2
🔍

Dry Run

Let's trace the search for target 7 in the array [3, 5, 7, 9].

1

Check first element

item = 3, target = 7, not equal, continue

2

Check second element

item = 5, target = 7, not equal, continue

3

Check third element

item = 7, target = 7, equal, return index 2

IndexItemCompare with TargetMatch?
033 == 7No
155 == 7No
277 == 7Yes
💡

Why This Works

Step 1: Loop through each element

The code uses each_with_index to check every element and keep track of its position.

Step 2: Compare element with target

Inside the loop, it compares the current element with the target using ==.

Step 3: Return index if found

If the element matches the target, the function immediately returns the current index.

Step 4: Return -1 if not found

If the loop finishes without finding the target, the function returns -1 to show the target is not in the array.

🔄

Alternative Approaches

Using a for loop
ruby
def linear_search(array, target)
  for i in 0...array.length
    return i if array[i] == target
  end
  -1
end

puts linear_search([3, 5, 7, 9], 7)
This uses a classic for loop instead of <code>each_with_index</code>, which some beginners find easier to understand.
Using Array#index method
ruby
def linear_search(array, target)
  array.index(target) || -1
end

puts linear_search([3, 5, 7, 9], 7)
Ruby's built-in <code>index</code> method does the linear search internally and returns the index or nil; we return -1 if nil.

Complexity: O(n) time, O(1) space

Time Complexity

The search checks each element once until it finds the target or reaches the end, so it takes linear time proportional to the array size.

Space Complexity

The search uses only a few variables and no extra arrays, so it uses constant extra space.

Which Approach is Fastest?

All approaches have the same time complexity O(n), but using Ruby's built-in index method is simplest and often fastest due to internal optimizations.

ApproachTimeSpaceBest For
each_with_index loopO(n)O(1)Clear code with index access
for loopO(n)O(1)Beginners familiar with classic loops
Array#index methodO(n)O(1)Shortest code and built-in optimization
💡
Use Ruby's each_with_index to get both element and position easily during linear search.
⚠️
Forgetting to return -1 when the target is not found, which can cause unexpected nil results.