Cow farming learning shell bubble sort

Source: Internet
Author: User

#!/bin/bash
##############################################################
# File Name:/server/scripts/maopaopao.sh
# version:v1.0
# Author: Cattle Farming
# Created time:2017-09-20 19:44:10
# Description: Bubble sort
# Read Instructions:
# 1, read-a shuzu-a indicates that the next input is an array Shuzu is the array name set
# 2, ${#. [@]} The number of elements representing the array
# 3, ${. [@]} Represents all elements of an array
# 4 、.. [A] represents the first element of an array
##############################################################
echo "Please enter a number of numbers:"
Read-a Shuzu #获得数列
For ((a=0;a<${#shuzu [@]};a++)) #循环条件, A=0;a is less than the number of sequence elements, a++
Do #干
For ((b=0;b<${#shuzu [@]}-1;b++)) #
Do #干
if [[${shuzu[b]}-gt ${shuzu[b+1]}]];then #如果 the second element of the sequence is greater than the first B+1 element of the sequence is then
C=${SHUZU[B]} #将数列的第b个元素的的值赋给c
SHUZU[B]=${SHUZU[B+1]} #将数列第b + 1 elements assigned to the B element of the sequence
shuzu[b+1]= $c #将c的值赋给数列的第b + 1 elements The three steps are to swap the numbers in place
Fi #果如
Done
Done #干完了一个循环, back to for continue
echo ${shuzu[@]} #显示 all the elements inside the sequence

Cow farming learning shell 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.