ternary search c++
C++
// C++ program to illustrate
// recursive approach to ternary search
#include <bits/stdc++.h>
using namespace std;
// Function to perform Ternary Search
int ternarySearch(int l, int r, int key, int ar[])
{
if (r >= l) {
// Find the mid1 and mid2
int mid1 = l + (r - l) / 3;
int mid2 = r - (r - l) / 3;
// Check if key is present at any mid
if (ar[mid1] == key) {
return mid1;
}
if (ar[mid2] == key) {
return mid2;
}
// Since key is not present at mid,
// check in which region it is present
// then repeat the Search operation
// in that region
if (key < ar[mid1]) {
// The key lies in between l and mid1
return ternarySearch(l, mid1 - 1, key, ar);
}
else if (key > ar[mid2]) {
// The key lies in between mid2 and r
return ternarySearch(mid2 + 1, r, key, ar);
}
else {
// The key lies in between mid1 and mid2
return ternarySearch(mid1 + 1, mid2 - 1, key, ar);
}
}
// Key not found
return -1;
}
// Driver code
int main()
{
int l, r, p, key;
// Get the array
// Sort the array if not sorted
int ar[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
// Starting index
l = 0;
// length of array
r = 9;
// Checking for 5
// Key to be searched in the array
key = 5;
// Search the key using ternarySearch
p = ternarySearch(l, r, key, ar);
// Print the result
cout << "Index of " << key
<< " is " << p << endl;
// Checking for 50
// Key to be searched in the array
key = 50;
// Search the key using ternarySearch
p = ternarySearch(l, r, key, ar);
// Print the result
cout << "Index of " << key
<< " is " << p << endl;
}
// This code is contributed
// by Akanksha_Rai
Also in C++:
- Title
- visual studio 2019 read and write text file c++
- Category
- C++
- Title
- c++ max of array
- Category
- C++
- Title
- removing repeated characters in a string c++
- Category
- C++
- Title
- is not a nonstatic data member or base class of class
- Category
- C++
- Title
- maximum subarray sum equal with K in c++
- Category
- C++
- Title
- get elements of 2d array c++
- Category
- C++
- Title
- minimum swaps to sort an array
- Category
- C++
- Title
- c++ random numbers
- Category
- C++
- Title
- % operator in c++
- Category
- C++
- Title
- convert stirng to int c++
- Category
- C++
- Title
- c++ read matttrix from text file
- Category
- C++
- Title
- how do for loops on c++
- Category
- C++
- Title
- shortest path with bfs in c++
- Category
- C++
- Title
- c++ convert int to double
- Category
- C++
- Title
- c++ switch case statement
- Category
- C++
- Title
- split string at index c++
- Category
- C++
- Title
- rosrun actionlib_msgs genaction.py
- Category
- C++
- Title
- multiset c++
- Category
- C++
- Title
- find height of a tree
- Category
- C++
- Title
- insert at position in vector c++
- Category
- C++
- Title
- placement new c++
- Category
- C++
- Title
- c++ for loop
- Category
- C++
- Title
- find in set of pairs using first value cpp
- Category
- C++
- Title
- c++ how to add something at the start of a vector
- Category
- C++
- Title
- c++ function return array
- Category
- C++
- Title
- two elements with difference K in c++
- Category
- C++
- Title
- how to delete a node c++
- Category
- C++
- Title
- compile c++ linux
- Category
- C++
- Title
- fast input output in c++
- Category
- C++
- Title
- index string c++
- Category
- C++
- Title
- c++ parse int
- Category
- C++
- Title
- add two numbers in c++
- Category
- C++
- Title
- array<string, 7> c++
- Category
- C++
- Title
- power in c++
- Category
- C++
- Title
- how to compare lower case character to uppercase cpp
- Category
- C++
- Title
- quick sort predefined function in c++
- Category
- C++
- Title
- insertion c++
- Category
- C++
- Title
- c++ ros publisher
- Category
- C++
- Title
- if esle in c++
- Category
- C++
- Title
- what does count function do in hashmap
- Category
- C++
- Title
- how to sort an array according to another array c++
- Category
- C++
- Title
- c++ while true
- Category
- C++
- Title
- FInd the element which appears more than n/2 times C++
- Category
- C++
- Title
- pbds in c++
- Category
- C++
- Title
- how to declare a vector in c++
- Category
- C++
- Title
- int max in c++
- Category
- C++
- Title
- initialising 2d vector
- Category
- C++
- Title
- caesar cipher program in c++
- Category
- C++
- Title
- class is replace by structure
- Category
- C++
- Title
- how print fload wiht 3 decimal in c++
- Category
- C++
- Title
- create a 2d array c++
- Category
- C++
- Title
- Create a program that finds the minimum value in these numbers
- Category
- C++
- Title
- fill c++
- Category
- C++
- Title
- c++ clear stream
- Category
- C++
- Title
- arrays in C++
- Category
- C++
- Title
- c++ class constructor
- Category
- C++
- Title
- how to find length of string in c++
- Category
- C++
- Title
- euler's totient function c++
- Category
- C++
- Title
- c++ string^ to char*
- Category
- C++
- Title
- how to delete something in an array c++
- Category
- C++
- Title
- multiple words C++ in same
- Category
- C++
- Title
- convert decimal to binary in c++
- Category
- C++
- Title
- c++ tutorial
- Category
- C++
- Title
- array search c++
- Category
- C++
- Title
- remove item from layout
- Category
- C++
- Title
- kruskal's algorithm c++ hackerearth
- Category
- C++
- Title
- passing a vector to a function c++
- Category
- C++
- Title
- restting a queue stl
- Category
- C++
- Title
- c++ get length of array
- Category
- C++
- Title
- c++ base 10 to base 2
- Category
- C++
- Title
- c++ return multiple values
- Category
- C++
- Title
- namespace c++
- Category
- C++
- Title
- string input
- Category
- C++
- Title
- varint index
- Category
- C++
- Title
- delete a double pointer c++
- Category
- C++
- Title
- how to take input in C++ in coding
- Category
- C++
- Title
- how to append one vector to another c++
- Category
- C++
- Title
- sum of integer in array c++
- Category
- C++
- Title
- intersection between vector c++
- Category
- C++
- Title
- c++ excel cell blank cells
- Category
- C++
- Title
- first prime numbers less than
- Category
- C++
- Title
- Runtime Error: Runtime ErrorBad memory access (SIGBUS)
- Category
- C++
- Title
- what is time complexity of insertion sort
- Category
- C++
- Title
- to_string c++
- Category
- C++
- Title
- convert int to string c++
- Category
- C++
- Title
- Temporary file using MSFT API in cpp
- Category
- C++
- Title
- how to remove maximum number of characters in c++ cin,ignore
- Category
- C++
- Title
- c++ ambigous error
- Category
- C++
- Title
- rand c++
- Category
- C++
- Title
- switch case sinax c++
- Category
- C++
- Title
- runtime array size c++
- Category
- C++
- Title
- log base e synthax c++
- Category
- C++
- Title
- gta san andreas
- Category
- C++
- Title
- Operator overloading in C++ Programming
- Category
- C++
- Title
- initialise 2d vector in c++
- Category
- C++
- Title
- c++ ternary operator
- Category
- C++
- Title
- char size length c++
- Category
- C++
- Title
- c++ functions
- Category
- C++
- Title
- nearest integer rounding in c++
- Category
- C++
- Title
- error: ISO C++ forbids comparison between pointer and integer [-fpermissive] if(s[i] != "b"){
- Category
- C++