Here is the finished one:
<CFSCRIPT>
/**
* int arrayFind(Array, item, ["textnocase"|"numeric"]);
*
* Searches an array for a specific item
* and returns the positon of that item
* or 0 if that item is not found.
* text searches are case sensitive.
* uses the binary search algorithm
*/
function arrayFind(collection, item){
//what kind of array
var type = "textnocase";
if(arrayLen(arguments) gt 2 AND arguments[3]) {
type = arguments[3];
}
//need a sorted array for this algorithm
arraySort(collection,type);
floor = 1;
currentPos = arrayLen(collection);
roof = currentPos;
//should never really finish this loop, here
//for safety.
while(roof-floor gt 1){
//split the currentPos in half
currentPos = ceiling((roof - floor)/2 + floor);
switch(compareNoCase(collection[currentPos],item)){
case 1:
roof = currentPos;
break;
//equal
case 0:
return currentPos;
break;
case -1:
floor = currentPos;
break;
}
if(currentPos eq 1 OR currentPos eq arrayLen(collection)){
break;
}
}
return 0;
}
</CFSCRIPT>
Rob
Certified Organic
"When you put things in quotes, people think someone actually said it."
http://treebeard.sourceforge.net
http://ruinworld.sourceforge.net
Scientia Est Potentia
-----Original Message-----
From: Rob Rohan [mailto:[EMAIL PROTECTED]]
Sent: Monday, November 25, 2002 1:41 PM
To: CF-Talk
Subject: RE: CFMX array sort
Ok, here is a more specific question. I am writting an arrayFind function
that (mostly) uses the binary search algorithm - which needs to have a
sorted array. I have run several test on serial search vs. my search and see
little difference. Is the arraySort that expensive? Perhaps the array is too
small to see the difference.
Again it's not perfect right now as the lower half doesn't shift up, but if
some one could check to see if it is faster I would appreciate it.
If you do an output while in the function you'll see that it only has to hit
a couple times to find the index.
Thanks
Rob
<CFSCRIPT>
/**
* int arrayFind(Array, item, ["textnocase"|"numeric"]);
*
* Searches an array for a specific item
* and returns the positon of that item
* or 0 if that item is not found.
*/
function arrayFind(collection, item){
//what kind of array
var type = "textnocase";
if(arrayLen(arguments) gt 2 AND arguments[3]) {
type = arguments[3];
}
//need a sorted array for this algorithm
arraySort(collection,type);
currentPos = arrayLen(collection);
roof = currentPos;
//should never really finish this loop, here
//for safety.
for(x=1; x lte arrayLen(collection); x=x+1){
//split the currentPos in half
currentPos = ceiling(currentPos / 2);
switch(compareNoCase(collection[currentPos],item)){
case 1:
roof = currentPos;
currentPos = currentPos;
break;
//equal
case 0:
return currentPos;
break;
case -1:
currentPos = currentPos + roof;
break;
}
if(currentPos eq 1 OR currentPos eq arrayLen(collection)){
break;
}
}
return 0;
}
</CFSCRIPT>
-----Original Message-----
From: Jeffry Houser [mailto:[EMAIL PROTECTED]]
Sent: Monday, November 25, 2002 1:20 PM
To: CF-Talk
Subject: Re: CFMX array sort
Presumably some form of quicksort, however I do not know for a fact.
At 12:58 PM 11/25/2002 -0800, you wrote:
>Anyone know what algorithm arraySort() uses?
>
>Rob
>
>Certified Organic
>"When you put things in quotes, people think someone actually said it."
>http://treebeard.sourceforge.net
>http://ruinworld.sourceforge.net
>Scientia Est Potentia
>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Archives: http://www.houseoffusion.com/cf_lists/index.cfm?forumid=4
Subscription:
http://www.houseoffusion.com/cf_lists/index.cfm?method=subscribe&forumid=4
FAQ: http://www.thenetprofits.co.uk/coldfusion/faq
This list and all House of Fusion resources hosted by CFHosting.com. The place for
dependable ColdFusion Hosting.