Data structures and algorithms - Sorting Algorithm - Bubble Sort

################## Sorting Algorithm ######################

"" " 

Sorting algorithm, 
we want to disorderly sequence of linear tables, arranged in an ordered sequence, the algorithm is sorting algorithm, 

the stability of sorting algorithms 
Example: The following assumptions tuples to be their first numbers ordered. 
(4, 1) (3, 1) (3, 7) (5, 6) 
If, after you sort, (3, 1) (3, 7), and the original order, as is a stable, otherwise, it is unstable, 
(3, 1) (3, 7) (4, 1) (5, 6) (maintain order) 
(3, 7) (3, 1) (4, 1) (5, 6) (the order is changed) 

we say is the default ordering from small to large, ""
"

 

################## Bubble Sort ######################

"" " 

Bubble sort idea 

Analyzing the relationship between the present and the next digit numbers, the 
first case, if the present large numbers, the numbers are present and the cursor is moved backward, 
the second case, if this numeric, this figure does not move, move the cursor back, 
so go down a front to back, the largest digital moves to the final surface, and 
then perform one has been executed until all lined up, so the design must be thinking two cycles, 1, the inner loop back and be responsible for each digital comparator beginning from the first number, if they are smaller than this digital exchanged position, continue with this number and the following figures, and if larger than this number, the purpose of this digital It does not move, the cursor down, 2, the number of outer loop is responsible for the inner loop, since each of the inner loop into the last are the largest, so the number of the next cycle is successively decremented
 



"" "

 

################## Bubble Sort ######################

# The first edition 
DEF bubble_sort (alist): 
    the n- = len (alist)
     for J in the Range (0, the n--1 ):
         "" " 
            This is a go, just go to the second to last digit on it, Therefore, the end is n-1, the end does not contain, 
            J = 0 the inner loop is stopped come Range 2-n-(0, n-1-) 
            J 1 = cycle stop is come n-3 range (0, n -2 ) 
            J = 2 circulation is stopped. 4 is a walk-Range n-(0,. 3-n-) 
            J = n-Range (n---J. 1) 
        "" " 
        for I in Range (n-l-- J):
             IF alist [ I]> alist [I +. 1 ]: 
                alist [I], alist [I + 1'd] alist = [I +. 1 ], alist [I] 

# Difficulty is the range this place, first write the inner loop, is to go round, the outer loop control rounds to go,

 

################## Bubble Sort ######################

# Second Edition 
DEF bubble_sort2 (alist):
     for J in Range (len (alist) -1,0, -1 ):
         "" " 
        Range (len (alist) -1,0, -1) 
        sequences that is produced : n-1 n-2 n -3 .... 1 
        layer or in the number of cycles of the control memory, 
        "" " 
        # J represents the number of each pass to be compared, are decreasing 
        for I in Range (J ):   # this control loop or in each cycle number of steps he go, 
            IF alist [I]> alist [I +. 1 ]: 
                alist [I], alist [I + 1'd] alist = [I +. 1 ], alist [I] 

# of these two methods, their worst time complexity is the square of n,

 

################## Bubble Sort ######################

# Third Edition: 

# For an ordered sequence, [1,2,3,4,5,6] 
# if you can simplify this process, how to optimize this program, 
# If you went to the end from the beginning, I do not need to swap, it shows once is enough, explanation is orderly, and 
# how to achieve, 
DEF bubble_sort3 (alist): 
    the n- = len (alist)
     for J in the Range (0, the n--1 ): 
        COUNT = 0
         for i in the Range ( l--n- J):
             IF alist [I]> alist [I +. 1 ]: 
                alist [I], alist [I + 1'd] alist = [I +. 1 ], alist [I] 
                COUNT + =. 1
         IF 0 = =COUNT:
             BREAK 

# This is the optimal time complexity of n, the worst time complexity is still n squared, 
IF  __name__ == " __main__ " : 
    li = [54,26,93,17,77,31,44 , 55,20 ]
     Print (Li) 
    bubble_sort (Li) 
    Print (Li)

 

################## Bubble Sort ######################

 

################## Bubble Sort ######################

 

################## Bubble Sort ######################

Guess you like

Origin www.cnblogs.com/andy0816/p/12348239.html