dude, binary search algorithms are pretty easy to implement. They just sound like they are fancy...

Im sure there was psuedo code for it in my y12 computer studies text book.

For simple array searches its proly the fastest search you will need. ie. its an O(logn) operation. There are faster searches, but you wont notice the difference on small arrays that much.

99.9% of comp sci students would have to write this algorithm at some stage of their course.

ps. source code below is off the top of my head, might have to debug if you are going to copy / paste.

<cfscript>

function binarySearch(sortedArray, startIdx, endIdx, value)
{
        arrayMid = endIdx / 2;
        if( sortedArray[arrayMid] EQ value) //found the value
                return arrayMid;
        else if ( sortedArray[arrayMid] GT value) //search left
                binarySearch(sortedArray, startIdx, arrayMid-1, value);
        else if ( sortedArray[arrayMid] LT value) //search right
                binarySearch(sortedArray, arrayMid+1, endIdx, value);
        else if ( startIdx > endIdx ) //value not found.
                return -1;
}

sortedArray = ArraySort(unsoredArray);
result = binarySearch(sortedArray, 1, ArrayLen(sortedArray), value);

</cfscript>

Pat

ps. before you dig into a 3d array. think if an array of structs might work for you better. i dont know the problem, but values like name,type sound like it could be a candidate for an array of structs.

Chad Renando wrote:
Thanks Mark...

"quite easily write yourself a quick binary search algorithm"

you make me chuckle... *chuckle*  I'll get to that right after I whip
up my CFC for splitting atoms.

I'll have a look at the java thing.

Are array's overly bursonsome?  I am putting all my form field
attributes (name, type, etc) in an array, which will likely end up
being 3-dimensions.  The information in the array is coming from 3 or
4 queries, so either I loop through the queries in the body of the
page (I think kinda messy) or loop through my single array.  I am not
adding any looping to create the array.

Cheers,

Chad
who thinks he used up his sigs for the day



On 6/3/05, Mark Mandel <[EMAIL PROTECTED]> wrote:

Is the array sorted in any way?

If it's completely un-sorted, you kinda don't have any options but to
do a O(n) search.

If it is sorted, you could quite easily write yourself a quick binary
search algorithm, while it isn't the fastest searcher in the world, it
is better than looping through all the items.

OR you could just be really lazy, and utilise the contains() Java
function off the myArray.
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Vector.html#contains(java.lang.Object)

Could be lots of fun since it's a 2d array.

You may want to look into alternative data structures for your data if
it's a 2d array and you are finding it hard to manage.

No reason at all you can't leverage Java Collections.

Mark

On 6/3/05, Chad Renando <[EMAIL PROTECTED]> wrote:

'morning all.

What would be the best way to find a particular value in an array?
Ideally, I would like to query my array to get a certain value where a
condition is true, sort of like

SELECT MyArray[1][5]
FROM MyArray
WHERE MyArray[1][2] = "Something"

The altrnative is to loop through the array and grab the value on the
way through, but I am thinking I will take a performance hit,
particularly as I am doing this within a loop already...

<cfloop index="i" from="1" to="#ArrayLen(MyArray)#">
    <cfif MyArray[1][2] = "Something"><cfset MyValue=1></cfif>
</cfloop>

Cheers,

Chad
who uses band names like Rammstein in general conversation 'cause it
just sounds cool

--
E: [EMAIL PROTECTED]
W: www.compoundtheory.com
ICQ: 3094740

---
You are currently subscribed to cfaussie as: [EMAIL PROTECTED]
To unsubscribe send a blank email to [EMAIL PROTECTED]
Aussie Macromedia Developers: http://lists.daemon.com.au/





---
You are currently subscribed to cfaussie as: [email protected]
To unsubscribe send a blank email to [EMAIL PROTECTED]
Aussie Macromedia Developers: http://lists.daemon.com.au/

Reply via email to