Coco: I haven't seen you for a long time, I really want everyone. Due to someone's lazy, seriously affects my popularity.
I: I am so embarrassed, I have been a cold, but let you promote "some unknown respiratory infectious disease", I almost isolated.
Coco: Don't isolate you, how can you let you write an article?
Me: Isolated me also recognized, you will actually have a rumor to say that my condition is infected on QQ. Do you want me to be isolated? Is it unable to access the Internet? When will the infectious disease of human beings will be transmitted over?
Coco: So I said that you are CIH ~
I: @ # $% ^
When can people can CIH ~
Coco: Is it impossible?
Me: Is it possible?
Coco: Is it impossible?
Me: Is it possible?
Coco: Is it impossible?
Me: Is it possible?
COCO: I just explore, don't be so excited, is it impossible?
Me: If I can never in the CIH, I can find someone to format me again.
COCO: Don't forget to install Linux, say this East, I haven't used it yet.
I: Hello, let's go on so, and the space is wasted.
Coco: Ok, wear a mask, continue to work. What do we play this time?
(Play ... is really a word, I have to pack you into a positive good youth, this everyone knows that you learn Python is for fun ...)
Me: We play this time ... wrong, it is to learn a very kind sort algorithm, bubble.
Coco: Is this too simple? It seems that many people have written the second program after Hello World is this.
I: This is not fake. The characteristics of bubbling sorting are very simple. Basically all the language of process control capabilities can implement it, and it is also very easy to learn, it can be said that this is the "Hello World" of the algorithm class. The implementation in Python will not be more complex than other languages. Now you write a bubbling to order the example arrays of us.
COCO: Without your day, I wrote some programs for my loneliness, including this bubbling ~
I: Hello ~, don't say that the meat is good? Let people think that we have any relationships of nothing ... first take the program to give me a look.
Def Bubsort (TheInput):
#C = 0
#e = 0
For i in range (len (theInput):
For J in Range (1, Lenput) - i):
IF theinput [j-1]> theinput [J]:
TheInput [J-1], TheInput [J] = THEINPUT [J], THEINPUT [J-1]
#e = E 1
#C = C 1
#print C
#print e
Return
#Follow is the demo of bubble Up Sort.
Array = [6, 16, 10, 9, 15, 5, 11, 1, 19, 4, 14, 17, 18, 0, 13, 3, 12, 2, 8, 7)
Print Array
Bubsort (Array)
Print Array
Coco: The code in those comments seem to have nothing to do with me? I: This is what I gave you. Your code itself has no problem, it is good. I added these code to calculate how many operations have been made in the sort. As long as you remove the code line of the C, you can calculate how many times have been exchanged, and the code line information about E is removed, you can calculate how many times have occurred.
COCO: It seems to be very convenient, I will try it. Only 20 elements are listed in 190 times and 108 exchanges.
Me: In fact, in this program, the number of times is only related to the number of elements, the number of comparisons of n elements is N * (n 1) / 2. Even a complete sequence, an array of exchanges is not required.
COCO: I use [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 18, 19] ... It's so like this. Is this not done a lot?
I: Yes, do you have any good way to this problem?
COCO: I want to remember the last time in the last time in the traversal, just searched this location until the next time, I will try a sign in the program.
DEF MRKBUBSORT (THEINPUT):
# C = 0
# E = 0
i = 0
Bottom = len (theInput)
While i i = 0 M = true For J in Range (1, Bottom): IF theinput [j-1]> theinput [J]: TheInput [J-1], TheInput [J] = THEINPUT [J], THEINPUT [J-1] M = false Bottom = J # E = e 1 # C = c 1 IF M: Break i = i 1 # Print C #Print e Return #Follow is the demo of bubble Up Sort. Array = [6, 16, 10, 9, 15, 5, 11, 1, 19, 4, 14, 17, 18, 0, 13, 3, 12, 2, 8, 7) Print Array MRKBUBSORT (Array) Print Array COCO: After taking the counting code of the comment, you can know that 178 comparisons have occurred in the same array, it is indeed less. Me: If you try some "extreme" data, some interesting ideas will be observed. For example, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17, 18, 19), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 0), [19, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17, 18], [19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0], etc. Coco: The following is the output result, in each set of output results, the first group is the original number group, the single integer of the next line is the number of times, the next is the number of exchanges, the array of the last row is sorted. >>> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19) 19 0 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 18, 19) >>> >>> [19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0] 190 190 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 18, 19) >>> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17, 18, 19, 0) 190 19 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 18, 19) >>> >>> [19, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18] 37 19 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 18, 19) Coco: It is indeed some problems, for example, if there is an inverse order of the array, even if the previous data has been ranked, it takes 190 exchanges. Is there any way to solve it? I: Tip, if the reverse is starting? Coco: Well, only 37 comparisons are required. It seems that the number of comparisons is related to the direction of operation. I: So, if you alternately sort from both ends, you can avoid unnecessary comparison operations. This algorithm we call it. You try to implement it. (After a long time ...) Def shkbubsort (theInput): C = 0 e = 0 l = len (theInput) TMPT = 1 TOP = 0 TMPB = L Bottom = TMPB While Top IF TOP TOP = TMPT Else: TOP = TOP 1 Bottom = TMPB M = true For J in Range (Top, Bottom): IF theinput [j-1]> theinput [J]: TheInput [J-1], TheInput [J] = THEINPUT [J], THEINPUT [J-1] M = false TMPB = J E = E 1 C = C 1 IF M: Break For K in Range (TOP 1, BOTTOM): Cur = L - K IF Theinput [Cur - 1]> TheInput [CUR]: TheInput [Cur-1], Theinput [Cur] = TheInput [Cur], TheInput [CUR-1] M = false TMPT = CURE = E 1 C = C 1 IF M: Break #Print top, Bottom Print C Print E Return Coco: There are more troublesome than I think. What is the actual effect? Let me try. >>> [19, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18] 54 19 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 18, 19) >>> [6, 16, 10, 9, 15, 5, 11, 1, 19, 4, 14, 17, 18, 0, 13, 3, 12, 2, 8, 7) 169 108 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 18, 19) >>> >>> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17, 18, 19, 0) 54 19 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 18, 19) >>> [19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0] 190 190 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 18, 19) COCO: In general, it seems to be some improvement, but it is not very obvious. Me: In fact, the most critical is, no matter how it improves, it will not change the shortcomings of this series of algorithms exchange operations. In the sort operation, the cost of the write operation is much higher than the read operation, so no matter how to reduce the number of comparisons, it is impossible to improve the efficiency of the sorting. Coco: Hey, the cost is so big, learn a unabilized algorithm, it's really awkward ... I: Of course, learn this algorithm ... Coco: OK, OK, I know that you have to say, mainly to let me practice, but now I only open a face every month, no matter how many complicated procedures, I can't really effectively improve my proficiency. You should also work hard ~ I: In fact, I haven't let everyone wait for this month. I have been writing the eleventh episode of "SQL Story", and now I have basically solved all the problems and will be completed soon. Coco: ~, there is no chance to let me play, disappointed, I don't know where to see you again, I will miss you ...