Total members 11890 |It is currently Sat Apr 20, 2024 6:02 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Binary search implementation using C++
cpp code
int B(int a[],int f, int l, int t)
{
if( f > l)// 1st Base Case, return if the array is empty
{
return -1;
}
int mid = (f+l)/2;
if(a[mid] == t) // found the target
return mid;
else if(t >n)
r = B(c,0,9,n);
if( r == -1)
cout<<" Sorry !!! The Elemen Is not in our Array ... "<<endl;
else
cout<<" Your element is in the index : "<<r<<endl;
}


Another example of binary search implementation :
cpp code
#include <stdlib.h>
#include <string.h>
int bisearch(void *sorted, const void *target, int size, int esize, int
(*compare)(const void *key1, const void *key2)) {

int left,
middle,
right;

// Continue searching until left = right

left = 0;
right = size - 1;
while (left <= right) {
middle = (left + right) / 2;
switch (compare(((char *)sorted + (esize * middle)), target)) {
case -1:
// search the right of the middle index.
left = middle + 1;
break;
case 1:
// search the left of the middle index.
right = middle - 1;
break;
case 0:
return middle;
}
}
// not found
return -1;
}


Note tha the binary search order of growth is O(log2 (N) )



_________________
Please recommend my post if you found it helpful


Author:
Newbie
User avatar Posts: 14
Have thanks: 0 time

updated.

_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 2 posts ] 

  Related Posts  to : binary search
 Binary search tree C++     -  
 A simple search page using Google AJAX Search API     -  
 Java Binary Tree     -  
 Read Binary File in C++     -  
 convert string into binary     -  
 convert to binary number     -  
 need help for creatinb binary tree in php     -  
 convert binary number to decimal     -  
 conversion from binary to decimal numbers as string     -  
 Naive Bayes Classification (Binary )- Supervised Learning     -  



Topic Tags

C++ Algorithms
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com