# i need a function that implement a ternary search algorithm in MATLAB? any one could help ?

Is there something that confuses you in the description of the ternary search algorithm?

no i do understand it but my problem is the syntax it self since i never used matlab, i thought if someone could help
thats my code

function [numComparisons, returnindex ] = ternarySearch( array, target, firstIndex , lastIndex)
numComparisons = 0;
lastIndex = length(array);
firstIndex = 1;

mid1 = firstIndex + (lastIndex - 1);
mid2 = lastIndex - (lastIndex - 1);
xmid1 = 0;
xmid2 = 0;

if(array(mid1)== target)
xmid1 = mid1;

elseif(array(mid2) == target)
xmid2 = mid2;
elseif(target < array(mid1))

``````ternarySearch(mid2+1,lastIndex, target);
``````

else

``````ternarySearch(mid1+1,mid2-1,target);
``````

end
end

been a while since I’ve used matlab, but it’s definitely harder to read unformatted

If you put the code between triple backticks (a line of three of these: `, then the code, then a line with 3 of those symbols again) it’ll be much easier for us to read

the error is that any search target always 0 which means that algorithm isnt working

``````function [numComparisons, returnindex ] = ternarySearch( array, target, firstIndex , lastIndex)
numComparisons = 0;
lastIndex = length(array);
firstIndex = 1;

mid1 = firstIndex + (lastIndex - 1) / 3;
mid2 = lastIndex - (lastIndex - 1)/ 3;
xmid1 = 0;
xmid2 = 0;

if(array(mid1)== target)
xmid1 = mid1;

elseif(array(mid2) == target)
xmid2 = mid2;
elseif(target > array(mid1))

ternarySearch(mid2+1,lastIndex, target);
elseif(target < array(mid1))
ternarySearch(firstIndex,mid1 - 1, target);
else

ternarySearch(mid1+1,mid2-1,target);
end
end
``````

This function you’ve defined is a little odd, you’ve defined a function with 4 parameters, then only used 3 of them later on

You also declare two output parameters then don’t seem to assign to them, is that intentional? (probably not, I’m guessing)

Edit: you do assign to one of them, `numComparisons`, so the function returns what you’ve set it to, which is 0

Edit2: forgot to mention, you set the values of `firstIndex` and `lastIndex` immediately inside the function, you don’t want to do that, you should just get them from the function parameters

as i mention it’s only the syntax that is a bit weird for me, would you help me to rewrite it please?
the only param i need is the number of comparisons so i can use it in a graph later on

The syntax looks almost fine, not got matlab installed to check for definitely, it’s the actual code that’s wrong

To return values from a function you assign to your declared output parameters, which you’ve not been doing

You’ve declared a function, `ternarySearch` which takes in 4 parameters, `array`, `target`, `firstIndex` and `lastIndex`

You immediately wipe out the values of `firstIndex` and `lastIndex` (don’t do this if you’re intending to do the recursive calls)

You then call this function (wrongly) with only 3 parameters, e.g. `ternarySearch(mid2+1,lastIndex, target);` You’re missing something here

Also you’re not returning anything useful

To return useful values from a function, you assign to the variables you’ve declared as output parameters (in this case `numComparisons` and `returnIndex`)

Don’t forget to get those values out of the `ternarySearch` calls you make inside `ternarySearch` too, e.g. `[numComparions, returnIndex] = ternarySearch(array, target, newLeftIndex, newRightIndex);` or whatever you name the new indices to search with