0
0
Bash-scriptingHow-ToBeginner · 2 min read

Bash Script for Bubble Sort - Simple Sorting Example

You can write a bubble sort in Bash by looping through the array and swapping adjacent elements if they are in the wrong order, like this: for ((i=0; i arr[j+1] )); then temp=${arr[j]}; arr[j]=${arr[j+1]}; arr[j+1]=$temp; fi; done; done.
📋

Examples

Input3 1 2
OutputSorted array: 1 2 3
Input5 4 3 2 1
OutputSorted array: 1 2 3 4 5
Input7 7 7
OutputSorted array: 7 7 7
🧠

How to Think About It

Bubble sort works by repeatedly stepping through the list, comparing each pair of adjacent items and swapping them if they are in the wrong order. This process repeats until no swaps are needed, meaning the list is sorted.
📐

Algorithm

1
Get the list of numbers to sort.
2
Repeat for each element in the list except the last:
3
Compare each pair of adjacent elements.
4
Swap them if the first is greater than the second.
5
Repeat until no swaps occur in a full pass.
6
Return the sorted list.
💻

Code

bash
#!/bin/bash
arr=($@)
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

echo "Sorted array: ${arr[*]}"
Output
Sorted array: 1 2 3
🔍

Dry Run

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

1

Initial array

arr = [3, 1, 2]

2

First pass comparisons and swaps

Compare 3 and 1: swap -> arr = [1, 3, 2] Compare 3 and 2: swap -> arr = [1, 2, 3]

3

Second pass

Compare 1 and 2: no swap Compare 2 and 3: no swap No swaps, sorting done.

PassArray State
11 3 2
11 2 3
21 2 3
💡

Why This Works

Step 1: Compare adjacent elements

The code uses if (( arr[j] > arr[j+1] )) to check if two neighbors are out of order.

Step 2: Swap elements

If the first is greater, it swaps them using a temporary variable to reorder the array.

Step 3: Repeat passes

The nested loops repeat this process, pushing the largest unsorted element to the end each pass.

🔄

Alternative Approaches

Using while loop with swap flag
bash
#!/bin/bash
arr=($@)
n=${#arr[@]}
swapped=1
while (( swapped )); do
  swapped=0
  for ((i=0; i<n-1; i++)); do
    if (( arr[i] > arr[i+1] )); then
      temp=${arr[i]}
      arr[i]=${arr[i+1]}
      arr[i+1]=$temp
      swapped=1
    fi
  done
done

echo "Sorted array: ${arr[*]}"
This approach stops early if no swaps happen, improving efficiency on nearly sorted arrays.
Using external sort command
bash
#!/bin/bash
arr=($@)
sorted=($(printf "%s\n" "${arr[@]}" | sort -n))
echo "Sorted array: ${sorted[*]}"
This uses Bash's external sort command for simplicity and speed but is not a manual bubble sort.

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

Time Complexity

Bubble sort uses nested loops over the array, resulting in O(n^2) time because each element is compared multiple times.

Space Complexity

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

Which Approach is Fastest?

Using a swap flag to stop early can improve average performance, but external sort commands are faster for large data.

ApproachTimeSpaceBest For
Basic Bubble SortO(n^2)O(1)Small arrays, learning
Bubble Sort with Swap FlagO(n) best, O(n^2) worstO(1)Nearly sorted arrays
External sort commandO(n log n)O(n)Large arrays, practical use
💡
Use a swap flag to stop early if the array is already sorted to save time.
⚠️
Forgetting to update the array elements correctly during swap causes wrong sorting results.