JavaScript
  Home arrow JavaScript arrow Page 4 - 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 continued


    (Page 4 of 5 )

    Now, the mid element is U. The left element is R and the right element is Y. What we are looking for is T. As you can see from the table, the key is not equal to the mid element but is less than it. So we can ignore the elements (U, W, Y) on the right side of the new sub-list (R to Y).

    Since we are no longer interested in the right side of the new sub-list, let us make “right,”

    right = mid - 1


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


    E

    I

    O

    P

    Q

    R

    T

    U

    W

    Y






    Left

    mid

    Right

    (key)






    Again, we look for the new mid which is

    mid = (5 + 6)/2

    = 5.5


    Sending the value 5.5 through the parseInt() function, we have

    mid = 5

    We continue in the same way: the mid element is now R, the left element is also R, and the right element is T. As you can see from the table, the key is not equal to the mid element but is now greater than it. So we can ignore the element (R) on the left side of the new sub-list (R,T).

    Since we are no longer interested in the left side of the new sub-list, let us make “left,”

    left = mid + 1

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


    E

    I

    O

    P

    Q

    R

    T

    U

    W

    Y







    left

    right

    mid

    (key)





    We still look for the new mid which is

    mid = (6 + 6)/2

    = 6

    We still continue in the same way: the mid element is now T, the left element is T, and the right element is T. All three indices point to the same element. As you can see from the table, the key is now equal to the mid. The process ends here.

    At this point, you may wonder how the process will end if the key is a letter and does not exist in the list. You can deduce that the iteration terminates just after the “left” index is equal to the “right” index. So if the letter does not exist in the list, this condition will still be reached; no index within the range will be returned, but the total number of rows will be returned. Recall that this value is one unit greater than the maximum index.

    I hope you can see that binary search is faster than ordinary search. However this is due to the fact that the list is already sorted. If the list is not sorted, binary search will not make sense.

    Note: With binary search, if two or more elements are the same, any of their indices can be returned. The one that is returned depends on which element the binarySearch arrives at first.


    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 6 Hosted by Hostway
    Stay green...Green IT