Shell script sorting (Bubble Sorting), shell script sorting bubble

Source: Internet
Author: User

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

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.