In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Shell script programming bubble sort script implementation
Bubble sort as the most basic algorithm in programming, computer programming for beginners is worth more research and try to write more. For programming, it is not the language that is good or bad, but the logical thinking and sudden inspiration to achieve functionality.
Here is a script that uses shell to implement bubble sort, involving regular expressions that need to be understood in detail. A detailed explanation is given in the comments below.
#!/ bin/bash#The core idea of bubble sorting is to introduce variables, compare pairwise, match unchanged, and vice versa #The script is as follows: #Wait for user input read -p "please input num of this array:" n#The following is a test statement to determine whether the input is a number, but the error output prompt is not friendly, so change it to if#[ $n =~ ^[0-9]+$ ]&&echo "welcome"|| exit 1if [[ $n =~ ^[0-9]+$ ]]then echo "welcome"else echo "sorry you input no a number! " exit 1fi #loop n times, waiting for the user to enter a number, print and display the array contents for i in `seq $n`do read -p "please input your num:" num if [[ $num =~ ^[0-9]+$ ]];then : else echo "sorry you input no a number! " exit 1 fi let i=i-1array[$i]=$numdoneecho "array of the original order: "echo ${array[*]}#bubble sort algorithm, the core lies in the following if statement, in fact, it is not difficult to understand, if you played Hanoi Tower as a child is very easy to understand #Give a simple example: If you want to swap two different juices, you need an empty glass as an intermediary, which is similar to the temp variable #below. The script implements sorting from small to large, so the if judgment is-gt; and the script requires a double loop #Double loop formula: the inner loop is executed once, the outer loop is executed once; the combination of j and k below means that j=0 starts execution, and j=2 is not executed until k is n, and so on.# The inner loop controls a single comparison: that is, compare the first number with all the following numbers, replace it with the smaller one, and continue to execute if (where the variables involved in the comparison have changed) for (j=0;j
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.