"Algorithm" bubble sort

Source: Internet
Author: User

# first time, find the maximum value

n=[3,5,1,6,2]

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

If N[I]>N[I+1]:

N[i],n[i+1]=n[i+1],n[i]

Print (N[-1])

# Second, find the second largest value, put it in the penultimate position.

n=[3,1,5,2,6]

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

If N[I]>N[I+1]:

N[i],n[i+1]=n[i+1],n[i]

Print (N[-2])

# third time, find the third largest number

n=[1,3,2,5,6]

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

If N[I]>N[I+1]:

N[i],n[i+1]=n[i+1],n[i]

Print (n[-3])

# fourth time, find the fourth largest number

n=[1,2,3,5,6]

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

If N[I]>N[I+1]:

N[i],n[i+1]=n[i+1],n[i]

Print (n[-4])

# Bubble Sort Final

a= [3,5,1,6,2]

For I in range (Len (a)-1): #0,

For j in Range (Len (a) -1-i): #4 -0;4-1;4-2;4-3

If A[J]>A[J+1]:

A[J],A[J+1] = A[j+1],a[j]

Print (a)

The result of the first inner loop is to find the maximum value

The result of the second inner loop is to find the second largest value, this time ignoring the comparison of the last element

The result of the second inner loop is to find the third largest value, and this session ignores the comparison of the second and last element of the penultimate one.

.......

1. Bubble sort icon

#练习: On the basis of sorting above, put the smallest in the back, the largest in front

a= [3,2,1,9,56,48,20,4,9,6,0]

For I in range (Len (a)-1):

For j in Range (Len (a) -1-i):

If A[J]<A[J+1]:

A[J],A[J+1] = A[j+1],a[j]

Print (a)

or directly

a= [3,2,1,9,56,48,20,4,9,6,0]

For I in range (Len (a)-1):

For j in Range (Len (a) -1-i):

If A[J]>A[J+1]:

A[J],A[J+1] = A[j+1],a[j]

Print (A[::-1])

2. Counterfeit sorting principle

The basic idea of bubble sorting is to make 22 comparisons of adjacent elements, in reverse order, so that each trip will "float" the smallest or largest element to the top, culminating in an entirely orderly

3. Time complexity of bubbling algorithm

The order of the time complexity from small to large is:

Constant order O (1), logarithmic order O (log n), linear order O (n), square order O (n^2), Cubic O (n^3),..., K-Order O (n^k), exponential order O (2^n). With the increasing of the problem scale N, the complexity of the time is increasing and the efficiency of the algorithm is less. That is
O (1) < O (Logn) < O (n) < O (Nlogn) <o (n^2) < O (n^3) <...< O (n^k) <o (2^n) <o (n!)

For the algorithm, just see what is the most influential factor (how to determine which factor is the most influential?). ), generally ignoring the denominator, without looking at the denominator

#练习: Calculating the time complexity of bubbling sorting

When traversing the 1th pass, the n-1 is compared;

When traversing the 2nd pass, the n-2 is compared;

When traversing the 3rd pass, the n-3 is compared;

When traversing the 4th pass, the n-4 is compared;

..........

Traversing the first n-1 times, compared 1 times;

Overall, the total number of comparisons is 1+2+3+4+....+ (n-1) =n (n-1)/2 times

Because, N (n-1)/2=N^2/2-N/2

So, his time complexity is O (n^2)

"Algorithm" 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.