Python relationship between the bubble sort function and range

Title-depth analysis of the reasons implement bubble sort algorithm

Bubble algorithm is a classical algorithm programming languages, is also relatively simple, but this algorithm can improve your understanding of the structure of programming languages, then this algorithm in the end what secret? How specific is achieved? Why should count it? Below, I'll explain one by one.
First, referring to bubbling algorithm is a sequence of a predetermined integer unordered list usually called in Python, usually called List in java, arrays, so that the sequence listing is arranged in a certain order, typically incremented or decremented, thereby forming a new ordered list, it refers to a regular and orderly. Then the rule is to increment (decrement) arrangement.
Here the code.
= LST [10,8,6,66,22,4,8,93,2,0,55,76] #. 1
length = len (LST) # 2
Print (length). 3 #
count_swap #. 4 = 0
COUNT = 0 #. 5
for I in Range (length): #. 6
Print (I) #. 7
COUNT + =. 1 #. 8
for J in Range (0, length-. 1-I): #,. 9
IF LST [J]> LST [J +. 1]: # 10
count_swap + =. 1 #. 11
LST [J], LST [J +. 1] = LST [J +. 1], LST [J] # 12 is
Print ( 'exchanging digital number =', count_swap) # 13 is
Print (of the type (lst), lst) # 14
Print (length) # 15
# 1 given a list of names for the lst
# 2 is set to accept a variable named length len (), is the length of the list, is tantamount to telling us how many elements in the list, len () function is a built-in function returns the number of elements the object
# 3print test
# 4 defines an initialization variable, this is a zero-based variable increment of
# 5 above step
# 6 this is a cycle, the key bubbling in this range function, which has three parameters, start position, end position, step length, secret that the starting position is incremented to a value of 1, this parameter is typically the starting position can be omitted, showing scratch, plus 1 each cycle, until the one end position, i.e. not including the end position, the step is also typically is omitted, the default is a step length. Our list of default is zero-based index until the end, therefore, it can traverse the list () to obtain a list of index by len, set into the range function.
# 7print Test
# 8 increases a count each cycle, in order for counting the number of cycles.
# 9 This is an inner loop, since, the objective is to start from scratch, not including the end, it is necessary to control the number of cycles does not exceed the limit subscript, len is the limit (), and in a tenth step following step i.e. pairwise comparison later, after the end of a cycle, the left-most digit has been dynamically switched to the far right, and therefore need to length-1-i, which is to improve the efficiency, it does not require each full list traversal, therefore, -i can not write but it will reduce the efficiency of the algorithm, the first parameter is usually zero by default can not write
# j + 1 is a comparison with the index 10 cycle obtained by the subscript a ninth step, if the standard is greater than one, then the implementation of the tenth step
# 11 exchange positions of the two elements, in Python, we often think that writing is correct. For example:
in Python
A =. 1, B 2 =
A, B = B, A
Print (A, B)
That is, in a general programming language, exchange two variables, intermediate variables require realization by, Python this step is omitted.
# 12 cycles, the same principle. 7
# 13 is Print Test
# 14 print result type and a list of the new generation of

Published 13 original articles · won praise 0 · Views 309

Guess you like

Origin blog.csdn.net/alwaysbefine/article/details/104693432