472,799 Members | 1,287 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,799 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 2218
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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.