Practices
  Home arrow Practices arrow Page 2 - 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  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
PRACTICES

Basic Array Searching in C++
By: Bryan Roth
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 38
    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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    Basic Array Searching in C++ - Sequential Search
    ( Page 2 of 4 )

    The sequential search is best used if the array you are searching is unsorted. This method of searching is usually used on small arrays of less than 16 elements. We start the sequential search by first declaring a target to be found. The search initiates at the beginning of the array until it finds the target. 

    In the following example we will find a target value of 23 within a one dimensional array. At index 0, 32 is not equal to 23 so we proceed on to the next element.

    a[0]

    a[1]

    a[2]

    a[3]

    a[4]

    32

    431

    -34

    23

    12

    At index 1, 431 is not equal to 23 so we proceed.

    a[0]

    a[1]

    a[2]

    a[3]

    a[4]

    32

    431

    -34

    23

    12

    At index 2, -34 is not equal to 23 so we proceed.

    a[0]

    a[1]

    a[2]

    a[3]

    a[4]

    32

    431

    -34

    23

    12

    Finally at index 3, 23 is equal to 23 and we have found our target.

    a[0]

    a[1]

    a[2]

    a[3]

    a[4]

    32

    431

    -34

    23

    12

    Now we will implement this example of a sequential search into C++ code. The program below asks the user for a target to be found, then uses a for loop to analyze each element of the array. If the array element is equal to the target it will display that the target was found. Whenever a target is found the variable “flag” will be incremented by 1. At the end of the program if the variable “flag” is less than one, then the target was obviously not found.

    #include <iostream>
    using namespace std;

    int main()
    {
      const int arraySize = 5;
      double target;
      int array[arraySize] = {32, 431, -34, 23, 12};
      int flag;

      // flag is used to log how many times the target is encountered.

      flag = 0;

      cout << "Enter a target to be found: ";
      cin >> target;

      for(int cntr = 0; cntr < arraySize; cntr++)
      {
        if(array[cntr] == target)
        {
    cout << "Target found in array index " << cntr << "."
         << endl;

          flag += 1;
        }
      }
     
      // Test to see if target was found.

      if(flag < 1)
      {
        cout << "Target not found." << endl;
      }

      return 0;
    }

    The sequential search does have a pitfall. It is very slow and its performance rating is low. If a person had an array of one million elements, that would mean there could be up to one million comparisons, and that takes time! The sequential search method would be advisable to use only if the array you were searching was unsorted and small.



     
     
    >>> More Practices Articles          >>> More By Bryan Roth
     

       

    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-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek