0
0
Bash-scriptingHow-ToBeginner · 2 min read

Bash Script to Find Smallest Element in Array

Use a loop to compare elements and track the smallest value like this: smallest=${array[0]}; for i in "${array[@]}"; do if (( i < smallest )); then smallest=$i; fi; done; echo $smallest.
📋

Examples

Inputarray=(3 1 4 1 5)
Output1
Inputarray=(10 20 30 5 15)
Output5
Inputarray=(7)
Output7
🧠

How to Think About It

To find the smallest element, start by assuming the first element is the smallest. Then check each element one by one. If you find a smaller element, update your smallest value. At the end, the smallest value is the smallest element in the array.
📐

Algorithm

1
Set the first element as the smallest value.
2
Go through each element in the array.
3
Compare the current element with the smallest value.
4
If the current element is smaller, update the smallest value.
5
After checking all elements, return the smallest value.
💻

Code

bash
array=(3 1 4 1 5)
smallest=${array[0]}
for i in "${array[@]}"; do
  if (( i < smallest )); then
    smallest=$i
  fi
done

echo "$smallest"
Output
1
🔍

Dry Run

Let's trace array=(3 1 4 1 5) through the code

1

Initialize smallest

smallest=3 (first element)

2

Compare with 3

3 < 3? No, smallest stays 3

3

Compare with 1

1 < 3? Yes, smallest=1

4

Compare with 4

4 < 1? No, smallest stays 1

5

Compare with 1

1 < 1? No, smallest stays 1

6

Compare with 5

5 < 1? No, smallest stays 1

ElementSmallest So Far
33
11
41
11
51
💡

Why This Works

Step 1: Start with first element

We set smallest to the first element because we need a starting point to compare others.

Step 2: Loop through array

We check each element using a for loop to compare it with the current smallest.

Step 3: Update smallest if needed

If an element is less than smallest, we update smallest to that element.

Step 4: Print the result

After the loop ends, smallest holds the smallest element, which we print.

🔄

Alternative Approaches

Using sort command
bash
array=(3 1 4 1 5)
echo "${array[@]}" | tr ' ' '\n' | sort -n | head -1
This uses external commands and is easy but slower for large arrays.
Using while loop with index
bash
array=(3 1 4 1 5)
smallest=${array[0]}
i=1
while [ $i -lt ${#array[@]} ]; do
  if (( array[i] < smallest )); then
    smallest=${array[i]}
  fi
  ((i++))
done

echo "$smallest"
Uses index-based loop instead of for-each; more manual but similar logic.

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

Time Complexity

The script checks each element once, so time grows linearly with array size.

Space Complexity

Only a few variables are used, so space stays constant regardless of array size.

Which Approach is Fastest?

The loop method is fastest for large arrays because it avoids external commands like sort.

ApproachTimeSpaceBest For
Loop with comparisonO(n)O(1)Large arrays, efficient
Sort and pick firstO(n log n)O(n)Small arrays, simple code
While loop with indexO(n)O(1)Manual control, similar to loop
💡
Always initialize smallest with the first element before looping.
⚠️
Not initializing smallest before the loop causes wrong or empty results.