Shell script sorting (Bubble Sorting), shell script sorting bubble
#/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 above is sorted by a custom fixed array, and the following is a user-defined input array.
#/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~ $./A. out 7 6 56 4 3
~ $3 4 6 7 56