Python/C/C++/JAVA

Advanced Practice Programs with Code and Concept

By D.S

Ternery Search in C, C++, Python and Java

Yo, so if you're looking to write a program to search for an element using ternary search with loops and functions, you've come to the right place. Ternary search is a pretty nifty way to find an element in a sorted array by dividing it into three parts and checking the middle two sections for the element each time. By doing this repeatedly, we can hone in on the element we're searching for pretty quickly. Now, to implement this algorithm, you'll want to use some functions and loop structures to make your code modular and efficient. At its core, though, ternary search is all about breaking down problems into smaller chunks - just like figuring out what kind of pizza toppings you want. So why not give it a shot? Who knows - maybe your program will be the key ingredient in someone else's coding project!

(a.) C Program

#include <stdio.h>

      int ternarySearch(int arr[], int l, int r, int x) {
          if (r >= l) {
              int mid1 = l + (r - l) / 3;
              int mid2 = r - (r - l) / 3;
      
              if (arr[mid1] == x)
                  return mid1;
              if (arr[mid2] == x)
                  return mid2;
      
              if (x < arr[mid1])
                  return ternarySearch(arr, l, mid1 - 1, x);
              else if (x > arr[mid2])
                  return ternarySearch(arr, mid2 + 1, r, x);
              else
                  return ternarySearch(arr, mid1 + 1, mid2 - 1, x);
          }
          return -1;
      }
      
      int main() {
          int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
          int n = sizeof(arr) / sizeof(arr[0]);
          int x = 5;
          int result = ternarySearch(arr, 0, n - 1, x);
          if (result == -1)
              printf("Element is not present in array");
          else
              printf("Element is present at index %d", result);
      }
Output:-
Element is present at index 4

(b.) C++ Program

#include <iostream>
      using namespace std;
      
      int ternarySearch(int arr[], int l, int r, int x) {
          if (r >= l) {
              int mid1 = l + (r - l) / 3;
              int mid2 = r - (r - l) / 3;
      
              if (arr[mid1] == x)
                  return mid1;
              if (arr[mid2] == x)
                  return mid2;
      
              if (x < arr[mid1])
                  return ternarySearch(arr, l, mid1 - 1, x);
              else if (x > arr[mid2])
                  return ternarySearch(arr, mid2 + 1, r, x);
              else
                  return ternarySearch(arr, mid1 + 1, mid2 - 1, x);
          }
          return -1;
      }
      
      int main() {
          int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
          int n = sizeof(arr) / sizeof(arr[0]);
          int x = 5;
          int result = ternarySearch(arr, 0, n - 1, x);
          if (result == -1)
              cout << "Element is not present in array" << endl;
          else
              cout << "Element is present at index " << result << endl;
          return 0;
      }
Output:-
Element is present at index 4

(c.) Python Program

def ternarySearch(arr, l, r, x):
    if r >= l:
        mid1 = l + (r - l) // 3
        mid2 = r - (r - l) // 3

        if arr[mid1] == x:
            return mid1
        if arr[mid2] == x:
            return mid2

        if x < arr[mid1]:
            return ternarySearch(arr, l, mid1 - 1, x)
        elif x > arr[mid2]:
            return ternarySearch(arr, mid2 + 1, r, x)
        else:
            return ternarySearch(arr, mid1 + 1, mid2 - 1, x)
    return -1

arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
x = 5
result = ternarySearch(arr, 0, len(arr) - 1, x)
if result == -1:
    print("Element is not present in array")
else:
    print("Element is present at index", result)
Output:-
Element is present at index 4

(d.) Java Program

public class Main {
      public static int ternarySearch(int arr[], int l, int r, int x) {
          if (r >= l) {
              int mid1 = l + (r - l) / 3;
              int mid2 = r - (r - l) / 3;
  
              if (arr[mid1] == x)
                  return mid1;
              if (arr[mid2] == x)
                  return mid2;
  
              if (x < arr[mid1])
                  return ternarySearch(arr, l, mid1 - 1, x);
              else if (x > arr[mid2])
                  return ternarySearch(arr, mid2 + 1, r, x);
              else
                  return ternarySearch(arr, mid1 + 1, mid2 - 1, x);
          }
          return -1;
      }
  
      public static void main(String args[]) {
          int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
          int x = 5;
          int result = ternarySearch(arr, 0, arr.length - 1, x);
          if (result == -1)
              System.out.println("Element is not present in array");
          else
              System.out.println("Element is present at index " + result);
      }
  }
Output:-
Element is present at index 4

How did you feel about this post?

😍 🙂 😐 😕 😡

Was this helpful?

👍 👎