Reading through this article will no longer have a headache for "array sorting algorithm" (bubble sorting algorithm, direct sorting algorithm, reverse sorting algorithm)

Bubble Sort Algorithm (Bubble Sort)

Overview and basic idea of ​​the algorithm

  • The origin of the name of this algorithm is because the smaller the element will slowly "float" to the top of the sequence (ascending or descending order) through exchange, just like the carbon dioxide bubbles in carbonated beverages will eventually rise to the top, hence the name "bubble" Sort"
  • The basic idea: bubble sorting is to compare the values ​​of two adjacent elements, if the corresponding conditions are met, exchange the element values ​​of each other, move the smaller element to the front of the array, and move the larger element to the back of the array (that is, exchange The position of the two elements), 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 loop. The
    outer loop is used to control the number of sorting rounds (that is, several rounds of sorting operations). Generally, the length of the array to be sorted is "minus 1" times, because only the last cycle is left The next array element does not need to be compared, and the array has already been sorted.
    The inner loop is 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

Schematic diagram of the sorting process

Insert picture description here

Command script

method one

[root@localhost ~]# ./28.sh 
需排序数组的值为:61 6 22 1 4 13
排序后的新数组值为:1 4 6 13 22 61
[root@localhost ~]# vim 28.sh

#!/bin/bash

sz=(61 6 22 1 4 13)
echo "需排序数组的值为:${sz[@]}"

