Shell script Sorting Algorithm (Bubble Sorting)
#/Bin/basha = (9 84 51 0 345 1 2 34 1 0) # define an array temp = for (I = 0; I <10; I ++) {for (j = I; j <10; j ++ )) {x =$ {a [$ I]} if test $ x-ge $ {a [$ j]} then temp =$ {a [$ I]} a [$ I] =$ {a [$ j]} a [$ j] = $ tempfi} for (k = 0; k <10; k ++) {echo-n $ {a [$ k]} ""} echo
The array written above is the array sorting defined in the Code beforehand. The following is the array sorting customized by the user during execution.
#/Bin/basha = 'expr $ # + 1' # expr is a computing operation, $ # is the number of parameters, $ # + 1 is because $0 does not store parameters. temp = for (I = 1; I <$ a; I ++) {B [$ I] = $1 shift 1} for (I = 1; I <$ a; I ++) {for (j = I; j <$ a; j ++ )) {x =$ {B [$ I]} if test $ x-ge $ {B [$ j]} thentemp =$ {B [$ I]} B [I] = $ {B [$ j]} B [j] = $ temp # equivalent to bubble sort fi} for (k = 1; k <$ a; k ++) {echo-n $ {B [$ k]} "" # Show without line feed} echo
$:./Liu. sh 8 7 6 4 100 7
$: 4 6 7 8 100