Shell script sort (bubble sort)

Source: Internet
Author: User
#/bin/basha= (9 0 345 1 2 1 0) #自己定义一个数组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, the following is the user-defined input array to sort.

#/bin/basha= ' expr $# + 1 ' #expr是一个计算操作, $ #是参数个数, $#+1 is because there are no storage parameters. Temp=for ((i=1;i< $a; i++)) {b[$i]=$1shift 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 # Quite with bubble sort fi}}for ((k=1;k< $a; k++)) {echo-n ${b[$k]} "" #不换行显示}echo
~$./a.out 7 6 56 4 3

~$3 4 6 7 56

Shell script sort (bubble sort)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.