Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Implement binary search algorithm in C language     - CentOS of NFS (Server)

- How to make GRub instead of the default Ubuntu software center (Linux)

- Linux user management (Linux)

- Jigsaw project will solve the problem of Java JAR hell Mody (Programming)

- Linux Fundamentals of the memory management mechanism (Linux)

- Database Blob data type conversion String (Programming)

- 12 Linux Process Management Commands (Linux)

- Lua study notes under ubuntu (Linux)

- Use this one-time password via SSH secure login Linux (Programming)

- Oracle RMAN repair logical bad blocks (Database)

- Using Ruby to build a simple HTTP service and sass environment (Server)

- Management DB2 logs (Database)

- CentOS6 installation configuration rsh (Server)

- Use backup-manager system backup on Linux (Linux)

- Linux Getting Started tutorial: Experience QEMU virtual machine articles (Linux)

- Linux common commands: nslookup, ls md5sum, uname, history, etc. (Linux)

- Commonly used Linux system camouflage method (Linux)

- FileZilla FTP && TLS connection settings of (Linux)

- Linux alpine use the command line to access Gmail (Linux)

- Android Studio Clear Project or Rebuild Project Error (Linux)

 
         
  Implement binary search algorithm in C language
     
  Add Date : 2018-11-21      
         
         
         
  Binary search algorithm thinking is very simple, it is an ordered sequence of binary search, where I use a binary search integer array order. If implemented in C, then we need to pay attention to the following aspects:

1. How to determine the lookup is completed, the return value is defined meanings, define exit loop condition

2. How to deal with border issues, such as 123 of this sequence, when we're looking for 1 or 3, it will make the program appear BUG

3. For the number of columns, we usually use plastic storage under standard, remove the standard binary search if the intermediate numbers, what kind of problem occurs? Whether these problems will affect our search, if the problem is how to avoid?

Typically, as a beginner, I think even a binary search is too simple, not worth mentioning, the recent reflection, binary search algorithm for the requirements of the theory is not very high, but if it is put into the feasibility of the program code , then we need to think about many of the details, otherwise write the code is error-prone.

How to solve the first problem, because we understand the idea of ​​binary search is actually a binary search, to have the left margin and right margin we can determine the intermediate element, when the left edge of the right boundary coincides with the time, then find the object becomes an element, if it is not to find an object request, then look in the collection will not have the required elements. So that we clearly define the parameters needed to come out, and quit the Find conditions. We need a left border and right border, as well as the middle element, if the right margin smaller (larger than the left and right) than the left margin when the loop exits, returns an exception. If not, perform a lookup statement.

Here's how to design it to find the statement, if combined with the second and third question, we can readily write the code:

while (left <= right)
{
    mid = (left + right) / 2;
    if (x> mid)
    left = mid;
    else if (x     right = mid;
    else
    return mid;
}
return error;

Obviously doing so it is too good, according to the ideas of mathematics, this is simply not eligible borders, considering the C language will be automatically discarded plastic decimals, then the left boundary is entirely possible that we write but right boundary is never to take, if the intermediate value taken to a non-integer, it will affect other aspects of our search results, the answer is no, if discarded automatically generated status decimal, only to find only affect us in an ordered sequence of elements in a position to find the sub-segment length, and will not affect our search results, if you want to solve the border issue, we can make partial segment generated boundary shift, since the mid element has been judged too, so when we segment boundary segments can discard mid element, so that the border is mid + 1 or mid-1, and we will complete the binary search of an array of integers, codes are as follows:

#include // binary search algorithm that is the test case
int BinySerch (int * arr, int x, int lengh) // design parameters, as is the integer array, so we have to pass him
{// Array length downgrade occur when the Senate otherwise
int left = 0, right = lengh - 1;
int mid;
while (left <= right)
{
mid = left + (right - left) / 2;
if (x {
right = mid - 1;
}
else if (x> arr [mid])
{
left = mid + 1;
}
else
{
return mid;
}
}
return -1;
}
int main () // test case
{
int x = 0;
int arr [11] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int lengh = sizeof (arr) / sizeof (arr [0]);
for (int i = 0; i <12; i ++)
{
printf ( "% d", BinySerch (arr, i, lengh));
}
system ( "pause");
return 0;
}
     
         
         
         
  More:      
 
- Oracle lag () and lpad () function (Database)
- Why I do not like the Go language style interface (ie Structural Typing) (Programming)
- IOwait Linux system monitoring diagnostic tools (Linux)
- lolcat: an output terminal rainbow effects in the Linux command-line tool (Linux)
- How do you turn on and off IPv6 address on Fedora (Linux)
- Ubuntu install Eclipse can not find JAVA_HOME problem (Linux)
- The multiplexed signal driving IO (Programming)
- Improve the efficiency of Linux development tools 5 (Linux)
- Ubuntu 10.10 install Oracle 10g graphic tutorials (Database)
- How VirtualBox and VMware virtual machine conversion (Linux)
- Linux systems for entry-learning: Install closed-source packages in Debian (Linux)
- Linux User Management (Linux)
- Ubuntu install Scala 2.10.x version (Linux)
- Ubuntu install Tonido private cloud services (Server)
- tar command to extract a file error: stdin has more than one entry (Linux)
- Elixir: the future of programming languages (Programming)
- Linux, Google Test (GTest) test environment to build step (Linux)
- Eclipse Android development environment installation (Linux)
- Ubuntu installation 2.10.x version of Scala (Linux)
- How to configure Apache Solr on Ubuntu 14/15 (Server)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.