Bubble sort of shell programming

This is a shell script practice program, practice the use of the for statement

#! /bin/bash
n=0
while read arr[$n] #Assign the array through the keyboard...the first element of the array is stored in arr[0]...the carriage return indicates the input interval of the element, and Ctrl-d indicates the end of the input
do
   n=$[$n+1]
done              
#echo ${arr[*]} #This is a debugging point to observe whether the array is successfully generated (if successful, output all array elements horizontally)

len=${#arr[*]} #Get the length of the array

for((i=0;i<len;i++)) #Bubble sort algorithm... Note: The for statement is followed by "double parentheses"
do
    for((j=0;j<len-i-1;j++))
    do
        t=$[$j+1] #In order to improve the readability of the program, use the variable t to represent the "arr(j+1) element"
        if [[ ${arr[$j]} -gt ${arr[$t]} ]] #Brackets indicate that there must be a space between the conditional operation and the expression in it, -gt indicates greater than
        #if [[ ${arr[$j]} -gt ${arr[$[$j+1]} ]] #This is written without variable t, "arr(j+1) element" is a bit unintuitive span
        then
              term=${arr[$j]}
              arr[$j]=${arr[$t]}
              arr[$t]=$term
        be
    done
done

for((k=0;k<len;k++)) #After sorting, output the array elements in order
do
    echo ${arr[$k]}
done

The result of running the script program:

Explanation: The numbers above the blue arrow in the figure are the user input (press Enter for each number entered, and after all numbers are input, press Ctrl+d to indicate the end of the input), and the numbers below the blue arrow are the program output (press Enter). Arranged in order from smallest to largest)

Note: shell scripts can easily implement "indefinite-length arrays" (that is, when declaring an array, you can not specify the length of the array, but "automatically" determine the length of the array according to the user's input, such as the user input ten array array lengths It is 10), it seems that C language or java is not easy to implement this... Arrays of indefinite length, such things may be the advantages of interpreted languages

 

There are two types of for statements in shell scripts:
one is a "traverser" (that is, a for-in loop). Similar to the foreach statement in java, there must be a set before executing the loop. The process of loop execution is the process of traversing the elements of the set. ...you can refer to the " 99 Multiplication Table of Shell Programming " written by me.
One is that the "automatic machine" is similar to the for loop in the C language, and the boundary and step size of the loop need to be described... This is what this article introduces: the keyword for It is followed by double parentheses, in which the loop control variable is used to describe the starting condition, the ending condition and the change (self-increment) rule of the variable... This is almost the same as the for loop in C language

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325168723&siteId=291194637