logarithmicSearch
Posted: Fri Jun 23, 2017 1:20 am
Hi,
I found this code on internet, but I doesn't find anything, it returns 0 to me:
########CODE to copy and paste#######
function logarithmicSearch @pArray pItem pLeft pRight
local tIndex
local tResult
# create a new range pointer that points to the item that lies
# right between the left and right range pointers
put round ((pLeft + pRight) / 2) into tIndex
# if we have found the matching item then stop processing and return it
if pArray[tIndex] = pItem then
put tIndex into tResult
# if any of the range pointers have the same value
# then the item we are looking for does not exist in the array and we return 0
else if (pLeft = pRight or pLeft = tIndex or pRight = tIndex) then
put 0 into tResult
# if we have not yet found a match and none of the range pointers have the same
# value then call logarithmicSearch again with a smaller search range
# we are effectively halving the search area, each time we call logarithmicSearch
else if pArray[tIndex] > pItem then
put logarithmicSearch (pArray, pItem, pLeft, tIndex) into tResult
else
put logarithmicSearch (pArray, pItem, tIndex, pRight) into tResult
end if
return tResult
end logarithmicSearch
#####END OF CODE generated by http://tinyurl.com/j8xf3xq with livecode 9.0.0-dp-1#####
What's wrong?
I found this code on internet, but I doesn't find anything, it returns 0 to me:
########CODE to copy and paste#######
function logarithmicSearch @pArray pItem pLeft pRight
local tIndex
local tResult
# create a new range pointer that points to the item that lies
# right between the left and right range pointers
put round ((pLeft + pRight) / 2) into tIndex
# if we have found the matching item then stop processing and return it
if pArray[tIndex] = pItem then
put tIndex into tResult
# if any of the range pointers have the same value
# then the item we are looking for does not exist in the array and we return 0
else if (pLeft = pRight or pLeft = tIndex or pRight = tIndex) then
put 0 into tResult
# if we have not yet found a match and none of the range pointers have the same
# value then call logarithmicSearch again with a smaller search range
# we are effectively halving the search area, each time we call logarithmicSearch
else if pArray[tIndex] > pItem then
put logarithmicSearch (pArray, pItem, pLeft, tIndex) into tResult
else
put logarithmicSearch (pArray, pItem, tIndex, pRight) into tResult
end if
return tResult
end logarithmicSearch
#####END OF CODE generated by http://tinyurl.com/j8xf3xq with livecode 9.0.0-dp-1#####
What's wrong?