JavaScript
  Home arrow JavaScript arrow Page 3 - 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 / 2
    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 - Demonstration of Binary Search


    (Page 3 of 5 )

    When the values (elements) of an array are in sorted order, there are better search methods than the ordinary search method. When you look up a name in the phone book, you do not start at the beginning and scan (glance) through until you find the name – you use the fact that the names are listed in alphabetical (sorted) order and use some intelligence to jump quickly to the right page and then start scanning.

    Binary search deals with a list that is already sorted. The problem is that natural data is not normally sorted. Well, some data is stored sorted. A lot of data is kept in database. When retrieving the data, you can have it sorted by the database engine. You will then be able to use binary search on the sorted result. So even though natural data is not normally sorted, there are many sources of sorted data on which you can do binary search.

    In this article, I show you how to sort an array of elements (letters or strings) that are in ascending order. The same principles apply to elements that are in descending order.

    Consider the following list:

    E I O P Q R T U W Y


    We shall look for the letter T. So the key is T. There are 10 elements in the list.

    Let the index for the left most element be called “left” – at the moment this is 0.

    Let the index for the right most element be called “right” – at the moment this is 9.

    Let the whole number obtained from (left + right)/2 be called “mid.”

    Mid as defined above is the approximate mid-point index. The expression can result in a whole number and a fraction. We want the result to be an index. Since there is no fractional index, we have to drop the fractional part of the result from the expression. We shall use the top-level JavaScript function named "parseInt()" to obtain the whole number (integer) from the expression.

    At the moment left = 0, and right = 9.

    mid = (left + right)/2

    = (0 + 9)/2

    = 4.5


    We use the parseInt() function as follows:

     

    mid = parseInt((left + right)/2)


    Therefore,

    mid = 4


    The following table shows the present situation:


    E

    I

    O

    P

    Q

    R

    T

    U

    W

    Y

    left




    mid


    (key)



    right


    The mid element is Q. Now, since the array (list) is sorted, we can see from the array (table) that the key is not equal to the mid element, but is greater than the mid element. So we do not have to bother looking for the key on the left side of the array; that is from Q to E. This looks so obvious, but is not obvious to the computer. The computer does not observe or glance or inspect and appreciate things the way we are doing. The computer sorts using logical steps, which I show you in this demonstration.

    Since we are no longer interested in the left side of the array, let us make “left,”

    left = mid + 1

    So the element for the index, “left” is now R. The following table shows the situation.  

     

    E

    I

    O

    P

    Q

    R

    T

    U

    W

    Y






    left

    (key)

    mid


    right


    The new value for mid is

    mid = (5 + 9)/2

    = 7

    More JavaScript Articles
    More By Chrysanthus Forcha


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

    JAVASCRIPT ARTICLES

    - Validating Digits and Dates with jQuery`s Va...
    - Validating Ranges, Emails, and URLs with jQu...
    - More Uses for the jQuery Tooltip Plug-in`s b...
    - Building Image-Based Tooltips with the jQuer...
    - Using the jQuery Tooltip Plug-in`s bodyHandl...
    - Using Rangelength, Min and Max with the Vali...
    - Using Minlength and Maxlength with the Valid...
    - Modifying Tooltip Coordinates with the jQuer...
    - Applying a Fade Out Effect with the jQuery T...
    - Tracking Mouse Movements with the jQuery Too...
    - Checking Online Forms with the Validator jQu...
    - Nested JavaScript Functions as Objects
    - The jQuery Tooltip Plug-in
    - Active Client Pages at the Server
    - ACP Tab Web Page







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    Stay green...Green IT