Python Weekly Week Sixth

Source: Internet
Author: User

1. Initial knowledge algorithm 1. Bubble sort
The bubble sorting algorithm works as follows: (from backto front) 1. Compare adjacent elements. If the first one is bigger than the second one, swap them both. 2. Do the same work for each pair of adjacent elements, starting from the first pair to the end of the last pair. At this point, the last element should be the maximum number. 3. Repeat the above steps for all elements except the last one. 4. Repeat the above steps each time for fewer elements until there are no pairs of numbers to compare.

The initial bubble algorithm code is as follows:

1 ##冒泡2a=[9,2,1,55,33]3 4  forIinchRange (len (a)):5      forJinchRange (Len (a)-1):6         ifA[J] > A[j+1]:7Middle =A[j]8A[J] = a[j+1]9A[J+1] =MiddleTen         Else: One             Pass A  - Print(a)

The results of the implementation are as follows:

[1, 2, 9, 33, 55]

Code anatomy: Start execution: Initial list a=[9,2,1,55,33], first outer loop i=0; first inner loop j=0; if A[0]:9 > a[1]:2, swap position, a[0]:2;a[1]:9, second inner loop j=1; if A[1]:9 > a[2]:1, Exchange position, third inner loop j=2; if A[2]:9 > A[3]:55, not established; pass, fourth time inner loop j=3; if a[3]:55 > a[4]:33, swap position, inner loop ends, results are obtained a[4]== 55, get the first big value. Second outer loop i=1; first memory loop j=0; if A[0]:2 > a[1]:1, Exchange position, second inner loop j=1; if A[1]:2 > A[2]:9, not established, pass, third inner layer cycle j=2; if a[2]:9> a[ 3]:33, not established, pass, the fourth time the inner layer cycle j=3; if a[3]:33 > a[4]:55, not set, pass, get results a[3] ==33, get the second largest value, and so on. One to get the final list like bubbling.

2. Select sort
Select sort (Selection sort) is a simple and intuitive sorting algorithm. It works as follows: first find the smallest element in an unordered sequence, place it at the beginning of the sort sequence, and then continue looking for the smallest element from the remaining unsorted elements, and then place it at the end of the sort sequence. And so on until all elements are sorted.

Select the sort entry code as follows:

##选择b = [8,2,1,55,33]num= Len (b)-1 forIinchRange (len (b)): Index=0 forJinchrange (num):ifB[index] > B[j+1]:            Pass        Else: Index= J+1Middle=B[num] B[num]=B[index] B[index]=Middle Num-= 1Print(b)

Code anatomy:

Python Weekly Week Sixth

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.