473,408 Members | 1,875 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

get smallest from array - recurse or loop?

In IE6 both these functions *seem* to be working.
I don't see much recursion in this group except the
occasional setTimeout problems. Besides the obvious
stack problems that can occur if one recurses too deep
are there other reasons for this that I should know about?
This example returns the position of the leftmost element
of a specific type but it could be any array or any type
of comparison regex or whatever.
Also I'm sure these can be made better. Any help?
Jimbo
#1 loop
getSmallest(document.getElementsByTagName(tagName) );
function getSmallest(aArr) {
var small = aArr[0];
for (var i=1; i < aArr.length; i++) {
if(aArr[i].offsetLeft >= small.offsetLeft)
continue;
else small = aArr[i];
}
return small;
}

#2 recursion
var a = document.getElementsByTagName(tagName);
getSmallest( a, a.length-1);
function getSmallest(aArr, n) {
if (n < 0) return false;
if(n == aArr.length-1) smallest = n;
smallest = aArr[smallest].offsetLeft <= aArr[n].offsetLeft? smallest: n;
if(getSmallest(aArr,n-1))
return aArr[smallest].offsetLeft;
return aArr[smallest].offsetLeft;
}
Jul 23 '05 #1
2 2243
On Sun, 5 Dec 2004 11:03:01 +0200, J. J. Cale <ph****@netvision.net.il>
wrote:
In IE6 both these functions *seem* to be working.
An important thing to note is that they depend on at least one element in
the array. The behaviour the two alternatives is also very different. The
first version will return a reference to the left-most element, or
undefined if there are no elements in the array. The second version will
return smallest offset value, or false if there are no elements in the
array.

[snip]
Besides the obvious stack problems that can occur if one recurses too
deep are there other reasons for this that I should know about?
Recursion tends to be inefficient as function calls have the overhead of
setting up a new execution context. Loops are quicker.

[snip]
getSmallest(document.getElementsByTagName(tagName) );
function getSmallest(aArr) {
var small = aArr[0];
for (var i=1; i < aArr.length; i++) {
if(aArr[i].offsetLeft >= small.offsetLeft)
continue;
else small = aArr[i];
Why not

for(var i = 1, n = aArr.length; i < n; ++i) {
/* This being the most significant change: */
if(aArr[i].offsetLeft < small.offsetLeft) {
small = aArr[i];
}
}
}
return small;
}

#2 recursion
var a = document.getElementsByTagName(tagName);
getSmallest( a, a.length-1);
function getSmallest(aArr, n) {
if (n < 0) return false;
if(n == aArr.length-1) smallest = n;
smallest = aArr[smallest].offsetLeft <= aArr[n].offsetLeft?
smallest: n;
if(getSmallest(aArr,n-1))
This would also evaluate to false if the offset value returned below was
zero.
return aArr[smallest].offsetLeft;
return aArr[smallest].offsetLeft;
That's bizarre: if the result from the recursive call evaluates to true,
return the smallest offset value. Otherwise, return the same thing.
}


This is a much reduced version, but it too differs from both previous
versions. :) It will return the array index of the left-most elements, or
-1 if there are no elements in the array.

function getSmallest(arr, n) {var s;
if(n < 0) {return arr.length ? 0 : -1;}
s = getSmallest(arr, n - 1);
return (arr[n].offsetLeft < arr[s].offsetLeft) ? n : s;
}
getSmallest(arr, arr.length - 1);

Really, I'd just stick to the loop version.

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #2

"Michael Winter" <M.******@blueyonder.co.invalid> wrote in message
news:opsijcl4olx13kvk@atlantis...
On Sun, 5 Dec 2004 11:03:01 +0200, J. J. Cale <ph****@netvision.net.il>
wrote:
In IE6 both these functions *seem* to be working.
An important thing to note is that they depend on at least one element in
the array. The behaviour the two alternatives is also very different. The
first version will return a reference to the left-most element, or
undefined if there are no elements in the array. The second version will
return smallest offset value, or false if there are no elements in the
array.


