shell bubble sort algorithm to solve an array of problems

First, we must understand the operating principle of the bubble algorithm

1. By comparing the two numbers, a large number of backward movement, and then compares the next, if it is large, continues to move rearwardly.

2. Each number should be compared

  . 1 ! # / Bin / the bash
   2  
  . 3  echo  " Enter an array: " 
  . 4 Read - A NUM
   . 5  
  . 6  for ((I = 0 ; I <$ {# NUM [@]}; I ++ ))
   . 7  do 
  . 8          for (( J = 0 ; J <$ {# NUM [@]} - . 1 ; J ++ ))
   . 9          do 
 10                  IF [$ {NUM [J]} -gt $ {NUM [J + . 1 ]}]
  . 11                  the then 
 12 is                          tmp = $ { NUM [J]}
  13 is                          NUM [J] = $ {NUM [+ J . 1 ]}
  14                         NUM [+ J . 1 ] = $ tmp
  15                  Fi 
 16          DONE 
 . 17  DONE 
 18 is  
 . 19  echo  " sequence is complete !!!! " 
 20 is  echo  " $ {NUM [@]} "

test:

. [Logan the root @ Day4] numpaixu the bash # SH 
enter an array: 
100  59  41 is  52 is  . 1  2  . 8  74  62 is  
the sort is complete! ! ! ! 
. 1  2  . 8  41 is  52 is  59  62 is  74  100

Implement sorting function

Guess you like

Origin www.cnblogs.com/loganSxb/p/11077408.html