leng=${
    
    #sz[@]}                 
【获取数组长度】

for ((w=1;w<$leng;w++))        
【定义比较轮数,为数组长度减1,因为这里是小于,所以不用减1,并每次执行+1的操作】
do

  for ((e=0;e<$leng-$w;e++))  
【因为这里的变量e是用来定义第一个比较元素的索引值,所以从0开始,又因为最后一次比较是索引4和索引5进行比较,所以这里变量e的值小于数组长度
减变量w(减变量w而不直接减1是因为待比较元素的位置需要随着轮数的增加而减少)】
  do
  one=${
    
    sz[$e]}                
【定义第1个比较元素的值】
  two=${
    
    sz[$e+1]}
【定义第2个比较元素的值】
    if [ $one -gt $two ];then
【如果第1个元素的值大于第2个元素的值,则只需位置交换操作】
    sz1=$one
【把第一个元素的值赋给临时变量sz1】
    sz[$e]=$two
【把第二个元素的值赋给第一个元素】
    sz[$e+1]=$sz1
【再把第一个元素的值(sz1的值)赋给第二个元素】
    fi
  done

done

echo "排序后的新数组值为:${sz[*]}"  

Method two (manually define the array values ​​that need to be sorted)

[root@localhost ~]# ./28-1.sh 
输入你需要排序的数组:33 2 28 5 44 32 18 29
需排序数组的值为:33 2 28 5 44 32 18 29
排序后的新数组值为:2 5 18 28 29 32 33 44
[root@localhost ~]# vim 28-1.sh 

#!/bin/bash
read -p "输入你需要排序的数组:" num     【将需要排序的数组值赋给变量num】
sz=($num)                             【与方式一的区别在于这里是通过变量num获取到值,而不是脚本内定义好值】
echo "需排序数组的值为:${sz[@]}"

leng=${
    
    #sz[@]}

for ((w=1;w<$leng;w++))
do

  for ((e=0;e<$leng-$w;e++))
  do
  one=${
    
    sz[$e]}
  two=${
    
    sz[$e+1]}

    if [ $one -gt $two ];then
    sz1=$one
    sz[$e]=$two
    sz[$e+1]=$sz1

    fi
  done

done

echo "排序后的新数组值为:${sz[*]}"  

Method three (encapsulate the algorithm into a function, and pass the parameter to be sorted into the algorithm through the function)

[root@localhost ~]# ./28-2.sh 88 91 102 32 45 27 1 13
需排序数组的值为:88 91 102 32 45 27 1 13
排序后的新数组值为:1 13 27 32 45 88 91 102
[root@localhost ~]# vim 28-2.sh 

sz=($@)
echo "需排序数组的值为:${sz[@]}"

leng=${
    
    #sz[@]}

for ((w=1;w<$leng;w++))
do

  for ((e=0;e<$leng-$w;e++))
  do
  one=${
    
    sz[$e]}
  two=${
    
    sz[$e+1]}

    if [ $one -gt $two ];then
    sz1=$one
    sz[$e]=$two
    sz[$e+1]=$sz1

    fi
  done

done

echo "排序后的新数组值为:${sz[*]}"  

}

#################main#################   【仅为注释从这以下为主代码】
list=$@                                  【将命令行获取到的数组值赋给变量list】
paixu $list                              【将变量list获得的值传参到函数paixu内】

Straight Select Sorting (Straight Select Sorting)

Overview and basic idea of ​​the algorithm

  • Compared with bubble sorting, the number of exchanges for direct selection sorting is less, so the calculation speed will be faster
  • Basic idea: Compare the specified sort position with other array elements separately, and exchange element values ​​if the conditions are met
  • The difference with bubble sorting: instead of exchanging adjacent elements, the elements that meet the conditions are directly exchanged with the specified sorting position (for example, sorting from the last element). In this way, the sorted position gradually expands, and finally the entire array is output into a sorted format

Schematic diagram of the sorting process

Insert picture description here

Command script

[root@localhost ~]# ./29.sh 
待排序的数组值为:63 4 24 1 3 15
排序后的数组值为: 1 3 4 15 24 63
[root@localhost ~]# vim 29.sh

#!/bin/bash

sz=(63 4 24 1 3 15)
echo "待排序的数组值为:${sz[@]}"
leng=${
    
    #sz[@]}                           【定义排序的轮数】

for ((w=1;w<$leng;w++))
do

   jx=0                                  【假设所以为0的元素是最大,并将值赋给jx】
   for ((e=1;e<=$leng-$w;e++))           【定义和第一个元素比较的索引,用来确定最大的元素索引】   
   do

     max=${
    
    sz[$jx]}                      【定义最大的元素的值】
     cs=${
    
    sz[$e]}                        【定义从索引1开始比较的元素的值】

     if [ $cs -gt $max ];then            【判断如果从索引1开始比较的元素值大于当前最大元素的值时,则记录最大值的索引到变量jx】
        jx=$e
     fi
   done
qz=$[$leng-$w]                           【定义每一轮比较的最后一个元素的索引】
qz1=${
    
    sz[$qz]}                           【把当前轮次的最后一个元素的值赋给临时变量qz1】
sz[$qz]=${
    
    sz[$jx]}                       【把最大的值赋给当前轮次的最后一个元素】
sz[$jx]=$qz1                             【把qz1里的原来最后一个元素的值赋给原来最大值所在索引的元素】

done

echo "排序后的数组值为: ${sz[*]}"

Reverse Sort Algorithm (Reverse Sort)

Overview and basic idea of ​​the algorithm

  • Reorder the contents of the original array in reverse order
  • Basic idea: replace the last element of the array with the first element, replace the penultimate element with the second element, and so on, until all the array elements are reversed and replaced

Command script

[root@localhost ~]# ./30.sh 
待排序的数组为:3 4 24 31 43 65 89
反转排序后的数组顺序为:89 65 43 31 24 4 3
[root@localhost ~]# vim 30.sh

#!/bin/bash

sz=(3 4 24 31 43 65 89)
echo "待排序的数组为:${sz[@]}"

leng=${
    
    #sz[@]}                  【输出数组sz的长度值并赋给变量leng】

for ((w=0; w<$leng/2; w++))     【索引从0开始,/2是折半对调,所以是长度/2,且索引每次增加1次】
do
  one=${
    
    sz[$w]}                 【将第一个索引对应的元素赋值给变量one】  
  two=${
    
    sz[$leng-1-$w]}         【将最后一个索引对应的元素赋给变量two】

  sz1=$one
  sz[$w]=$two                   【将最后一个索引对应的元素赋给第一个索引对应的元素】
  sz[$leng-1-$w]=$sz1           【将第一个索引对应的元素赋给最后一个索引对应的元素】

done

echo "反转排序后的数组顺序为:${sz[@]}"

Guess you like

Origin blog.csdn.net/TaKe___Easy/article/details/114690492