Practices
  Home arrow Practices arrow Page 3 - Basic Array Searching in C++
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
Moblin 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PRACTICES

Basic Array Searching in C++
By: Bryan Roth
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 29
    2004-12-27

    Table of Contents:
  • Basic Array Searching in C++
  • Sequential Search
  • Binary Search
  • Coding a Binary Search

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!

    Basic Array Searching in C++ - Binary Search


    (Page 3 of 4 )


      
    The binary search is a more efficient way of searching an array of data. As an array starts to get large (greater than 16 elements) the binary search is the suggested method of searching. However, the use of a binary search requires that the array be sorted.

    The binary search begins by analyzing the middle term of the array. This determines if the target is in the first half or second half of the array. If the target is in the first half of the array there is no need to check the second half of the array. If the target is in the second half of the array there is no need to check the first half of the array. By knowing in which half the target is located, we eliminate the other half from consideration. This eliminates unnecessary searching, which improves performance. So, if we had a target value of 200,000 in an array of one million elements we could get rid of elements from 500,000 to 1,000,000 without searching! This continues until we find the given target or until it is not found.

    In the following example we find a target of 4 in a sorted array. We first find what the first, middle, and last indexes are of the array. The middle index is found by:

    middle index = (first index + last index)/2

    Here we see that 0 is the first index, 2 is the middle index, and 4 is the last index.

    a[0]

    a[1]

    a[2]

    a[3]

    a[4]

    1

    2

    3

    4

    5

    Now, we compare the target to the value of the middle index. The value of the middle index is 3, which is greater than the index of 2, meaning that the second half of the array doesn’t need to be considered. We are left with the first half of the array and now repeat the process to find the first, middle, and last indexes. We see that 0 is the first index and 1 is the last index. The middle index is 0 because even though (0 + 1)/2 = 0.5, the value is of data type integer and C++ truncates this making it 0 and does not round the value.

    a[0]

    a[1]

    a[2]

    a[3]

    a[4]

    1

    2

    3

    4

    5

    Lastly, we compare the target value to the value of the middle index. The value of the middle index is less than the target value so we end up having the first index of the array equaling the target value. This terminates the searching process and we become successful in finding the target value.

    a[0]

    a[1]

    a[2]

    a[3]

    a[4]

    1

    2

    3

    4

    5

    More Practices Articles
    More By Bryan Roth


       · A good refresher on basic array searching, but there are some flaws. Can you fix...
       · Hey Anonymous Loozah,1. I realized that the table examples where the same over...
       · Thanks. I'm looking forward to your next article.
       · on the one hand this is a useful article, on the other, anyone who doesn't know how...
       · Thanks for the quick editing.
     

       

    PRACTICES ARTICLES

    - More Techniques for Finding Things
    - Finding Things
    - Finishing the System`s Outlines
    - The System in So Many Words
    - Basic Data Types and Calculations
    - What`s the Address? Pointers
    - Design with ArgoUML
    - Pragmatic Guidelines: Diagrams That Work
    - Five-Step UML: OOAD for Short Attention Span...
    - Five-Step UML: OOAD for Short Attention Span...
    - Introducing UML: Object-Oriented Analysis an...
    - Class and Object Diagrams
    - Class Relationships
    - Classes
    - Basic Ideas




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway