Bubble sorting, direct sorting, and reverse sorting in shell scripts

One, bubble sort

Insert picture description here

Similar to the upsurge of bubbles, the data will continue to move forward in the array from small to large or from large to small.

Basic idea:
The basic idea of ​​bubble sorting is to compare the values ​​of two adjacent elements. If the conditions are met, the element values ​​are exchanged, the smaller element is moved to the front of the array, and the larger element is moved to the back of the array (that is, exchange two The position of each element), so that the smaller element rises from the bottom to the top like a bubble.

Algorithm idea: The
bubbling algorithm is implemented by a double-layer loop, where the outer loop is used to control the number of sorting rounds. Generally, the length of the array to be sorted is reduced by 1, because there is only one array element left in the last loop, and no comparison is required. At the same time, the array The sorting has been completed. The inner loop is mainly used to compare the size of each adjacent element in the array to determine whether to exchange positions. The number of comparisons and exchanges decreases with the number of sorting rounds.


#!/bin/bash
#冒泡排序

arr=(10 44 55 24 33 78 11)             #这里是我自己定义得数组arr
length=${#arr[@]}
echo "原数组为:${arr[@]} "
for((i=1;i<${#arr[@]};i++))            #外部循环,控制轮次
do
   for ((j=0;j<${#arr[@]}-$i;j++))     #内部循环,控制每轮比较次数
   do

   if [ ${arr[$j]} -gt ${arr[$j+1]} ]  #对每轮从头开始得相邻两个数进行比较,小的上浮(放前面),大的下沉(放后面替换位置) 
   then
   temp=${arr[$j]}                     #这里把前一个定义给一个变量temp
   arr[$j]=${arr[$j+1]}                #如果前一个大于后一个,则两两交换位置
   arr[$j+1]=$temp

   fi

   done
done
   echo "冒泡排序完得数组为:${arr[@]} "



success!
Insert picture description here

Two, direct sorting

Insert picture description here

Compared with bubble sorting, direct selection sorting has fewer exchanges, so it is faster.

The basic idea:
Compare the specified sort position with other array elements separately. If the condition is met, the element value will be exchanged. Note that the difference between bubble sorting is not to exchange adjacent elements, but to exchange the elements that meet the condition with the specified sort position (such as Sort from the last element), so that the sorted position gradually expands, and finally the entire array becomes the sorted format


#!/bin/bash
#直接排序

arr=(10 44 55 24 33 78 11)
echo "原先得数组为:${arr[@]} "
length=${#arr[@]}
for ((i=1;i<$length;i++))
do
    index=0                                #定义一个index变量,用来接收
    for ((j=1;j<=$length-i;j++))           #这里我用的是<=因为我下面用的是 -lt 来判断的,所以这里需要<=
    do
     if [ ${arr[$index]} -lt ${arr[$j]} ]  #这里我用的是小于
     then
       index=$j

     fi

       last=$[ $length - $i ]
       temp=${arr[$last]}
       arr[$last]=${arr[$index]}

       arr[$index]=$temp

    done
done
    echo "直接排序完得数组为:${arr[@]} "


Insert picture description here

Three, reverse sort

Reorder the contents of the original array in reverse order

The basic idea:
replace the last element of the array with the first element. The penultimate element is replaced with the second element, and so on, until all the array elements are reversed and replaced

#!/bin/bash
#直接排序

arr=(10 44 55 24 33 78 11)
echo "原先得数组为:${arr[@]} "
length=${#arr[@]}
for ((i=1;i<$length;i++))
do
    index=0
    for ((j=1;j<=$length-i;j++))
    do
     if [ ${arr[$index]} -lt ${arr[$j]} ]
     then
       index=$j

     fi

       last=$[ $length - $i ]      #从这里开始,以下就是首尾替换。
       temp=${arr[$last]}
       arr[$last]=${arr[$index]}

       arr[$index]=$temp

    done
done
    echo "直接排序完得数组为:${arr[@]} "


Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44324367/article/details/114818318