Shell script sorting algorithm (bubble sort)

Source: Internet
Author: User
Tags array sort
#/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 array that is written above is the array sort that was previously defined in the code, and the following is the sort of array that the user customized during the execution.

#/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

$:./liu.sh 8 7 6 4 100 7
$: 4 6 7 7 8 100

Shell script sorting algorithm (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.