TheAlgorithms / MATLAB-Octave

This repository contains algorithms written in MATLAB/Octave. Developing algorithms in the MATLAB environment empowers you to explore and refine ideas, and enables you test and verify your algorithm.
MIT License
364 stars 173 forks source link

Fix ternary search #84

Closed MaximSmolskiy closed 3 years ago

MaximSmolskiy commented 3 years ago

Current implementation has several errors and doesn't work.

In Octave:

>> ternarySearch(1:10, 5, 1, 10)
error: max_recursion_depth exceeded
error: called from
    ternarySearch
    ternarySearch at line 28 column 5

In MATLAB:

>> ternarySearch(1:10, 5, 1, 10)
Out of memory. The likely cause is an infinite recursion within the program.

Error in ternarySearch (line 28)
    ternarySearch(array,mid2+1,lastIndex, target);