Chapter II Experimental Report

Source: Internet
Author: User

Chapter II Experimental Report

20171003172 Fraught

1.

7-2 rewrite binary search algorithm (20 points)

Source: Design and analysis of computer algorithms, Wang Xiaodong

Set A[0:n-1] is an ordered array, rewrite the binary search algorithm so that when X is not in the array, it returns the maximum element position of less than x and the minimum element position J greater than X. When the search element is in an array, I and J are the same, and all are the positions of X in the array.

Input format:

The input has two lines:

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

Output format:

The minimum subscript j for the largest element of the output less than x and the smallest element greater than X. When the search element is in an array, I and J are the same. Tip: If x is less than all values, the output:-1 0 if x is greater than all values, then the output: the value of the value N of n-1

2. Problem Description: Find the relationship between x and the elements in this array

3. Algorithm Description:

#include <iostream>

using namespace Std;

int BIN (int a[],int n,int key)

{

int left=0;

int right=n-1;

int t=0;

if (Key>=a[0]&&key<=a[n-1]) {

while (Left<=right)

{

int middle= (left+right)/2;

t++;

if (Key==a[middle])

{

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

return middle;

}

if (Key>a[middle]) left=middle+1;

else right=middle-1;

}

Int J;

for (j=1; j<n; j + +)

{

if (Key<a[j])

Break

}

cout<<j-1<< "" <<j;

}

if (key<a[0]) cout<<-1<< "" <<0;

if (key>a[n-1]) cout<<n-1<< "" <<n;

}

int main ()

{

int n,x;

cin>>n>>x;

int a[n];

for (int i=0; i<n; i++)

cin>>a[i];

BIN (A,N,X);

return 0;

}

First determine whether the element is within the range of the array, and then use the binary search to determine whether X is an array of elements, is the output middle middle; otherwise output j-1,j;

Then determine if x is less than the entire element of the array or is larger than the entire array

4. The time complexity sub-search method takes full advantage of the order relationship between elements, using the divide-and-conquer strategy to complete the search task in the worst case with O (logn) time. Every time a while loop is executed, the size of the array to be searched is reduced by half. So in the worst case, the while loop is executed O (Logn) times. An O (1) time is required for both inside and outside operations of the loop. The time complexity of the entire algorithm in the worst case is therefore O (Logn).

Spatial complexity: The spatial complexity of the binary search method is O (1). The spatial complexity of this algorithm is a constant, i.e. it can be represented as O (1) when it is not changed with the size of the amount of n being processed.

5. Experience:

The binary search method is faster and more convenient.

In the process of doing the problem, the case is wrong, and later found that the statement is wrong, and nothing else is too big a problem.

Chapter II Experimental Report

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.