Hdu2964-Prime (Bases)

Source: Internet
Author: User

Implement binary search and implement binary search using standard library functions

 

 


Algorithm ideas: 1. Sort arrays (from small to large); 2. Compare each time with the number mid in the middle. If they are equal, they can be directly returned,

If it is larger than the mid value, search for the larger side; otherwise, search for the smaller side.

 

Input: sorted array-arrayname [], array size-array_size, searched value-searchnumber

Return: locate and return its position in the array; otherwise,-1 is returned.

 

Binary Search code implementation

Int binary_search (int arrayname [], intarray_size, int searchnumber)

{

Intlow = 0, high = array_size-1, mid;

While (low <= high)

{

Mid = (low + high)/2; // obtain the intermediate position

If (arrayname [mid] = searchnumber)

Returnmid; // returns the corresponding location

If (arrayname [mid]> searchnumber)

High = mid-1; // if it is greater than the key, search for the lower part.

Else

Low = mid + 1; // if it is smaller than the key, search for it in a higher position.

}

Return-1;

}

Sample Code:

(1) Sort unordered Arrays

(2) binary search for the number to be searched

(3) Display Search Results

# Include <iostream>

# Include <algorithm>

Using namespace std;

Int binary_search1 (int arrayname [], intarray_size, int searchnumber)

{

Intlow = 0, high = array_size-1, mid;

While (low <= high)

{

Mid = (low + high)/2; // obtain the intermediate position

If (arrayname [mid] = searchnumber)

Returnmid; // returns the corresponding location

If (arrayname [mid]> searchnumber)

High = mid-1; // if it is larger than searchnumber, search for the lower part.

Else

Low = mid + 1; // if it is smaller than searchnumber, search for it in a higher position.

}

Return-1;

}

 

Int main ()

{

Int a [10] = {9, 6,}, I = 0;

Printf ("every element in the array before being sorted! \ N ");

For (I = 0; I <10; I ++)

Cout <a [I] <"\ t ";

Cout <endl;

Sort (a, a + 10 );

Printf ("every element in the sorted array! \ N ");

For (I = 0; I <10; I ++)

Cout <a [I] <"\ t ";

// Search for numbers in the array

I = binary_search1 (a, 10, 5 );

If (I)

{

Cout <"the position of the number to be searched in the array is" <I <endl;

Cout <"the number is" <a [I] <endl;

}

Else

Cout <"No such number! "<Endl;

// Search for numbers that do not exist in the array

I = binary_search1 (a, 10, 20 );

If (I> 0)

Cout <a [I] <endl;

Else

Cout <"No such number! "<Endl;

 

System ("pause ");

Return 0;

}

Use the binary search function in the standard library to implement binary search

# Include <iostream>

# Include <algorithm>

Using namespace std;

 

Int main ()

{

Int a [10] = {9, 6,}, I = 0;

Printf ("every element in the array before being sorted! \ N ");

For (I = 0; I <10; I ++)

Cout <a [I] <"\ t ";

Cout <endl;

Sort (a, a + 10 );

Printf ("every element in the sorted array! \ N ");

For (I = 0; I <10; I ++)

Cout <a [I] <"\ t ";

// Search for numbers in the array

 

If (binary_search (a, a + 10, 5 ))

{

Cout <"the number to be searched is in the array" <endl;

}

Else

Cout <"No such number! "<Endl;

// Search for numbers that do not exist in the array

If (binary_search (a, a + 10, 20 ))

Cout <a [I] <endl;

Else

Cout <"No such number! "<Endl;

 

System ("pause ");

Return 0;

}

 

Related Article

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.