Applying the ternary search algorithm

To invoke the TernarySearch() function, we can use the following main() function:

int main(){    cout << "Ternary Search" << endl;    // Initialize a new array    int arr[] = {3, 8, 11, 15, 16, 23, 28, 30, 32, 39, 42, 44, 47,       48, 50};    int arrSize = sizeof(arr)/sizeof(*arr);    // Define value to be searched    int searchedValue = 16;    // Find the searched value using Ternary Search    int i = TernarySearch(arr, 0, arrSize - 1, searchedValue);    // Notify user the result    // if the return is not -1,    // the searched value is found    if(i != -1)    {        cout << searchedValue << " is found in index ";        cout << i << endl;    }    else    {        cout << "Could not find value " << searchedValue;        cout << endl;    }    return 0;}

If we build and run the preceding ...

Get C++ Data Structures and Algorithms now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.