Half-fold search (multiple implementations of different half-fold algorithms)

Source: Internet
Author: User

Basic Idea of half-Lookup

Binary Search is also called binary search. The basic idea is: in an ordered table
Comparison object. If the key code of the record to be searched is equal to the key code of the intermediate record
If the key code of the intermediate record is smaller than the key code of the intermediate record, the key is located in the left half of the intermediate record.
Continue to search. If the key code of the record is greater than the key code of the intermediate record
Continue searching. Repeat the above search process until the search is successful or there is no search in the ordered table
Failed to search.


Different from the half-writing method, we can write down the three Program

1. The central point is mid = (low + high)/2.
2. Set the center axis to int mid = (high-low)/2.
3. Recursive writing

Okay. If you leave it empty. For details, see Code Right.

But before we start, let's assume that we have an order number.

Int [] SER = new int [] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 18, 50 };

Bytes ----------------------------------------------------------------------------------------------------------------------

/// <Summary>
/// Mid = (low + high)/2
/// </Summary>
/// <Param name = "S"> ordered array </param>
/// <Param name = "v"> value to be searched </param>
/// <Returns> </returns>
Private string search (INT [] S, int V)
{
Int mid = 0;
Int high = S. Length-1;
Int low = 0;
While (low <= high)
{
Mid = (low + high)/2; // The low boundary + high boundary is divided by 2, that is, the average value is obtained. The calculated value x must be at low <= x <= high

If (s [Mid] = V)
{
Return s [Mid]. tostring (); // if the current value is equal to the value to be searched, the value found is returned.
}
Else
{
If (s [Mid]> V)
{
High = mid-1; // if the current value is greater than the value to be searched, it indicates that the value to be searched is before the current value.
}
Else
{
Low = Mid + 1; // otherwise, it indicates that the value to be searched is behind the current value.
}
}
}

Return "not found"; // if no value is returned after the above tossing, it indicates that no value is found
}

 

 

Bytes --------------------------------------------------------------------------------------------------------------

//


// The center axis is int mid = (high-low) /2
//
// ordered array
// low boundary
// high boundary
// value to be searched
//
private string search (INT [] S, int low, int high, int v)
{

While (low <= high)
{
Int mid = (high-low)/2;

If (s [Low + mid] = V)
{
Return "s [" + (low + mid ). tostring () + "] =" + s [Low + mid]. tostring () + "V =" + v. tostring ();

}
Else
{
If (s [Low + mid]> V)
{
High = low + mid-1;
}
Else
{
Low = low + Mid + 1;
}
}
}

Return "not found ";
}

 

Bytes ---------------------------------------------------------------------------------------------------------

/// <Summary>
/// Recursive writing
/// </Summary>
/// <Param name = "S"> ordered array </param>
/// <Param name = "low"> low boundary </param>
/// <Param name = "high"> high boundary </param>
/// <Param name = "v"> value to be searched </param>
/// <Returns> </returns>

Private string search (INT [] S, int V, int low, int high)
{
Int mid = (low + high)/2;

If (low <= high)
{
If (s [Mid] = V)
{
Return s [Mid]. tostring ();
}
Else
{
If (s [Mid]> V)
{
High = mid-1;

}
Else
{
Low = Mid + 1;
}
Return search (S, V, low, high );
}
}

Return "not found ";
}

 

Bytes --------------------------------------------------------------------------------------

Well, let's write so much .....

 

 

 

 

 

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.