0
0
Bash-scriptingHow-ToBeginner · 2 min read

Bash Script for Selection Sort with Example and Explanation

You can perform selection sort in Bash by looping through the array, finding the smallest element with nested loops, and swapping it with the current position using code like: for ((i=0; i.
📋

Examples

Input3 1 2
Output1 2 3
Input5 4 3 2 1
Output1 2 3 4 5
Input10 10 5 7
Output5 7 10 10
🧠

How to Think About It

To sort an array using selection sort, think of repeatedly picking the smallest number from the unsorted part and swapping it with the first unsorted element. You move through the array from start to end, shrinking the unsorted section each time until the whole array is sorted.
📐

Algorithm

1
Get the array input and its length.
2
Start from the first element and assume it is the minimum.
3
Compare this element with the rest of the array to find the actual minimum.
4
Swap the found minimum element with the current element.
5
Move to the next element and repeat until the array is sorted.
6
Print the sorted array.
💻

Code

bash
arr=(3 1 2 5 4)
n=${#arr[@]}
for ((i=0; i<n-1; i++)); do
  min=$i
  for ((j=i+1; j<n; j++)); do
    if (( arr[j] < arr[min] )); then
      min=$j
    fi
  done
  temp=${arr[i]}
  arr[i]=${arr[min]}
  arr[min]=$temp
done
printf "%s " "${arr[@]}"
Output
1 2 3 4 5
🔍

Dry Run

Let's trace the array [3, 1, 2] through the selection sort code.

1

Initial array

arr = [3, 1, 2]

2

First outer loop (i=0)

min = 0 (value 3), check j=1 (1 < 3) → min=1, check j=2 (2 < 1) no, swap arr[0] and arr[1] → arr = [1, 3, 2]

3

Second outer loop (i=1)

min = 1 (value 3), check j=2 (2 < 3) → min=2, swap arr[1] and arr[2] → arr = [1, 2, 3]

IterationArray State
Start[3, 1, 2]
i=0[1, 3, 2]
i=1[1, 2, 3]
💡

Why This Works

Step 1: Find minimum in unsorted part

The inner loop uses if (( arr[j] < arr[min] )) to find the smallest element from the unsorted section.

Step 2: Swap minimum with current

After finding the minimum, the script swaps it with the element at the current position using temporary storage.

Step 3: Repeat until sorted

The outer loop moves forward, shrinking the unsorted section until the entire array is sorted.

🔄

Alternative Approaches

Bubble Sort
bash
arr=(3 1 2 5 4)
n=${#arr[@]}
for ((i=0; i<n-1; i++)); do
  for ((j=0; j<n-i-1; j++)); do
    if (( arr[j] > arr[j+1] )); then
      temp=${arr[j]}
      arr[j]=${arr[j+1]}
      arr[j+1]=$temp
    fi
  done
done
printf "%s " "${arr[@]}"
Bubble sort repeatedly swaps adjacent elements; simpler but usually slower than selection sort.
Using sort command
bash
arr=(3 1 2 5 4)
IFS=$'\n' sorted=($(sort -n <<<"${arr[*]}"))
printf "%s " "${sorted[@]}"
Using the built-in sort command is faster and simpler but relies on external tool, not pure Bash logic.

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

Time Complexity

Selection sort uses two nested loops each running up to n times, resulting in O(n^2) time.

Space Complexity

It sorts the array in place, so it only needs O(1) extra space for swapping.

Which Approach is Fastest?

Using the built-in sort command is fastest for large data, while selection sort is simple but slower.

ApproachTimeSpaceBest For
Selection SortO(n^2)O(1)Small arrays, learning sorting logic
Bubble SortO(n^2)O(1)Simple swaps, educational use
sort commandO(n log n)O(n)Large arrays, practical sorting
💡
Use array indexing and nested loops carefully to avoid off-by-one errors in Bash selection sort.
⚠️
Beginners often forget to swap elements correctly, causing the array to remain unsorted.