Experimental reporting requirements choose one topic for analysis. content include:
- Practice topic
- Problem Description
- Algorithm Description
- Time and space complexity algorithm analysis (analysis process have)
- Feelings and experiences (for this practice of harvesting and doubts summarize)
The practice of a total of three questions, I chose to analyze the first question.
1. Practice questions asked:
Input formats:
The input common three lines: The first line is the value of n; n is an integer second line; the third line is the x value.
Output formats:
Where the subscript x output (0 ~ n-1) and the number of comparisons. If x does not exist, the output 1 and the number of comparisons.
2. Description of the problem:
It is simply in a non-descending order list to find the data you want to find, and compare the number of output index, using the methods specified dichotomy
Binary basic idea is to find the n elements is divided into two approximately equal portions, to take a [n / 2] x and compared, if x = a [n / 2], x is found, the algorithm is aborted. If x <a [n / 2], as long as the left half of the array a continuing search for x, if x> a [n / 2], the search for the right half of x as long as the array a. Such has been the search continues
3. Algorithm Description
3.1 Find interval counter initial value, left is 0, right to n-1
3.2 or less when left right, execute the following cycle of operations:
- middle value is the middle value of the left and right;
- Recording the number of cycles counter + 1.
- The setting position of the recording intermediate key x are compared, if equal, the search is successful, return to the neutral position Middle;
- If not equal, the recording sheet by the intermediate position into the former, the two sub-tables. If the key is smaller than the intermediate position of the recording of x, is taken as the left middle-1, otherwise it is taken right middle + 1.
3.3 cycle ends, the instructions to locate the interval is empty, then the lookup fails, returns 0.
Code Description:
int Binsearch(int array[], int n, int x){ int left = 0; int right = n - 1; int counter = 0; while(left <= right){ int middle = (left + right) / 2; counter++; if(x == array[middle]){ cout << middle << endl; cout << counter; return middle; } if(x > array[middle]){ left = middle + 1; } else right = middle - 1; } cout << "-1" << endl; cout <<counter; return -1; }
4. The time and complexity of the spatial analysis algorithm
Time complexity: a total of n elements, once for each section is the size of n, n / 2, n / 4, ..., n / 2 ^ k (Next, the operation of the remaining number of elements), where k is the cycle frequency. Since n / 2 ^ k after rounding> = 1, and even if n / 2 ^ k = 1, can be obtained k = log2n, (based on base 2, n for logarithmic), it may represent a time complexity O () = O (logn)
Space complexity: since in an array can be completed all the work, so that the space complexity of O (1)
5. feelings and experiences (for this practice of harvesting and doubts summarize)
Harvest is I am more familiar with the binary search algorithm
There are small details need to pay attention to some of the code:
- Functions can not return value of the function,
- Find the names of some variables such as the number of variable records should note, the best counter counts the class, boolean with flag