Binary search really that simple? - Chapter II algorithm Shangjishijian report

A practice topic

Rewrite the binary search algorithm  (20  points )

Source title: "Design and Analysis of Computer Algorithms", Wang Xiaodong

Set a [0: n-1] is an array row have good sequence, rewrite the binary search algorithm , such that when x is not in the array, return the position of the largest element of x is less than i and greater than a minimum J x element of the position. When searching for an element in an array, i and j the same, both the position x in the array.

Input formats:

Enter two lines:

The first row is n and the value of x; the second line is a non-descending sequence of n different integers, separated by spaces between each integer.

Output formats:

Subscript j is less than the minimum x maximum output element and the maximum index i is greater than the smallest element of x. When searching for an element in an array, i and j same. Tip: If x is less than the full value, the output: -1 0 if x is greater than all the values, the output: the value of n-1, n

Sample input:

Here we are given a set of inputs. E.g:

6 5
2 4 6 8 10 12

Sample output:

Given here corresponding output. E.g:

1 2

 

Second, the description of the problem

Of the binary search algorithm improvements, known to be already sorted array, the data x input through the binary search algorithm improved, first determine x is an array larger than the given (in the right array) or small (on the left array ), located within or size of the array, the array if there are x where x is the output of the array subscript or output index index and x is greater than the minimum number of maximum numbers smaller than x.

Binary search to find x array subscript in the array, just improve it, so that it can get the results in the case of x is not in the array on it.

 

Third, the algorithm description

1、  if(x<a[0]) cout<<"-1 0\n";

    else if(x>a[n-1]) cout<<n-1<<" "<<n<<"\n";

    else BinarySearch(a,x,n);

 

Determines the maximum x is greater than or less than the minimum array of arrays, binary search algorithm, if the middle, improved call

 

2, binary search (modified)

 

void BinarySearch(int a[],int x,int n){

    int left = 0;

    int right = n-1;

    int flag=0;

    while (left <= right){

        int middle = (left+right)/2;

        if (x == a[middle]){

            cout<<middle<<" "<<middle<<endl;

            return ;

        }

        if (x > a[middle]){

            left = middle+1;

        }

        else {

            right = middle-1;

        }

    }

    if(a[left]>x) cout<<left-1<<" "<<left<<endl;

    else if(x>a[left]) cout<<left<<" "<<left+1<<endl;

    return;

}

 

Both ends of the array to take digital left, right, i.e. when the right is less than the left and the presence of more than one array element, the array intermediate obtained to obtain a digital loop (median, sorted), the intermediate obtained if the number is x, then the direct output x small scale, or narrow range (half and half shrink, if x is greater than the number of intermediate, then a recursive call to the function, into the intermediate-range to the right, and vice versa for the left range to the calling dichotomy algorithm intermediate number);

Because it is an array of good order had already been booked, if not in the array x, then x in the array is left and right sides of the figures required to answer. So when half of the algorithm stops, simply comparing the current "left" of that number and size of x will be able to determine the minimum number larger than x and x is less than the maximum number of array index. If x is greater than the left so that the left two and the number of left-1, and vice versa for the left and left + 1.

 

Fourth, the complexity of time and space analysis algorithm

 

void BinarySearch (int a[],int x,int n){

    int left = 0;

    int right = n-1;

    int flag=0;

    while (left <= right){

        int middle = (left+right)/2;

        if (x == a[middle]){

            cout<<middle<<" "<<middle<<endl;

            return ;

        }

        if (x > a[middle]){

            left = middle+1;

        }

        else {

            right = middle-1;

        }

    }

    if(a[left]>x) cout<<left-1<<" "<<left<<endl;

    else if(x>a[left]) cout<<left<<" "<<left+1<<endl;

    return;

}

 

Binary search algorithm time complexity analysis:

Assumed that the data size is N (i.e., right-left during each call), the number of comparisons the program execution is represented by C (N), is assumed to find a program data does not exist, the number of times is performed at this time most :

 

There is performed the first time: (x represents a comparison is performed and a [middle], the value of the right-left algorithm is called once when the (N / 2) on behalf of, i.e., the size of each new data less than half)

 

Assumes a total of n elements, then once for each two minutes after the interval size is n, n / 2, n / 4, ..., n / 2 ^ k (Next, the operation of the remaining number of elements), where k is the cycle frequency. In the worst case after K-half times, and finally down to the size of each sub-section 1, to find the element you want.

So that n / 2 ^ k = 1

Available k = log2n, (based on base 2, n for logarithmic), it may represent a time complexity O () = O (logn)

 

Binary search algorithm space complexity analysis:

Using the iterative algorithm, the hands in the original array, cyclically changing the range of the array, there is no other auxiliary space applications, the space complexity is a constant level, is O (1).

 

Fifth, feelings and experiences

Hit a binary code search is not difficult, it is very simple and very basic algorithm, This question is difficult to improve on. To find rule: if x is not in the array, x is less than the maximum number and minimum number of points is two points to the last element of the left or right, and it is greater than x, because the array is an ordered sequence. Beginning to lay the framework for binary search, and then divided into many cases, the thought of this and took some time to achieve, but also started to play a particularly complex code, in fact, many places are long-winded repeat the operation, delete the teacher reminded some useless code, especially the improved refreshing.

So the code is not difficult to play, hard thinking about the solution to the problem. Once on shore, as well as playing their own code to be Duokanjibian code, analyzing every line of every step is doing, what changes and use, you even see reeling solution is not good, so in order to improved and simplified for the first time to break out the code.

Guess you like

Origin www.cnblogs.com/990924991101ywg/p/11563859.html