Python version bubble sort

Source: Internet
Author: User

starting with the first element, 22 adjacent elements in the array are compared, the elements with the smaller values are placed in front, the larger values are placed in the back, the round is finished, and a maximum number sink becomes the last element in the array, and some smaller numbers float one position as bubbles. n number, after the n-1 round comparison completed sorting.

Suppose there is a sequence of n+1 numbers labeled 0~n , which requires ascending order, the following steps are implemented:

(1) Starting from the first 0 elements compared to the latter, if the larger than the two elements of the exchange, in turn, compared to the nth element, and eventually the largest number into the nth element,A (n) does not move

(2) repeat (1)   n-1 not moving

(3) Repeat (1) , compare to the first n-2 element, and eventually the largest number into the n-2 element,A (n-2) does not move

.............................................

(n) A (0) compared to a (1), if a (0) is large, and a (1) is exchanged, a (0) is the smallest


Code implementation:

#coding: Utf-8


arr = []


#冒泡排序函数

Def bubblesort ():

For I in range (len (arr)-1):

For j in Range (arr)-i-1):

If arr[j+1] < Arr[j]: #相邻两数比较

ARR[J+1],ARR[J] = arr[j],arr[j+1] #交换两个数


#打印函数

Def Printans ():

For I in Arr:

Print (i,end= "") #以空格分开来输出


#主函数

sum = Int (Input ("Enter the number of tests to be tested:"))

str_in = input ("%d data separated by a space:"%sum) #输入的是一串字符串, laying the groundwork for space-spaced input

For I in Str_in.split (): #以空格为间隔取str_in中的值

Arr.append (int (i)) #先把i对应转换成int类型, and then add to the ARR list

Bubblesort () #调用BubbleSort () function

Printans () #调用PrintAns () function



650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M01/92/13/wKioL1j7NqeDdJSXAACjOLkFVQA225.png-wh_500x0-wm_ 3-wmp_4-s_3292253552.png "title=" qq picture 20170422185024.png "alt=" Wkiol1j7nqeddjsxaacjolkfvqa225.png-wh_50 "/>

650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M02/92/14/wKiom1j7NsrAojbDAAANI5swj18818.png-wh_500x0-wm_ 3-wmp_4-s_2789203499.png "title=" qq picture 20170422185558.png "alt=" Wkiom1j7nsraojbdaaani5swj18818.png-wh_50 "/>







This article is from "A growing small tree" blog, please be sure to keep this source http://growing2015.blog.51cto.com/12006344/1918498

Python version bubble sort

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.