Examples: tuple - three Numbers Comparative Solution and Bubble Act

1. The most basic solution: if - elif - else

 1 while True:
 2     a = int(input("The first num: "))
 3     b = int(input("The second num: "))
 4     c = int(input("The third num: "))
 5     if a > b:
 6         if b > c:
 7             print(a,b,c)
 8         elif a > c:
 9             print(a,c,b)
10         else:
11             print(c,a,b)
12     else:
13         if a > c:
14             print(b,a,c)
15         elif b > c:
16             print(b,c,a)
17         else:
18             print(c,b,a)
19 #################################################
20 D:\untitled\project2\venv\Scripts\python.exe D:/untitled/project2/day1/teryer.py
21 The first num: 3
22 The second num: 1
23 The third num: 2
24 3 2 1
25 The first num: 1
26 The second num: 3
27 The third num: 2
28 3 2 1

2. Method list of the minimum and maximum values:

. 1 the nums = []
 2  # define an empty list 
. 3  for I in Range (l, 4 ):
 . 4  # . 3 times the number of cycles 
. 5      nums.append (int (INPUT ( ' Please enter numbers {}: ' .format (I))))
 . 6  # to an empty list which individually add a digital nums 
. 7  the while True:
 . 8      MAX_NUM = max (nums)
 . 9  # selected from the list of maximum inside nums 
10      Print (MAX_NUM, End = '  ' )
 . 11  # outputs the the maximum value of 
12 is      nums.remove (MAX_NUM)
 13 is  #Removing the maximum 
14  # for selecting a maximum value for the residual values, and continues to remove a maximum value, until the last remaining element 
15      IF len (the nums) ==. 1 :
 16          Print (the nums [0], End = '  ' )
 . 17          BREAK 
18 is  # If the list length is 1, i.e. there is a list of elements when out while True loop 
19  # ####################### ########################## 
20 D: \ Untitled \ project2 \ Venv \ Scripts \ python.exe D: / Untitled / project2 / DAY1 / teryer.py
 21 is Please enter digits: 32
 22 is enter 2nd number: 5
 23 is Please enter numbers:. 11
 24 32. 5. 11 
 25  Process Finished with code 0 Exit
 26 is  # The method may be performed a plurality of digits ordered

3. The list is sorted sort () method:

. 1  # Coding GBK = 
2 the nums = []
 3  # Create an empty list 
. 4  for I in Range (l, 4 ):
 . 5  # for 3 cycles 
. 6      nums.append (int (INPUT ( ' Please enter digits {} : ' .format (I))))
 . 7 nums.sort (Reverse = True)
 . 8  # Sort the list () method, reverse = True for the descending order, ascending order by default 
. 9  Print (the nums)
 10  # ##### ########################################## 
11 D: \ Untitled \ project2 \ Venv \ Scripts \ python.exe D: / Untitled / project2 / DAY1 / teryer.py
12 Enter the first number: 21
 13 is Enter 2nd number: 33
 14 Please enter numbers:. 1
 15 [33 is, 21 is,. 1 ]
 16  
. 17 Process Finished with Exit code 0 
18 is # the method may be a multi- digit row

4. Bubble Act

  • Sort belongs exchange
  • Pairwise comparison size, swap places. Gulp to take up as blisters
  • Results are divided into ascending and descending
  • Ascending: the number n from left to right, numbered from 0 to n-1, comparing the index value 0 and 1, 0 if the index is large, the exchange of both the position, if the index is a large, no exchange. Continue comparison index values ​​1 and 2, a large value on the right. Until n-2 and n-1 Comparative finished, completed the first round of comparison. Comparing the second round from the index 0 to n-2, n-1 because the rightmost position is already the maximum value. And so on, each round will reduce the far right is not involved in the comparison, until leaving the last two numbers to compare.
  • Descending: Ascending and opposite
. 1 the nums = [1,9,8,5,6,7,4,3,2 ]
 2  # Create a list 
. 3 length = len (the nums)
 . 4  # calculates the length of the list 
. 5 count_swap = 0
 . 6  # number of exchanges 
7 = COUNT 0
 . 8  # iterations 
. 9  for I in Range (length):
 10  # several elements on several comparison cycles 
. 11      for J in Range (I--length. 1 ):
 12 is  # per cycle once finished fixing a maximum number on the right side, on the number of the remaining sub-robin length-i 
13  # -1 boundary problem, because there is a boundary below. 1 + J 
14         =. 1 + COUNT
 15          IF the nums [J]> the nums [J +. 1 ]:
 16              tmp = the nums [J]
 . 17              the nums [J] = the nums [J +. 1 ]
 18 is              the nums [J +. 1] = tmp
 . 19  # bubbling the core idea of the best value exchange 
20              count_swap + 1 =
 21  Print (nums, count_swap, COUNT)
 22  # ########################### ###################### 
23 D: \ Untitled \ project2 \ Venv \ Scripts \ python.exe D: / Untitled / project2 / DAY1 / teryer.py
 24- [. 1, 2,. 3,. 4,. 5,. 6,. 7,. 8,. 9] 25 36
 25  
26 is Process Finished Exit with code 0

Imagine, is given if the nums [1,2,3,4,5,6,7,8,9] such a list, lines 15 to 18 is not completely performed, i.e. no switching frequency value of count_swap It is 0, but the number of iterations and a count nums lists, without once less, still needs, 2 and 3 takes a maximum value for comparison 1 and 2, 2 and 3 takes a maximum value 3 and 4 compare ...... count = 36, there is no way to reduce the number of comparisons that make it? We can make a label flag, such as following the code, count = 8, efficiency has improved a lot.

. 1  # Coding GBK = 
2 the nums = [1,2,3,4,5,6,7,8,9 ]
 . 3 length = len (the nums)
 . 4 count_swap = 0
 . 5 COUNT = 0
 . 6 In Flag = False
 . 7  # provided a tag flag, the initial value Flase 
. 8  for i in Range (length):
 . 9      flag = False
 10  # each cycle i are reconstituted flag value, nums = [1,2,3,4,9,6,7, 8,5] when viewed clearly 
. 11      for J in Range (I--length. 1 ):
 12 is          COUNT +. 1 =
 13 is          IFthe nums [J]> the nums [J +. 1 ]:
 14              tmp = the nums [J]
 15              the nums [J] = the nums [J +. 1 ]
 16              the nums [J +. 1] = tmp
 . 17              In Flag = True
 18 is  # here means is, if all elements of the exchange, the value of the flag becomes True 
. 19              count_swap +. 1 =
 20 is      IF  not flag:
 21 is          BREAK 
22 is  # two lines mean if not in exchange element, then out of j for loop 
23 is  Print ( nums, count_swap, COUNT)
 24-  # ########################################## ### 
25 D:\untitled\project2\venv\Scripts\python.exe D:/untitled/project2/day1/teryer.py
26 [1, 2, 3, 4, 5, 6, 7, 8, 9] 0 8
27 
28 Process finished with exit code 

Bubble Act summary:

  • Bubble Act requires a round of comparison data
  • Determining whether a flag can be set to round the data exchange takes place, if the exchange has not occurred, sorting can end, if switching occurs to continue the next round of sorting
  • Sorting the worst case, the initial target sequence and the opposite sequence number Complete Coverage 1, ..., n-1 and the sum n (n-1) / 2
  • The best situation is sorted, and the target sequence identical to the original sequence, n-1 times to traverse
  • Time complexity of O (n-th power of 2)

 

Guess you like

Origin www.cnblogs.com/linfengs/p/11714595.html