Saturday, June 20, 2015

[Programming book Chapter Eight] binary search.

Let's start with a simple game. It needs two to play. I think one will find a number. Some of the questions that will get the number of the latter. However, your number? " - A question but can not be direct. The question is: What number N (a number) of greater than or equal to small? The number but would be in a certain range (eg 1 to 100, 10, 1000, from -1000 100000). Now, let's say, first, that the issue has a number from 1 through 1000. But most of the times equal to the number of N- What? ' It can be out of the question. (What is the number 1? Number 2? ... What number is the number 999 ?, 1000?). If the number must be out of the question. However, the question to be bad luck if a thousand times. But we do not have so much time. Suppose that in 1000 the number from 1 to 50. Our first question then would be: 1) The number 500 is greater than, equal to or smaller? Small. Ii) the number 250 is greater than, equal to or smaller? Small. 3) The number 125 is greater than, equal to or smaller? Small. 4) The number 62 is greater than, equal to or smaller? Small. 5) greater than the number 31, is equal to or smaller? Large. 6) What number is 46 greater than, equal to or smaller? Large. 7) The number 54 is greater than, equal to or smaller? Small. 8) What number is greater than 50, equal to or smaller? Equal. We just got the number of the eight questions! Surely you have to understand the procedure? Each time the number of questions that are in the range has been cut to half. The game was the beginning of the time limit from 1 to 1000. Then it is 1 to 500. Then from 1 to 250, 1 to 125, 1 to 62, 31, 62, 46, 62, 46 and 54. This process binary search to find the number. Let's try to write the algorithm: Binary Search (low, high, N): (at the beginning of the three of us will know the number, the lower the number (low), upper limit (high) and the number (N)) Step 1: mid = (low + high) / 2 Step 2: If the value of the mid and N- is equal to the step 5. Step 3: If the N, mid- greater than if it is at low = mid + 1. Step 1. Step 4: If the N, mid- smaller than it is, then high = mid - 1. Step 1 seconds. Step 5: the number of m (mid). Now we will see any number from an array of binary search is to find out how. But the numbers of array from small to large or small order will be. Otherwise binary search can not be used. Can anyone tell me what is the cause? First, we assume an array of integers, where the numbers are arranged in sequence from small to large. int ara [] = {1, 4, 6, 8, 9, 11, 14, 15, 20, 25, 33 83, 87, 97, 99, 100}; Now tell me how much the value of low and high-? low = 1 and high = 100? But here's the catch right there is a little problem. We do not have to find all the numbers here, but have to find in the array index. Because the array is an array of binary search is inadeksagulo sequence. Here ara- lowest index of 0 and a maximum of 15 indexes. If we let the two variables - low_indx = 0; high_indx = 15; Suppose that the number of T 97. num = 97; Many of you may be thinking, num the number is not on the ara- What will happen then? We also take a look. If the number is not found, then it must be extended to our program. Since the task of finding us again and again that, we will have to use a loop. We will be searched and the number of the loop when (or if you are sure that the numbers are not there), we'll be out of the loop. We tried to write binary search program. The program is very simple. Lupati will continue until you find the number, because we wrote while (1) and 1 all the time, really. But if the number is not on the ara- lupati will continue and will not stop in our program. So it needs to be a system. Well, we understand how that number is on ara-? You have already noticed that we were to continue the search in the simata half. If this trend continues, there will be a time within the range. And will be equal to the value of high- and low. Since each of the low- and high- value is increasing at decreasing the value, equal to the yebara low and high, the next time will be better than the quality of low- to high- value. And we understand that the number could not be found. So long low <= high as lupati run. I was out of the loop if low> high, then I know that the number could not be found, and found not only the number and value of bujhaba ara [mid_indx]. You will work in a different function for the write binary search.

No comments:

Post a Comment