Experts Round Table Network
Navigate
Home
ArticleWiki
Forum
Journal
Search
Newsletter
Links
Tech News
expertsrt.com
Welcome Guest.
Username:
Password:
Remember me
Forgot your password?
Register
Search: binary vs. linear
Welcome,
Guest
. Please
login
or
register
.
August 28, 2008, 01:57:30 PM
11291
Posts in
1243
Topics by
838
Members
Latest Member:
gjpino
Home
Help
Search
Login
Register
Experts Round Table Network
|
Serverside Technology
|
PHP
|
Search: binary vs. linear
« previous
next »
Pages:
[
1
]
Print
Author
Topic: Search: binary vs. linear (Read 436 times)
amarone
Offline
Posts: 7
Search: binary vs. linear
«
on:
November 09, 2007, 02:51:55 AM »
Hello everybody, here I am with my new dilemma, trying to understand when binary search is actually cheaper then linear search. Hope it's not too stupid <scratches head>:
I have an array, ascending sorted by value, I must find out the index of the one element containing 50, out of 1,000,000.
Linear search works on 50 iterations (5,000,000 if I look for 5,000,000 value), as many comparisons and 1 return, null or the identified index.
Binary search needs 20 iterations on the array (2
20
= 1,048,576) and a repeated amount of php built-in functions to decrease the search area on the basis of as many comparisons; eventually, the return of the index (or null if not found, obviously).
Well, here is my doubt: if I count my code operations, it's easy to state that on average binary search is lighter then linear search; but what is the actual weight of all the comparisons, the assignments, the structures of the b.s. versus the single structure, the single comparison, the single assignment, the single return, all multiplied by the number of iterations needed by the l.s., keeping in mind everything lies under each of the built in functions?
One more element is to be considered: I don't know processing architecture of pc cpus and operating systems, but the same question based on an IBM AS400 would need an in-depth analysis because of it's way of processing linear operations. The AS bufferizes big chunks (of records of a file, of array elements, etc.) in its ram and then chews them one by one in big aggregate operations, speeding enormously the job and giving sense to my doubt.
Not an idle talk, i actually need to optimize my code because of the huge amount of data to be processed.
Thank you all!
Iacopo
Logged
VGR
Mentor
Offline
Posts: 680
Re: Search: binary vs. linear
«
Reply #1 on:
November 11, 2007, 12:45:15 PM »
given you can't intrumentalize the binary code in PHP, you've either to instrumentate your source code - that's tedious - OR - simplicity is queen - just do some benchmarking (performance measurements).
that'll tell you which technique is the best in worst/best/average conditions, depending on the test data
good luck
Logged
techie overlord, answers all kind of questions on
http://www.europeanexperts.org
GrandSchtroumpf
Mentor
Offline
Posts: 408
Re: Search: binary vs. linear
«
Reply #2 on:
November 11, 2007, 11:00:21 PM »
It took me some time to understand the question. We were missing some information.
This is how i understand the question:
We have a sorted array that contains 1,000,000 distinct positive integer values and we need to find the index of the value "50".
Since the array is sorted and has no duplicate values, we know that the index will be between 0 and 49.
AFAIK, PHP does not have optimized built-in search functions for sorted arrays (neither binary nor linear).
This means you"ll need to write your own search function.
Note that the "array_search" PHP function will traverse the complete array if the searched value is not in the array.
Your mistake is to only look at the max comparisons (50 for linear, 20 for binary on the whole array).
You also need to look at the typical number of comparisons, and that will depend on the typical content of your array.
What will be the typical index of your value "50"?
If it's close to 0, then do a linear search starting at index 0.
If it's close to 49, then do a backwards linear search starting at index 49.
If there is no typical index, do a binary search that is limited to the first 50 values of your array.
And as VGR said, benchmark the different algorithms inside your application.
Cheers.
Logged
Pages:
[
1
]
Print
« previous
next »
Jump to:
Please select a destination:
-----------------------------
ERT 1.5
-----------------------------
=> Round Table Learning Center
=> Bug reports
-----------------------------
Legacy
-----------------------------
=> The next level
=> History of ERT
-----------------------------
Community Affairs
-----------------------------
=> Introductions
=> Ballot Box
===> Closed Polls
=> Soapbox
=> Propose and Consult
===> Propose and Consult...CLOSED
-----------------------------
Bits and Bytes
-----------------------------
=> Tips, Tricks, Snippets, Tidbits And General Pearls Of Wisdom
-----------------------------
Serverside Technology
-----------------------------
=> PHP
=> ASP
-----------------------------
Webservers
-----------------------------
=> Apache
=> IIS
-----------------------------
Databases
-----------------------------
=> MySQL
=> Access
=> MS SQL Server
-----------------------------
Clientside Technology
-----------------------------
=> HTML
=> CSS
=> Javascript
=> Flash
=> WAP/WML
-----------------------------
Web Technologies
-----------------------------
=> General Web Dev
=> Web Standards
=> XML
=> Online Marketing
-----------------------------
Graphics
-----------------------------
=> Graphics Design and Animation
-----------------------------
Programming
-----------------------------
=> .NET
=> JAVA
=> MS DOS Batch Scripting
=> Mathematics
=> C & C++
=> VB
=> Delphi
=> Algorithm design
-----------------------------
Operating Systems
-----------------------------
=> Windows (General)
=> NT Based (2K, 2K-03, NT, XP, Vista)
=> Open Source (All)
-----------------------------
Hardware
-----------------------------
=> Hardware General
=> Gamers Hardware (Advanced)
-----------------------------
Networking
-----------------------------
=> Home (small)
=> Office (large)
=> Internet
-----------------------------
Security
-----------------------------
=> General Security Issues
-----------------------------
Rants/Opinions/Proposals
-----------------------------
=> Site operation
Powered by SMF 1.1 RC2
|
SMF © 2001-2005, Lewis Media
Joomla Bridge by
JoomlaHacks.com