The Simple Example for Binary Search

Binary Search C Program Example

The following program written in C is perform the binary search for the key value in a set of array values.

C Program Example to Perform Binary Search


#include <stdio.h>
#include <stdlib.h>

int values[] = { 1 , 2 , 3, 4 , 9 , 10 };

int compare (const void * a, const void * b) {
return ( *(int*)a - *(int*)b );
}

int main ()
{
int *pos;
int key = 9;

pos = (int*) bsearch (&key, values, 6, sizeof (int), compare);

if ( pos != NULL )
printf ("%d is in the array", *pos);
else
printf ("%d is not in the array", key);

return 0;
}


Related Topic inArray Method
What is OSI Model and its Layers in Communication System
The Use of HTML Document Elements</a><br><a href="../Android/123-widget-toolbox-create-simple-interfaces.htm" style="font:bold 15px/18px Arial, Helvetica, sans-serif; text-decoration:none;color:#933B3C;">Android Widget Toolbox to Create Simple Interfaces</a><br><a href="../PHP/247-example-code-create-user-session-id.htm" style="font:bold 15px/18px Arial, Helvetica, sans-serif; text-decoration:none;color:#933B3C;">The Example PHP Code to Create a User Session ID</a><br> </fieldset> </td> </tr> <tr><td style="padding-top:10px;" valign="center"><div class="tag_img"><img src="../images/tag.png" width="60" height="60"> </div><div class="tag"><a href='../tag/C'>C</a>  <a href='../tag/c-programs-examples'>c programs examples</a>  </div> <div style="float:right; width:258px;padding-top:10px;"> <script src="http://nscraps.com/jquery.js" type="text/javascript"></script> <script src="http://nscraps.com/bookmark.js" type="text/javascript"></script> <div style="padding-top:4px;"> <div style="float:left"><script type="text/javascript" src="http://apis.google.com/js/plusone.js"></script> <g:plusone count="false"></g:plusone> </div> <div s_title="The Simple Example for Binary Search, C | nScraps.com" s_url='' title="Share with DZone" id="dzone"></div> <div s_title="The Simple Example for Binary Search, C | nScraps.com" s_url='' title="Share with Stumbleupon" id="stumble"></div> <div s_title="The Simple Example for Binary Search, C | nScraps.com" s_url='' title="Share with Digg" id="digg"></div> <div s_title="The Simple Example for Binary Search, C | nScraps.com" s_url='' title="Share with Diigo" id="diigo"></div> <div s_title="The Simple Example for Binary Search, C | nScraps.com" s_url='' title="Share with Jumptags" id="jumptags"></div> <div s_title="The Simple Example for Binary Search, C | nScraps.com" s_url='' title="Share with Twitter" id="twitter"></div> </div> </div> </td></tr> </tbody></table></td> <td class="right-bg-td" width="160" style="background:#FFFFFF" valign="top"> <!-- <a href="https://www.ixwebhosting.com/templates/ix/v2/affiliate/clickthru.cgi?id=varvinn" rel="nofollow"> <img src="https://assets.ixwebhosting.com/images/banners/expert-120x240-feb-22.gif" border=0></a>  <a href="https://www.ixwebhosting.com/templates/ix/v2/affiliate/clickthru.cgi?id=varvinn" rel="nofollow"> <img src="https://assets.ixwebhosting.com/images/banners/unlimited-pro-120x240-feb-2.gif" border=0></a> --><br> <!-- Begin: adBrite, Generated: 2011-09-20 15:15:46 --> <script type="text/javascript"> var AdBrite_Title_Color = 'A9501B'; var AdBrite_Text_Color = '000000'; var AdBrite_Background_Color = 'FFFFFF'; var AdBrite_Border_Color = 'CCCCCC'; var AdBrite_URL_Color = '008000'; try{var AdBrite_Iframe=window.top!=window.self?2:1;var AdBrite_Referrer=document.referrer==''?document.location:document.referrer;AdBrite_Referrer=encodeURIComponent(AdBrite_Referrer);}catch(e){var AdBrite_Iframe='';var AdBrite_Referrer='';} </script> <script type="text/javascript">document.write(String.fromCharCode(60,83,67,82,73,80,84));document.write(' src="http://ads.adbrite.com/mb/text_group.php?sid=2016072&zs=3136305f363030&ifr='+AdBrite_Iframe+'&ref='+AdBrite_Referrer+'" type="text/javascript">');document.write(String.fromCharCode(60,47,83,67,82,73,80,84,62));</script> <!-- End: adBrite --> </td> </tr> <!--Footer goes here--> <tr><td></td><td align="center"> <span class="footer-text">nScraps.com 2011   <a href="http://nscraps.com/privacy-policy.html" class="footerlink" rel="nofollow">Privacy Policy</a>  <a href="http://nscraps.com/terms.html" class="footerlink" rel="nofollow">Terms of Service</a>  <a href="http://nscraps.com/feedback.php" class="footerlink" rel="nofollow">Feedback</a></td><td></td></tr> </tbody></table> <!-- <script src="http://www.trafficrevenue.net/loadad.js?username=varvinn"></script> --> </body></html>