Thank you for pointing that out Michael. I originally intended to
return an index to the object in both examples but pulled these
out of some operative code on the fly. I realize how important
it is to check the returns properly and handle failures correctly.
[snip]
Besides the obvious stack problems that can occur if one recurses too
deep are there other reasons for this that I should know about?
Recursion tends to be inefficient as function calls have the overhead of
setting up a new execution context. Loops are quicker.


Ahh. That's what I was looking for!
[snip]
getSmallest(document.getElementsByTagName(tagName) );
function getSmallest(aArr) {
var small = aArr[0];
for (var i=1; i < aArr.length; i++) {
if(aArr[i].offsetLeft >= small.offsetLeft)
continue;
else small = aArr[i];
Why not

for(var i = 1, n = aArr.length; i < n; ++i) {
/* This being the most significant change: */
if(aArr[i].offsetLeft < small.offsetLeft) {
small = aArr[i];
}
}
}
return small;
}

#2 recursion
var a = document.getElementsByTagName(tagName);
getSmallest( a, a.length-1);
function getSmallest(aArr, n) {
if (n < 0) return false;
if(n == aArr.length-1) smallest = n;
smallest = aArr[smallest].offsetLeft <= aArr[n].offsetLeft?
smallest: n;
if(getSmallest(aArr,n-1))


This would also evaluate to false if the offset value returned below was
zero.
return aArr[smallest].offsetLeft;
return aArr[smallest].offsetLeft;


That's bizarre: if the result from the recursive call evaluates to true,
return the smallest offset value. Otherwise, return the same thing.


I thought so too :>) that's why the *seems* you quoted. I thought it
best to serve up some code not just the simple question and I don't
write recursions every day. Good drill for an old programmer.
I wrote (hacked) this on the fly from bits that I've been working on.
Thus two differently oriented versions neither of which returns
an index as your fix does.
}


This is a much reduced version, but it too differs from both previous
versions. :) It will return the array index of the left-most elements, or
-1 if there are no elements in the array.

function getSmallest(arr, n) {var s;
if(n < 0) {return arr.length ? 0 : -1;}
s = getSmallest(arr, n - 1);
return (arr[n].offsetLeft < arr[s].offsetLeft) ? n : s;
}
getSmallest(arr, arr.length - 1);

Really, I'd just stick to the loop version.


Amen
Mike

Thanks Mike. To the point answer and thanks for the fixes.
Saves me a lot of mileage.
Have a nice day.
Jimbo -- Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.

Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Keke922 | last post by:
I have to write a program that allows the user to enter a series of integers and -99 when they want to exit the loop. How do I display the largest and smallest number the user entered?
1
by: Cat | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 I'm getting a validation error when I try to restrict the content of nested groups with xs:redefine whereas the same restriction on xs:element's...
4
by: Code4u | last post by:
I need to write an algorithm that sheds the outliers in a large data set, for example, I might want to ignore the smallest 2% of values and find the next smallest. Boost has a nth_element...
5
by: Andrew Poulos | last post by:
If I'm searching for an occurance of a value in a multi-dimensional array how can I get it's index returned as an array, if found? For example, if: foo = new Array(); foo = , 5, , 9, 10]; ...
4
by: eksamor | last post by:
I have a simple linked list: struct element { struct element *next; int start; }; struct list { struct element *head;
3
by: Chris | last post by:
I am clearly missing something simple here.... I have a function (fileTree) that recursively reads all the files in a directory passed to it, and returns an array of the files, I have included...
15
by: Aggelos | last post by:
Hello, I am using php4 And I have a class product I create an array of productObjects that has 2 dimensions. Generraly I am sorting my objects inside arrays and I need to some how recurse those...
1
by: anwar7517525 | last post by:
Dearest how can i find three smallest values in array I solve it by sorting array in ASC Order then i print first three values but i want to another efficient way can you have any efficient way
8
by: Sunny | last post by:
Hi, do someone know, How we can find the smallest distance between a bunch of lat 7 long? Like I have 10 Latitude & Longitude. -73.924598,40.879010 -73.924506,40.878978 -73.924506,40.878978...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.