Developing ternary search algorithm

Suppose we have an array as we have in a binary search, {3, 8, 11, 15, 16, 23, 28, 30, 32, 39, 42, 44, 47, 48, 50}, and want to search for a value of 16. The array contains 15 elements, so we will have the fifth index as the middle-left index (middleLeftIndex = 4), and ninth index as the middle-right index (middleRightIndex = 8). By using these two indexes, we can find the searched value in each area using the ternary search algorithm itself (recursive invocation). The code of a ternary search algorithm in C++ is as follows:

int TernarySearch(    int arr[],    int startIndex,    int endIndex,    int val){    // Only perform searching process    // if the end index is higher than    // or equals to start index if(startIndex ...

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.