Swap sort-bubble sort + improvements

Source: Internet
Author: User

Bubble sort

Bubbling sorting is done by comparing and exchanging the positions of two elements adjacent to the unordered zone to achieve the order.

The insert sort is a sequence of 1 to n-1 elements that are unordered, the initial ordered area has only 0 of this element, and then the elements in the unordered area and the elements in the ordered area are compared and inserted. The scope of the orderly area is constantly expanding. Reduce the range of unordered areas until sorting is complete.

The bubble sort, however, is the whole sequence of unordered areas. That is, from 0 to n-1 these elements are in the unordered area. The position of the adjacent element is adjusted by comparing the elements of the adjacent elements to the result of the comparison of the size of the neighboring element and the desired final sort result.

For example, to sort results from small to large, then compare adjacent elements, a B C

If a>b:

Swap the position of a B and compare the A C size

Else

Keep a B position constant and compare the size of B C.

The code is as follows:

def Bubblesort (A, N):      for  in Range (n-1,0,-1):  # The entire sequence is unordered, and the initial participation in the sort is n elements for in         Range (i ):  #  traverse the adjacent elements within this trip and move to the far right.             if a[j]>a[j+1]:                a[j],a[j+1]=a[j+1],a[j]    return  AB  = [41,14,22,45,43,76,29,27,38]print(Bubblesort (B,len (B)))
View Code

Bubble sort-improved

, as the order progresses, it will be found that in the last three times the order has been arranged, the last three times the sort is a bit superfluous. So you can set an indication that if a trip is not exchanged, it means that all sorts have been completed to exit the loop and stop sorting.

The code is as follows:

defBubblesort (A, N): forIinchRange (n-1,0,-1): Exchange= False#set a flag        Print(A) forJinchRange (i):#traverse the adjacent elements within this tour, moving to the far right.             ifA[j]>a[j+1]: A[j],a[j+1]=a[j+1],a[j] Exchange= True#as long as there is a change in the position of the element in this tour, the state of the marker is changed.         ifExchange = = False:#if Exhange ==false can infer that the for loop on the upper level does not change the position of the element, it means that the sort is complete. You can stop the loop.             returnAB= [4,1,5,2,6,3]Print(Bubblesort (B,len (B)))

Swap sort-bubble sort + improvements

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.