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, butarr=5 3 8 1is not (this only setsarrto5and tries to run3,8,1as commands). - When accessing array elements, use
${arr[@]}to get all items—$arronly 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 lastielements 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 writeif [ ${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:
Skipping the temp variable will set both elements to the same value, breaking the sort.temp=${arr[j]} arr[j]=${arr[j+1]} arr[j+1]=$temp
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/bashat the top of your script, and run it withbash your_script.shinstead ofsh 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
相关产品推荐
相关产品推荐

