Binary search is used to locate a value in a sorted list of values.
It selects the middle element in the array of sorted values, and compares it with the target value; that is the key we are searhing for in the array.
If it islower than the target value then the search is made after that middle element and till the end of the array. If it is equal then the target value is found and the middle is returned. Otherwise, search is made from the beginning of the array till middle -1. If the lower limit is larger than the upper limit, the ones that determine the search area, then the target value is not in the array.
I present an example code for a binary search function. -
-
/**
-
*
-
*
-
* FILE: bsearch.h
-
*
-
*
-
* Description:
-
*
-
* Function prototype for binarySearch function.
-
*
-
*
-
*/
-
-
-
-
#ifndef _bsearch_h
-
-
-
-
#define _bsearch_h
-
-
-
-
-
-
/**
-
*
-
* Synopsis:
-
*
-
* int binarySearch( int a[], int key, int size )
-
*
-
*
-
* Description:
-
*
-
* Function binarySearch performs a binary search in a
-
* sorted array a searching for a key.
-
*
-
*
-
* Parameters:
-
*
-
* a[]: sorted array of integers.
-
*
-
* key: key to be searched for in a[].
-
*
-
* size: size of a[].
-
*
-
*
-
* Assertions:
-
*
-
* none.
-
*
-
*
-
* Returns:
-
*
-
* Function binarySearch returns -1 if key is not in a[].
-
* Otherwise in returns the i, for which a[ i ] == key.
-
*
-
*
-
*/
-
int binarySearch( int a[], int key, int size );
-
-
-
-
-
-
#endif
-
/**
-
*
-
*
-
* FILE: binarySearch.c
-
*
-
*
-
* Description:
-
*
-
* File contains binarySearch functions declared in bsearch.h file.
-
*
-
*
-
*/
-
-
-
-
-
-
#include "bsearch.h"
-
-
-
-
-
-
/**
-
*------------------------------------------------------------
-
*
-
* Function _binarySearch is called by binarySearch function with
-
* low = 0 and high = size - 1, to secure the search boundaries of
-
* the array.
-
*
-
*------------------------------------------------------------
-
*/
-
static int _binarySearch( int a[], int key, int low, int high );
-
-
-
-
-
-
int binarySearch( int a[], int key, int size ){
-
-
-
-
return ( _binarySearch( a, key, 0, size - 1 ) );
-
-
-
-
} // int binarySearch( int a[], int key, int size )
-
-
-
-
-
/**
-
*------------------------------------------------------------
-
*
-
* Search key value in a[].
-
*
-
*------------------------------------------------------------
-
*/
-
static int _binarySearch( int a[], int key, int low, int high ){
-
-
-
-
if ( low > high ){
-
-
-
-
return ( -1 ); /** key not found in a[] */
-
-
-
-
}
-
else{
-
-
-
-
int middle = ( low + high ) / 2;
-
-
-
-
if ( a[ middle ] == key ){
-
-
-
-
return ( middle ); /** key found in a[] */
-
-
-
-
}
-
else if ( a[ middle ] < key ){
-
-
-
-
/**
-
*
-
* key we are searching is larger than the value of a[ middle ]
-
* and since array is sorted, the search will continue in the side
-
* after the middle index; that is from middle + 1 till high.
-
*
-
*/
-
return ( _binarySearch( a, key, middle + 1, high ) );
-
-
-
-
}
-
else{
-
-
-
-
/**
-
*
-
* key we are searching is lower than the value of a[ middle ]
-
* and since array is sorted, the search will continue in the side
-
* before the middle index; that is from low till high - 1.
-
*
-
*/
-
return ( _binarySearch( a, key, low, middle - 1 ) );
-
-
-
-
}
-
-
-
-
}
-
-
-
-
} // static int _binarySearch( int a[], int key, int low, int high )
-
/**
-
*
-
*
-
* FILE: main.c
-
*
-
*
-
* Description:
-
*
-
* Test binarySearch function.
-
*
-
*
-
*/
-
-
-
-
-
-
#include "bsearch.h"
-
-
-
-
#include <assert.h>
-
-
-
-
-
-
/**
-
*
-
* Simple check for binarySearch function
-
*
-
*/
-
int main( void ){
-
-
-
-
int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
-
-
-
-
int i;
-
-
-
-
for ( i = 0 ; i < 21 ; ++i ){
-
-
-
-
assert( binarySearch( a, i, 21 ) == i );
-
-
-
-
}
-
-
-
-
for ( ; i < 50 ; ++i ){
-
-
-
-
assert( binarySearch( a, i, 21 ) == -1 );
-
-
-
-
}
-
-
-
-
return ( 0 );
-
-
-
-
} // int main( void )
0 6947 Sign in to post your reply or Sign up for a free account.
Similar topics
by: Ravi |
last post by:
I recently had to write some code which required me to use a binary
search tree (BST) due to running time concerns. I found that there is no
BST class in the Standard C++ library. I have been told...
|
by: Tarique Jawed |
last post by:
Alright I needed some help regarding a removal of a binary search
tree. Yes its for a class, and yes I have tried working on it on my
own, so no patronizing please. I have most of the code working,...
|
by: Hemang Shah |
last post by:
Hello fellow Coders!
ok, I"m trying to write a very simple application in C#. (Yes its my first
program)
What I want to do is :
1) Open a binary file
2) Search this file for a particular...
|
by: Gordon Knote |
last post by:
Hi
can anyone tell me what's the best way to search in binary content? Best if someone could post or link me to some source code (in C/C++).
The search should be as fast as possible and it would...
|
by: Andrew |
last post by:
Hi, im trying to create a small function which can create a binary tree
from the entries in a text file and after that we can perform all the
usual operations like search, insert and delete etc....
|
by: free2cric |
last post by:
Hi,
I have a single link list which is sorted.
structure of which is like
typedef struct mylist
{
int num;
struct mylist *next;
|
by: Timmy |
last post by:
The bigger problem is with the Binary Search. The program crashes when it's
excuted. and Visual Studio 2005 indicates stack over flow and shows a break
at that function.
Sequential search is...
|
by: Vince Filby |
last post by:
Hi,
We are working with distributing Lucene.net. We have Master Index
Server which takes responsibility of distributing the index searching
to multiple Index Servers by calling the remote...
|
by: jhansi |
last post by:
create a binary file and insert the records and query on those inserted record..
queries are: query based on search by name,search by age and search by designation.
the problem is i have...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |