---- 1 algorithm experimental report

First,  the practice of title

The first question: binary search

Second, the  description of the problem

Value for n (1 <= n <= 1000), n is an integer of non-descending order, and to find the number x, x using binary search, where the subscript output x (0 ~ n-1) and the number of comparisons. If x does not exist, the output 1 and the number of comparisons.

Third, the  algorithm description

1. The key part of the algorithm description:

 

int binary_search(vector<int>v,int key)

{

          int left=1,right=v.size()-1,mid;

          while(left<=right)

         {

              mid=(left+right)/2;

              if(v[mid]<key) left=mid+1;

              else if(v[mid]>key) right=mid-1;

              else if(v[mid]==key) return mid;

         }

         return -1;

}

          2. Find the first sequences are ordered, from the middle to find, if less or more than an intermediate value, then the corresponding maximum and minimum index should change to an intermediate value plus one or minus one. Repeat until the corresponding value is found.

Fourth, the  algorithm time and space complexity analysis (analysis process have)

Time complexity: O (h) = O (log2n)

 

                 Set up a total of n elements,

        After the value of n while () by n, n / 2, n / 4, ..., n / 2 ^ k, since n / 2 ^ k to be rounded, and even if n / 2 ^ k = 1, can be obtained k = log2n .

                 Space complexity: 0 (n) = 1

       

                 He did not apply for other spaces.

Fifth,  feelings and experiences (for this practice of harvesting and doubts summarize)

  1. A group coding efficiency is higher than their own when a write. Because there is no longer a place card for a long time, there will be members explain each other.

 

         2. A deeper understanding of the dichotomy, I think it is easier to understand the dichotomy is also relatively easy to implement the algorithm, but requires dichotomy is stored sequentially, and data are sorted according to certain rules, requiring more, we when practical applications to be used flexibly according to demand.

 

         3. PTA errors sometimes confusing, we should improve the accuracy of how to answer it?

Guess you like

Origin www.cnblogs.com/coding-specification-of-Java/p/11568158.html