Bubble Sort Example Using Bash Script

Learn how to implement a Bubble Sort algorithm using Bash scripting. Bubble Sort is a simple sorting technique that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. This article provides a complete example, including a script, sample data, and an explanation of the logic.

Prerequisites

  • Basic knowledge of Bash scripting
  • Understanding of loops (e.g., for and while)
  • Familiarity with arrays in Bash
  • A Linux or macOS environment with Bash installed

Sample Data

The script will sort the following array of integers:

data=(5 3 8 6 2 7 4 1)

DID YOU KNOW?

Bubble Sort gets its name from the way larger elements “bubble” to the top of the list with each iteration.

The Script

This Bash script implements the Bubble Sort algorithm:

#!/bin/bash

# Sample data
data=(5 3 8 6 2 7 4 1)

# Bubble Sort implementation
n=${#data[@]}
echo "Original array: ${data[@]}"

for ((i = 0; i < n; i++)); do
    for ((j = 0; j < n - i - 1; j++)); do if ((data[j] > data[j + 1])); then
            # Swap elements
            temp=${data[j]}
            data[j]=${data[j + 1]}
            data[j + 1]=$temp
        fi
    done
done

echo "Sorted array: ${data[@]}"

Step-by-Step Explanation

NOTE!

This script uses a nested loop to compare and swap adjacent elements, iterating until the entire array is sorted.

Here’s how the script works:

  1. Initialize the Array: Define the sample data as a Bash array.
  2. Determine the Array Size: Use ${#data[@]} to get the number of elements in the array.
  3. Outer Loop: Iterate over the array multiple times, reducing the range with each pass.
  4. Inner Loop: Compare adjacent elements and swap them if the first is greater than the second.
  5. Display Results: Print the sorted array once all iterations are complete.
Bubble Sort Example Using Bash Script Execution
Bubble Sort Example Using Bash Script Execution

How to Run the Script

Follow these steps to execute the script:

  1. Save the script to a file, e.g., bubble_sort.sh.
  2. Make the script executable by running chmod +x bubble_sort.sh.
  3. Run the script using ./bubble_sort.sh.

Conclusion

The Bubble Sort algorithm is a straightforward sorting technique that is easy to implement in Bash. While it is not the most efficient for large datasets, it serves as an excellent introduction to sorting algorithms and array manipulation in Bash scripting.

FAQ

  1. Is Bubble Sort efficient for large datasets?

    No, Bubble Sort has a time complexity of O(n^2) and is inefficient for large datasets compared to other algorithms like Quick Sort or Merge Sort.

  2. Can this script handle floating-point numbers?

    No, Bash only supports integer arithmetic natively. Use a more advanced language like Python if you need to handle floats.

  3. What happens if the array is already sorted?

    The script will still iterate through the loops, but no swaps will occur, making it less efficient.

  4. Can I use this script for strings?

    Yes, but you need to modify the comparison logic to use string comparison operators.

Troubleshooting

  • Error: Permission denied
    Fix: Ensure the script is executable by running chmod +x bubble_sort.sh.
  • Error: Command not found
    Fix: Verify that Bash is installed and the script is being executed with the correct path.
  • Issue: Incorrect sorting of strings
    Fix: Use proper string comparison operators like [[ "${data[j]}" > "${data[j+1]}" ]].