Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 为什么在执行手动冒泡排序时,在bash中使用关联数组得到这种奇怪的输出?_Linux_Bash_Associative Array_Bubble Sort - Fatal编程技术网

Linux 为什么在执行手动冒泡排序时,在bash中使用关联数组得到这种奇怪的输出?

Linux 为什么在执行手动冒泡排序时,在bash中使用关联数组得到这种奇怪的输出?,linux,bash,associative-array,bubble-sort,Linux,Bash,Associative Array,Bubble Sort,我有一个bash关联数组,如下所示 declare -A arraySalary=( [1]=1000 [8]=3000 [2]=2000) 我正在学习bash脚本,并试图在数组上实现气泡排序 用这段代码 sortedDesc=false while ! $sortedDesc ; do sortedDesc=true for ((currentIndex=0; currentIndex<$((${#arraySalary[@]} -1)); currentIndex++

我有一个bash关联数组,如下所示

declare -A arraySalary=( [1]=1000 [8]=3000 [2]=2000)
我正在学习bash脚本,并试图在数组上实现气泡排序 用这段代码

sortedDesc=false
while ! $sortedDesc ;
do
    sortedDesc=true
    for ((currentIndex=0; currentIndex<$((${#arraySalary[@]} -1)); currentIndex++))
    do
        if [[ ${arraySalary[$((currentIndex))]} -lt ${arraySalary[$((currentIndex + 1))]} ]]
        then
            sortedDesc=false    
            biggerNumber=${arraySalary[((currentIndex - 1))]}
            arraySalary[$((currentIndex + 1))]=${arraySalary[$((currentIndex))]}
            arraySalary[currentIndex]=${biggerNumber}
            echo "swapped"
        fi
    done
done

echo "Printing new values"
# Print new values
for key in "${!arraySalary[@]}";
do
    echo $key "->" ${arraySalary[$key]}
done

有人能解释一下为什么会这样吗?谢谢

我不知道您为什么选择使用关联数组而不是普通数组,但您需要知道关联数组的索引不是数字上下文

如果用
-a
而不是
-a
声明了
阵列报警
,那么这就可以了:

arraySalary[currentIndex]=${biggerNumber}
因为序号数组的索引是数字上下文,并且在数字上下文中,您可以使用variabke名称,而无需
$
。但由于它是关联的,所以它所做的是设置其键为字符串
currentIndex
的元素。你想要的是:

arraySalary[$currentIndex]=${biggerNumber}

无需编写
$((currentIndex))
;bash不区分整数和包含整数的字符串。

仅根据数组的值对数组进行排序将很容易。在对键进行排序的情况下 通过使用assoc数组来确定其值,您需要引入另一个数组来保持元素的顺序,因为assoc数组是无序的

让我们命名新的数组
索引
,它包含
(1 8 2..)
,然后脚本将如下所示:

#!/bin/bash

declare -A arraySalary=([1]=1000 [8]=3000 [2]=2000)
declare -a index=("${!arraySalary[@]}")

sortedDesc=false
while ! $sortedDesc ;
do
    sortedDesc=true
    for ((i=0; i<$((${#index[@]} - 1)); i++))
    do
        if [[ ${arraySalary[${index[$i]}]} -lt ${arraySalary[${index[$(($i + 1))]}]} ]]
        then
            sortedDesc=false
            biggerIndex=${index[$(($i + 1))]}
            index[$(($i + 1))]=${index[$i]}
            index[$i]=$biggerIndex
            echo "swapped"
        fi
    done
done

echo "Printing new values"
# Print new values
for key in "${index[@]}";
do
    echo $key "->" ${arraySalary[$key]}
done
#!/bin/bash

declare -A arraySalary=([1]=1000 [8]=3000 [2]=2000)
declare -a index=("${!arraySalary[@]}")

sortedDesc=false
while ! $sortedDesc ;
do
    sortedDesc=true
    for ((i=0; i<$((${#index[@]} - 1)); i++))
    do
        if [[ ${arraySalary[${index[$i]}]} -lt ${arraySalary[${index[$(($i + 1))]}]} ]]
        then
            sortedDesc=false
            biggerIndex=${index[$(($i + 1))]}
            index[$(($i + 1))]=${index[$i]}
            index[$i]=$biggerIndex
            echo "swapped"
        fi
    done
done

echo "Printing new values"
# Print new values
for key in "${index[@]}";
do
    echo $key "->" ${arraySalary[$key]}
done
swapped
swapped
swapped
Printing new values
8 -> 3000
2 -> 2000
1 -> 1000