JavaScript
  Home arrow JavaScript arrow Page 2 - Binary Searching
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
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? 
JAVASCRIPT

Binary Searching
By: Chrysanthus Forcha
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2008-06-30

    Table of Contents:
  • Binary Searching
  • Tutorial Approach
  • Demonstration of Binary Search
  • Demonstration continued
  • Algorithm for 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


    Binary Searching - Tutorial Approach


    (Page 2 of 5 )

    I will explain the ordinary search function. I will then demonstrate the binarySearch function. Next I give the algorithm and then the function (code). I use letters of the alphabet to do most of the explanation. Finally, I show you how the BinarySearch function works with strings.

    The Ordinary Search

    The ordinary search function simply iterates through an array until it finds what you are looking for.

    Before I continue, let me talk about the “return” statement. This statement is not only used with the JavaScript language, it is used with C++ and other languages. If the “return” statement is in a function, during execution, when the function reaches the point with the statement, the function stops and gives control to the calling function. This means that any other thing below the return statement is not executed. Even another return statement is not executed. As control is given to the calling function, the called function can also give a result from what it has processed, to the calling function.

    In my implementation of the ordinary search function, if the function sees the element, it returns the corresponding index in the array. If it does not see the element, it returns the length of the array. This is the implementation.


    function ordinarySearch(theList, key)

    {

    for (i=0; i<theList.length; ++i)

    {

    if (theList[i] == key)

    return i;

    }


    return theList.length;

    }


    The element you are looking for is called the key. The function takes the name of the array and the element (key) you are looking for as arguments. The function then goes into a for-loop, and beginning from the first element, it compares the key with the element of the index. When the function sees the element, it returns the index of the key seen in the array. If the function goes through the array completely, and does not see the key, it returns the length of the array.

    You may ask why we do not allow the function to complete the iteration; why should we return the index as soon as the key is seen? This is to reduce processing time. Imagine that the key is the first element in the array. Going through (comparing) the rest of the elements would simply be a waste of time.

    If the function does not see the key in the for-loop, it will not return because of the “if” condition. The execution of the function will continue. As such, the second return statement “return theList.length” will ultimately be executed.

    More JavaScript Articles
    More By Chrysanthus Forcha


       · Binary Searching produces very fast result whenever you have an array that is...
     

    JAVASCRIPT ARTICLES

    - Using jQuery to Preload Images with CSS and ...
    - Using Client-Side Scripting to Preload Image...
    - Removing Non-Semantic Markup when Preloading...
    - Using the Display CSS Property to Preload Im...
    - Preloading Images with CSS and JavaScript
    - Scaling and Moving Web Page Elements with th...
    - Fading, Hiding and Sliding HTML Elements wit...
    - Toggling CSS Properties with the GX JavaScri...
    - Cancel, Queue and Pause Animations with the ...
    - Producing Elastic Effects with the GX JavaSc...
    - Moving Divs Diagonally with the GX JavaScrip...
    - Moving Elements Vertically and Horizontally ...
    - Making Bouncing Effects in Parallel with the...
    - Creating Bouncing Effects with the GX JavaSc...
    - Manipulating Background Colors with the GX J...







    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek