Select sort and Bubble sort

Source: Internet
Author: User

Select sort/Cell sort (Selection sort)

def selsort (L): for     I in range (len (L)-1):         print L         minindx = i         minval= l[i]         j = i + 1 while         J < ; Len (L):             if Minval > l[j]:                 minindx = J                 minval= L[j]             j = j + 1         temp = l[i]         l[i] = L[minindx]         

Principle:
The first element in the list is the minimum number, followed by a backward comparison. If it is less than the second element, the comparison continues with the third element. When the first element is greater than the element to which it is compared, the element is replaced by the first element as the minimum number, and the backward comparison continues. Meeting the large number continues, meeting decimals is replaced by decimals until the list is last. The element with the smallest number in the list and the first element's interaction position, that is, the element with the smallest number is ranked first. Repeat steps from the second element, the third element, the fourth element ... Each cycle will find the smallest of the remaining elements aligned to the front.

Example:
Sort the list [3,4,7,2,5,1]

[3,4,7,2,5,1]
Start with the 1th element. 3 is the decimal. 3:4 Small, continued, 3:7 small, continued, 3:2 large, 2 as the minimum, 2:5 small, continued, 2:1 large, 1 as the minimum, and the comparison ended. Swap positions of 1 and 3.
[1,4,7,2,5,3]
Start with the 2nd element. 4 is the decimal. 4:7 Small, continue, 4:2 large, 2 as the minimum, 2:5 small, continue, 2:3 small, the comparison is over. Swap positions of 2 and 4.
[1,2,7,4,5,3]
Start with the 3rd element. 7 is the decimal. 7:4 large, 4 as the minimum number, 4:5 small, continue, 4:3 large, 3 as the minimum number, the comparison ends. Swap positions of 3 and 7.
[1,2,3,4,5,7]
Start with the 4th element. 4 is the decimal. 4:5 Small, continue, 4:7 small, the comparison is over.
[1,2,3,4,5,7]
Start with the 5th element. 5 is the decimal. 5:7 small, compared to the end. Sort end.

Bubble sort (Bubble sort)

def bubblesort (L):    swapped = True while    swapped:        swapped = False for        i in range (Len (l)-1):            if L[i] & Gt L[I+1]:                temp = l[i]                l[i] = l[i+1]                l[i+1] = temp                swapped = True

Principle:
Starting with the first element of the list, if the first element is less than the second element, the second element and the third element continue to be compared. If the second element is greater than the third element, the position of the second and third element is swapped, and the third element and the fourth element are compared, less then continued, and the interchange is greater than the other. Proceed sequentially, and the largest element in the list will be queued to the end of the table. Repeat from the first element, the second largest element to the second-to-last position in the list, the third-largest element to the third-lowest position ... That is, each time the loop swaps a number to the end of the list.

Example:
Sort the list [3,4,7,2,5,1]

[3,4,7,2,5,1]
Start with the 1th element. 3:4 Small, continue, 4:7 small, continue, 7:2 large, swap 2 and 7 positions, 7:5 large, swap 7 and 5 positions, 7:1 large, swap 7 and 1 positions. The comparison ends.
[3,4,2,5,1,7]
Start with the 1th element. 3:4 Small, continue, 4:2 large, Exchange 4 and 2 position, 4:5 Small, continue, 5:1 large, Exchange 5 and 1 position, 5:7 Small, the comparison is over.
[3,2,4,1,5,7]
Start with the 1th element. 3:4 Small, continue, 4:2 large, Exchange 4 and 2 position, 4:1 large, Exchange 4 and 1 position, 4:5 Small, continue, 5:7 small, the comparison is over.
[2,3,1,4,5,7]
Start with the 1th element. 2:3 Small, continue, 3:1 large, Exchange 3 and 1 position, 3:4 Small, continue, 4:5 small, continue, 5:7 small, the comparison is over.
[2,1,3,4,5,7]
Start with the 1th element. 2:1 large, Exchange 2 and 1 positions, 1:3 Small, continue, 3:4 small, continue, 4:5 small, continue, 5:7 small, the comparison is over.
[1,2,3,4,5,7]
Start with the 1th element. 1:2 Small, continue, 2:3 small, continue, 3:4 small, continue, 4:5 small, continue, 5:7 small, the comparison is over. Sort end.

The algorithm complexity of selecting sort and bubble sort is squared. But choosing a sort is more efficient.

Select sort and 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.