Menu DaniWeb
Log In Sign Up
  • Read
  • Contribute
  • Meet
  1. Home
  2. Programming Forum
  3. Software Development Forum
  4. Code Snippet Repository
  5. Reusable Code Snippet

Binary search

16 Years Ago sahasrara 0 Tallied Votes 119 Views Share

Dear all
This code shows, look for an integer number in the array of this type that we used from Binary search.

It was wrote by Borland 5.02 .

Good Luck!

c++
//Binary search 
//Programming by : Erfan Nasoori
//Mail : ketn68@yahoo.com
Date of send : 2009/2/1


#include <iostream>
#include <conio>

int binarysearch(int[],int,int);

int main()
{
 int sortedArray[100];
 int key,n,hold,F,L,k,G,i;

 cout<<"How many :";
 cin>>n;

 cout<<"Enter numbers :\n";
 for(int i=0;i<n;++i)
 {
  cout<<"number["<<(i+1)<<"] = ";
  cin>>sortedArray[i];
 }

 for(int l=1;l<=n-1;++l)
  for(int j=0;j<(n-l);++j)
  if(sortedArray[j]>sortedArray[j+1])
  {
	hold=sortedArray[j];
	sortedArray[j]=sortedArray[j+1];
	sortedArray[j+1]=hold;
  }

 cout<<"sorted numbers:";
 for( i=0;i<n;++i)
 cout<<sortedArray[i]<<'\t';

 cout<<"\nkey:";
 cin>>key;
 G=binarysearch(sortedArray,n,key);

 if(G == -1)
    cout<<"It didn't found!";
 else
    cout<<"\nIts index = "<<G<<endl;

 getch();
 return 0;
}   //end of main
//////////////////////////////////////////////////////////
int binarysearch(int sortedArray[], int n, int key)
{
 int first=0,last=n-1;
 while (first <= last)
 {
   int mid = (first + last) / 2;

   if (key > sortedArray[mid])
      first = mid + 1;
   else if (key < sortedArray[mid])
      last = mid - 1;
   else
      return mid;
 }
 return -1;
}
About the Author
Member Avatar for sahasrara
sahasrara 0 Newbie Poster
Member Avatar for sahasrara
sahasrara 0 Newbie Poster
16 Years Ago

Excuse me for 4th line.
it is a comment line.

Member Avatar for ddanbe
ddanbe 2,724 Professional Procrastinator Featured Poster
16 Years Ago

I WRITE this sentence now.
Yesterday, I WROTE a letter to my friend.
It WAS WRITTEN with a ballpen.

Reply to this topic
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.

Sign Up — It's Free!
Related Topics
  • Member Avatar Binary Search Tree, Array 1
  • Member Avatar C++ Help with binary search and sort! 12
  • Member Avatar SDL abstraction 1
  • Member Avatar Binary Search in C Spell Checker 2
  • Member Avatar Access Your PC Soundcard (Python) 2
  • Member Avatar Binary Search Using Recursion on 2-D Array 4
  • Member Avatar simple calculator help 1
  • Member Avatar binary search tree 6
  • Member Avatar need help. asembler basics 5
  • Member Avatar Overload Operator<< for a Binary Search Tree. Getting C.E. C3861 2
  • Member Avatar Data Type Mismatch In Criteria Expression [help] 7
  • Member Avatar Output Binary Search Tree to file help 4
  • Member Avatar Bit String help 4
  • Member Avatar Recursive Binary Search Tree Header File 1
  • Member Avatar system wide api hook 4
  • Member Avatar Urgent Help in Binary Search Trees.. 16
  • Member Avatar Binary Search of an element in an array of elements 1
  • Member Avatar Binary Tree - Works in C++ 6.0 but doesn't work in VS2008 3
  • Member Avatar Binary Expression Tree 9
  • Member Avatar C++ newbie Need help^_^ 5
Not what you need?

Reach out to all the awesome people in our software development community by starting your own topic. We equally welcome both specific questions as well as open-ended discussions.

Start New Topic
Topics Feed
Reply to this Topic
Edit Preview

Share Post

Insert Code Block

  • Forums
  • Forum Index
  • Hardware/Software
    • Recommended Topics
  • Programming
    • Recommended Topics
  • Digital Media
    • Recommended Topics
  • Community Center
    • Recommended Topics
  • Latest Content
  • Newest Topics
  • Latest Topics
  • Latest Posts
  • Latest Comments
  • Top Tags
  • Topics Feed
  • Social
  • Top Members
  • Meet People
  • Community Functions
  • DaniWeb Premium
  • Newsletter Archive
  • Markdown Syntax
  • Community Rules
  • Developer APIs
  • Connect API
  • Forum API Docs
  • Tools
  • SEO Backlink Checker
  • Legal
  • Terms of Service
  • Privacy Policy
  • FAQ
  • About Us
  • Advertise
  • Contact Us
© 2025 DaniWeb® LLC