You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Shell脚本Bubble Sort无输出且无法排序问题求助

Troubleshooting Your Non-Working Bubble Sort Shell Script

Hey there! Let's figure out why your bubble sort shell script isn't doing its job—no errors, no output, no sorting? Total frustration, I’ve been there. Even if you’re sure your logic checks out, shell scripting has some sneaky pitfalls with arrays, loops, and variable handling that can derail things. Here are the most common issues to investigate:

1. Array Initialization & Reference Mistakes

  • In Bash, arrays need proper syntax: arr=(5 3 8 1) is correct, but arr=5 3 8 1 is not (this only sets arr to 5 and tries to run 3, 8, 1 as commands).
  • When accessing array elements, use ${arr[@]} to get all items—$arr only grabs the first element. If you’re looping with the wrong reference, your sort can’t even access most of the values.

2. Incorrect Loop Boundaries

Bubble sort relies on precise loop ranges:

  • The outer loop should start at the array length minus 1 and decrement (since each pass bubbles the largest unsorted element to the end). A correct outer loop looks like: for ((i=${#arr[@]}-1; i>0; i--))
  • The inner loop should run from 0 to i-1 (since the last i elements are already sorted). If you run the inner loop across the full array every time, you’ll waste cycles or fail to sort properly.

3. Wrong Comparison & Swap Logic

This is the most frequent gotcha:

  • Number comparison: Use arithmetic operators like -gt/-lt (in [ ]) or >/< inside (( )). If you write if [ ${arr[j]} > ${arr[j+1]} ], that’s not comparing numbers—it’s trying to redirect the contents of ${arr[j]} to a file named ${arr[j+1]}!
    Correct examples:
    if (( arr[j] > arr[j+1] ))
    # OR
    if [ ${arr[j]} -gt ${arr[j+1]} ]
    
  • Swapping elements: Don’t overwrite values directly—use a temporary variable:
    temp=${arr[j]}
    arr[j]=${arr[j+1]}
    arr[j+1]=$temp
    
    Skipping the temp variable will set both elements to the same value, breaking the sort.

4. Missing or Broken Output

If you see no output at all, double-check:

  • Did you forget to print the sorted array? Add echo "${arr[@]}" after the sort loops to display the result.
  • Are you running the script with a shell that suppresses output? Unlikely if there are no errors, but worth confirming.

5. Shell Compatibility Issues

  • Make sure you’re running the script with Bash, not Sh. Sh doesn’t support arrays or (( )) arithmetic expansion. Add #!/bin/bash at the top of your script, and run it with bash your_script.sh instead of sh your_script.sh.

Working Bubble Sort Example

Here’s a solid reference script to compare against yours:

#!/bin/bash

# Initialize array
arr=(9 2 5 1 7 3)
echo "Original array: ${arr[@]}"

# Get array length
len=${#arr[@]}

# Bubble sort implementation
for ((i=len-1; i>0; i--)); do
    for ((j=0; j<i; j++)); do
        if (( arr[j] > arr[j+1] )); then
            # Swap elements
            temp=${arr[j]}
            arr[j]=${arr[j+1]}
            arr[j+1]=$temp
        fi
    done
done

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

If you’re still stuck, share your actual script code—we can spot the specific issue faster!

内容的提问来源于stack exchange,提问作者John Shin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 09:14:53