4

I have a problem regarding about the syntax on how to swap the element value in an array.

array="5 3 2 1 4"
echo "${array[*]}"

changed=1
while [ $changed != 0 ]
do
    changed=0
    for (( i=0 ; i<=${#array[@]}-1 ; i++ )) 
    do
        if [ ${array[$i]} -gt ${array[$i+1]} ]
            then
                tmp=${array[$i]}
                array[$i]=${array[$i+1]}
                array[$i+1]=$tmp
                changed=1
        fi
    done    
done

echo "Sorted array: "
echo "${array[*]}"

Edit:

Thanks for answering my question. I have changed the code, and now and it looks something like this.

But unfortunately there is still a problem.

It says:

jdoodle.sh: line 3: $'\r': command not found
jdoodle.sh: line 8: syntax error near unexpected token `$'\r''
jdoodle.sh: line 8: `    for ((i=0;i<=${#array[@]}-1;i++))
7
  • 1
    Please take a look: shellcheck.net Commented Dec 10, 2017 at 13:24
  • For arithmetic within a shell ((...integer math...)) is the preferred method, $((...integer math...)) for the result itself (let and expr work, but have limitations by comparison). Note: for (( i=0 ; i<${#array[@]}; i++ )) and for indexed arrays ${array[i]} is sufficient in bash. But for arithmetic within the index $((i+1)) is the best choice. Commented Dec 10, 2017 at 13:28
  • No spaces around = in assignments. Commented Dec 10, 2017 at 13:30
  • Thanks for answering guys, yes, i have just removed the unnecessary space. but I am now facing now about for loop process. :( any suggestion.. Thank You. Commented Dec 10, 2017 at 13:44
  • Are you using a WIndows editor? Windows uses cr lf (\r\n) as end of line; Linux and Unix use only \n. See man dos2unix. Commented Dec 10, 2017 at 13:54

2 Answers 2

3

This is one implementation of bubble sort:

#!/bin/bash
array=(5 3 2 1 4)
echo "${array[*]}"

size=${#array[@]}

for (( i=0; i<size-1; i++ )); do

   for (( j=0; j<size-i-1; j++ )); do
      if (( array[j] > array[j+1] )); then
         tmp=${array[j]}
         array[j]=${array[j+1]}
         array[j+1]=$tmp
      fi
   done

done

echo "Sorted array:"
echo "${array[*]}"

Major problem with your code is that it actually does not use arrays.

Define arrays like array=(value1 value2 value3). It's also better to use [[ ]] for testing instead of [ ]. If we were to change your code just a little to also create a functioning bubble sort algorithm, it could look like this:

#!/bin/bash
array=(5 3 2 1 4)
echo "${array[*]}"

changed=1 j=0

while [[ $changed != 0 ]]
do
    ((j++))
    changed=0
    for (( i=0; i<${#array[@]}-j; i++ )) 
    do  
        if [[ ${array[i]} -gt ${array[i+1]} ]]
        then
           tmp=${array[i]}
           array[i]=${array[i+1]}
           array[i+1]=$tmp
           changed=1
        fi  
    done        
done

echo "Sorted array:"
echo "${array[*]}"
Sign up to request clarification or add additional context in comments.

6 Comments

Hi! thanks for suggestion. I've tried to compile it online using jdoodle.com/test-bash-shell-script-online
@J.John why do you test it using online interpreter?
Hi @PesaThe, because I don't have linux installed in my laptop. :)
@J.John The code should work, try some other online tool, for example tutorialspoint looks very good
@J.John, you can get busybox, no installation needed.
|
1

I do not get the \r messages, even in your test environment; in general, they are a result of DOS/Windows combatability (with a b).

Since this is obviously a tutorial example (why else would someone do bubblesort), some remarks about the code.

array="5 3 2 1 4"

does not create the array that you want. It creates a string. What you are looking for is:

array=(5 3 2 1 4)

The last element of the array is ${#array[@]}-1. Element counting starts at 0. So your for-loop should be:

for (( i=0 ; i<=${#array[@]}-2 ; i++ )) 

-2 because you're referencing ${array[$i+1]}, which would otherwise be outside the boundary.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.