Home arrow JavaScript arrow Page 3 - Binary Searching
JAVASCRIPT

Binary Searching


Binary Searching involves searching for an element in an array, where the items are arranged in ascending or descending order. That means the entries in the array are sorted. If you want to learn how to write a function that does this, keep reading.

Author Info:
By: Chrysanthus Forcha
Rating: 5 stars5 stars5 stars5 stars5 stars / 3
June 30, 2008
TABLE OF CONTENTS:
  1. · Binary Searching
  2. · Tutorial Approach
  3. · Demonstration of Binary Search
  4. · Demonstration continued
  5. · Algorithm for Binary Search

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

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


blog comments powered by Disqus
JAVASCRIPT ARTICLES

- More Top jQuery Tutorials for Beginners
- More Top jQuery Plugins for Menus
- Top jQuery Tutorials for Beginners
- New UI Framework and SDK for JavaScript Rele...
- JavaScript OpenPGP Tool, Node.js 0.6.3 Avail...
- Yahoo Releases Cocktails Language and Develo...
- Customizing jQuery Slideshows: Dynamic Contr...
- Customizing jQuery Slideshows: the animate()...
- Customizing jQuery Slideshows: slideUp() and...
- Customizing jQuery Slideshows: hide() and sh...
- Web Workers: Performing Calculations in Para...
- More Top JavaScript Frameworks and Libraries
- More Dynamic jQuery Styling Techniques
- The Top JavaScript Libraries
- The Top JavaScript Frameworks

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 7 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials