Code Snippets

  

C++ Source Code


Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,214 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,269 people online right now. Registration is fast and FREE... Join Now!





Iterative Binary Search

Traverses the list to find data

Submitted By: KYA
Actions:
Rating:
Views: 536

Language: C++

Last Modified: July 18, 2008
Instructions: Use within a program to search data for specific values--list must already be sorted!

Snippet


  1. int BinarySearch(int A[], int value, int low, int high)
  2. {
  3.         while (low <= high)
  4.         {
  5.            mid = (low + high) / 2;
  6.            if (A[mid] > value)
  7.                high = mid - 1;
  8.            else if (A[mid] < value)
  9.                low = mid + 1;
  10.            else
  11.                return mid; // found
  12.         }
  13.         return -1; // not found
  14. }//end binary search
  15.  

Copy & Paste


Comments


There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month