# Python Bubble sorting and python Bubble Sorting

Source: Internet
Author: User

Python Bubble sorting and python Bubble Sorting

Bubble sorting, as the name implies, keeps data in a regular order.

1 import random 2 3 def bubblesort (data): 4 change = True 5 for I in range (len (data)-1, 1,-1): 6 for j in range (0, i): 7 if data [j]> data [j + 1]: 8 data [j], data [j + 1] = data [j + 1], data [j] 9 change = False10 if change: 11 break12 13 data = [random. randint (1000) for I in range ()] 14 print (data) 15 bubblesort (data) 16 print (data)View Code

What is the bubble method? [Details]

BubbleSort
Basic Concepts
The basic concept of Bubble Sorting is to compare two adjacent numbers in sequence, put decimal places in front, and put large numbers in the back. That is, first compare the numbers of 1st and 2nd, and put the decimal places before and after the large numbers. Then compare the numbers of 2nd and 3rd, place the decimal places before and after the large number, and continue until the last two digits are compared. Place the decimal places before and after the large number. Repeat the above process and compare it from the first logarithm (because of the exchange of 2nd and 3rd, the number of 1st is no longer greater than 2nd). Before and after placing the decimal number, always compare to the adjacent number before the minimum number. Place the decimal number before the decimal number, place the large number, and end with the second row. A new minimum number is obtained in the second to last number. So on until the sorting is completed.
Because the sorting process always places decimal places forward and large numbers backward, it is equivalent to bubbles rising, so it is called Bubble sorting.
It is implemented using a double loop. The External Loop Variable is set to I and the inner loop variable is set to j. The External Loop repeats 9 times, and the inner loop repeats 9, 8,..., 1 time in sequence. The two elements for each comparison are related to the inner loop j, which can be identified by a [j] And a [j + 1] respectively, the values of I are 1, 2 ,..., 9. For each I, j values are 1, 2 ,... 10-i.
Generate
In many program designs, we need to sort a sequence to facilitate statistics. Common sorting methods include Bubble sorting, binary tree sorting, and selective sorting. Bubble Sorting has always been favored by its concise ideas and methods and high efficiency.
Sorting Process
Imagine the sorted array R [1 .. n] vertical erect, each data element is considered as a bubble with weight, according to the principle that light bubbles cannot be under heavy bubbles, scanning array R from bottom up, when a light bubble that violates this principle is scanned, it is made to "float" up, so it is repeated until the last two bubbles are both light and heavy.
Algorithm example
49 13 13 13 13 13 13 13 13
38 49 27 27 27 27 27
65 38 49 38 38 38 38 38 38
97 65 38 49 49 49
76 97 65 49 49 49 49
13 76 97 65 65 65 65 65
27 27 76 97 76 76 76
49 49 49 76 97 97 97
Procedure BubbleSort (Var R: FileType) // Bubble Sorting from bottom to top //
Begin
For I: = 1 To N-1 Do // Do N-1 sort //
Begin
NoSwap: = True; // set unordered flag //
For J: = N-1 DownTo 1 Do // scan from bottom up //
Begin
If R [J + 1] <R [J] Then // exchange element //
Begin
Temp: = R [J + 1]; R [J + 1: = R [J]; R [J]: = Temp;
NoSwap: = False
End;
End;
If NoSwap Then Return // If no exchange occurs in this sort, terminate the algorithm //
End
End; // BubbleSort //
The time complexity of this algorithm is O (n2), and the algorithm is a stable ordering party.
Bubble sort c ++ code
# Include <iostream. h>
Void B... the remaining full text>

The difference between Bubble sorting and selected sorting

Yes.
The main difference lies in the way of exchange.

In each round, the largest or smallest element is filtered out and placed in the corresponding position.
This is the same
However
For each round
For example, the first round
To set ~ Place the largest one in n to n.
The bubble method compares and moves two adjacent items at a time.
The current item and the nth item are exchanged each time.
After writing the code, you will understand:
Bubble:
For I: = 1 to n-1 do
If (a [I]> a [I + 1]) then swap (I, I + 1 );
Select:
For I: = 1 to n-1 do
If (a [I]> a [n]) then swap (I, n );
(Swap indicates switch)

In general, the two sort comparisons have the same number of times.
However, if the number of exchanges is selected, the sorting is less.
Although the time complexity of both is O (n ^ 2)
But usually, the sorting is faster.
Reference: hi.baidu.com/...7.html

Related Keywords:
Related Article

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.