Algorithm Learning (10), algorithm Learning (
1. Binary Search (Binary Search)
(Binary search is a common programming task, because it is used to search for sorting arrays (which is why we learn to sort) and to solve mathematical equations.
Our goal is to solve the following equation:
A * x + B * sqrt(x ^ 3) - C * exp(-x / 50) - D = 0
Here both a B and C are positive, so this function is monotonous. The solution of x must be in the range of 0 to 100 (0 <= x <= 100).
The solution must be accurate to 0.0000001 = 1e-7 or better.
Input data: the first line contains the number of test cases.
The following rows contain four numbers for each test case, that is, the four values of a B C D are separated by spaces.
Answer: It should include the solution-that is, the value of x, which satisfies the given equation-several answers (for several test cases) should be separated from spaces.
For example:
input data:20.59912051 0.64030348 263.33721367 387.9206961715.68387514 1.26222280 695.23706506 698.72384731answer:73.595368554162 41.899174957955
Test data:
69.20675416 0.87395008 1667.33525026 495.835818301.87353375 1.61305979 1376.91480917 1153.471755354.66566950 0.67303113 366.84269196 434.6881207515.67052619 0.48875617 714.77221172 -264.2399346314.69705865 0.69917802 297.42862112 1568.0658576018.22457610 0.50751334 784.28709171 1720.63987051
The Code is as follows:
1 import math # math module 2 test_case = int (input () # Number of test cases 3 for I in range (test_case): 4 case = input (). split () # obtain A, B, C, D 5 A = float (case [0]) 6 B = float (case [1]) 7 C = float (case [2]) 8 D = float (case [3]) 9 def func (x): 10 return A * x + B * math. sqrt (x ** 3)-C * math. exp (-x/50)-D # create A required equation, which is substituted into A, B, C, the value of D is 11 low = 012 height = 100.013 n = low + (height-low)/2.0 # calculates the value of 14 m = 100 # Because it is found that the Code has an infinite loop, after the analysis, all the solutions can be solved after 100 cycles, all set 100 cycles 15 while func (n )! = 0 and m> 0: # When the equation is not equal to 0, the start cycle may be because n is a decimal, and all equations can only be close to 0, not equal to 0, this causes an infinite loop 16 if func (n)> 0: # So add a 100 limit on the number of cycles to avoid an infinite loop 17 height = n-1.018 low = low19 elif func (n) <0: 20 low = n + 1.021 height = height22 n = low + (height-low)/2.023 m-= 1 # reduce one cycle by 24 print (n, end = '') 25 26 output: 61.34503889699867 84.37226003243683 56.756393017339064 15.062697470552255 78.1010605734923 82.05365393491547
Finally, I would like to ask you what other methods can be used to avoid wireless loops.
2. Selection Sort (select sorting method)
NOTE: If we have an array of N values and want to sort them, just like Bubble Sorting:
[3, 1, 4, 1, 5, 9, 2, 6, 5, 3]
The method is as follows:
Locate the position of the largest element in the entire array (in the above example, 5 is the index with the maximum value 9 );
Swap this element with the last element (because it should be the last in the sorted array)-that is, the location N-1;
Now consider the child array whose length is N-1, with no last value (already "in the correct position ");
Locate the location of the largest element in this sub-array (that is, the second largest element in the entire array)-Now it will be index 7 (where the value of 6 is located );
Swap it with the last element in the Child array (for example, location N-2 );
Now consider the child array of the length N-2 (without two final elements)-execute the following selection and swapping, and so on;
When "sub-array" is reduced to 1, the algorithm ends.
Example:
[3, 1, 4, 1, 5, 9, 2, 6, 5, 3] - max is 9 at position 5, swap 5-th with 9-th[3, 1, 4, 1, 5, 3, 2, 6, 5], 9 - max is 6 at position 7, swap 7-th with 8-th[3, 1, 4, 1, 5, 3, 2, 5], 6, 9 - max is 5 at position 4, swap 4-th with 7-th - they are equal![3, 1, 4, 1, 5, 3, 2], 5, 6, 9 - max is 5 at position 4, swap 4-th with 6-th[3, 1, 4, 1, 2, 3], 5, 5, 6, 9 - max is 4 at position 2, swap 2-th with 5-th[3, 1, 3, 1, 2], 4, 5, 5, 6, 9...[1], 1, 2, 3, 3, 4, 5, 5, 6, 9 - subarray of length 1 is reached, stop an algorithm.
Analysis:
This algorithm requires N to pass through the array, and it performs N/2 operations on average each time it passes, so it has O (N ^ 2) "Time Complexity", the same as Bubble sorting. However, unlike the bubble sort, it does not execute n/2 exchanges in each pass, but only needs to perform one exchange. This makes it work faster. For large datasets, more
This tricky algorithm.
You can select the minimum value instead of the maximum value, and then exchange them with the beginning of the array. Similarly, based on the maximum value (or minimum value) on the left or right, the sorting may be stable or unstable, this means it is important to either retain or not keep the order of equal elements when we sort objects, not numbers.
Problem description:
You need to implement the algorithm described above, and print the index of the selected maximum value upon each transfer.
Input data: the size of the array contained in the first row.
The next row contains the array itself (all elements are different ).
Answer: It should contain the index (N-1 value) for each maximum passed ).
For example:
input data:631 41 59 26 53 58answer:2 2 2 1 0
Test data:
12854 28 159 13 163 41 137 160 4 191 129 173 61 44 102 135 161 32 130 16 53 142 38 5 26 172 196 190 199 51 149 15 150 175 45 153 166 127 27 17 30 162 178 92 193 113 34 117 39 57 11 104 24 118 183 93 60 2 87 19 84 49 145 140 59 174 22 78 29 131 73 35 36 66 58 154 50 31 48 157 107 79 12 98 95 42 182 85 99 156 106 74 169 184 103 116 151 105 152 146 180 21 126 119 33 96 197 133 72 25 114 110 189 6 55 170 63 67 186 192 65 64 90 177 136 158 125 109
The Code is as follows:
Test_case = int (input () # test case data = input (). split () # delimiter array = [] for I in data: array. append (int (I) # converts a character to an integer for I in range (test_case-1): max_index = array. index (max (array) print (max_index, end = '') # print index last_elem = array [-1] array [-1] = max (array) # swap the maximum value and the last value array [max_index] = last_elem array = array [:-1] # Return the new array output: 28 106 26 44 119 9 27 112 118 93 54 86 100 42 93 33 65 11 25 100 92 36 4 41 16 7 2 26 79 75 35 89 35 32 30 79 62 21 63 6 44 15 30 69 18 10 37 7 16 53 47 21 11 45 65 28 37 62 47 51 35 14 63 37 4 30 55 43 9 43 6 30 10 47 7 28 26 30 11 33 27 44 12 12 14 21 26 12 0 20 29 10 10 10 13 15 5 13 22 13 15 4 14 17 17 17 0 1 12 15 13 11 7 2 6 2 1 6 3 0 0 3 0 1