chore(C & CPP): Ternary Search (#280)

* added Ternary search in c and c++

* added ternary search in c

* added Ternary search link

* correct sequence of numbers

* requested changes for c code

* requested changes for c++ code
pull/281/head
Ujjwal 2021-05-04 15:58:56 +05:30 committed by GitHub
parent e8660430d8
commit 4d6a7cfa87
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 163 additions and 1 deletions

View File

@ -31,3 +31,4 @@
## Searching
- [Binary Search](searching/Binary-search.c)
- [Jump Search](searching/Jump-search.c)
- [Ternary Search](searching/Ternary-search.c)

View File

@ -0,0 +1,80 @@
/* Program to search data in given array using ternary search */
#include <stdio.h>
#include <stdlib.h>
// Function to search data in a given array
int Ternary_search(int *arr,int l,int r,int to_search)
{
//split current array into 3 part
int Mid1=l+(r-l)/3,Mid2=r-(r-l)/3;
// data not found condition
if(l>r)
return -1;
// if data found in Mid1 index then return index number
else if(arr[Mid1]==to_search)
return Mid1;
// if data found in Mid2 index then return the index number
else if(arr[Mid2]==to_search)
return Mid2;
//else if data not found at index Mid1 and Mid2 then search in middle part of array
else if(arr[Mid1]<to_search&&arr[Mid2]>to_search)
{
Mid1=Mid1+1;
Mid2=Mid2-1;
}
// if data is less than Mid index data then search in before index number of current
else if(arr[Mid1]>to_search)
{
Mid2=Mid1-1;
Mid1=l;
}
// if data is less than Mid index data then search in after index number of current
else if(arr[Mid2]<to_search)
{
Mid1=Mid2+1;
Mid2=r;
}
// recursive calling the function
return Ternary_search(arr,Mid1,Mid2,to_search);
}
// driver code
int main()
{
int n,*arr,i,to_search,res;
printf("Enter size: ");
// taking input
scanf("%d",&n);
// if the size entered is negative then make it positive one
if(n<0)
n=n*-1;
// dynamically allocating array
arr=(int*)malloc(sizeof(int)*n);
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
printf("Enter the number to search: ");
// taking data to search
scanf("%d",&to_search);
// calling function to search index of given number and assign it in res
res=Ternary_search(arr,0,n,to_search);
// if res contain -1 that means data not found
if(res==-1)
printf("Data Not Found");
else
printf("Data found at index no: %d",res);
return 0;
}
/*
Input: Enter size: 5
1,2,3,4,5
Enter the number to search: 2
Output: Data found at index no: 1
Time complexity: O(log3 n)
Space complexity: O(1)
*/

View File

@ -29,7 +29,8 @@
2. [Jump Search](Searching/jump-search.cpp)
3. [Binary Search](Searching/binary-search.cpp)
4. [Finding squareroot using Binary search](Searching/sqrt-monotonic-binary-search.cpp)
3. [KMP String Searching](Searching/kmp.cpp)
5. [KMP String Searching](Searching/kmp.cpp)
6. [Ternary Search](Searching/Ternary-search.cpp)
## Stacks
1. [Balancing Parenthesis](Stacks/balanced-parenthesis.cpp)

View File

@ -0,0 +1,80 @@
/* Program to search data in given array using ternary search */
#include <iostream>
using namespace std;
// Function to search data in a given array
int Ternary_search(int *arr,int l,int r,int to_search)
{
//split current array into 3 part
int Mid1=l+(r-l)/3,Mid2=r-(r-l)/3;
// data not found condition
if(l>r)
return -1;
// if data found in Mid1 index then return index number
else if(arr[Mid1]==to_search)
return Mid1;
// if data found in Mid2 index then return the index number
else if(arr[Mid2]==to_search)
return Mid2;
//else if data not found at index Mid1 and Mid2 then search in middle part of array
else if(arr[Mid1]<to_search&&arr[Mid2]>to_search)
{
Mid1=Mid1+1;
Mid2=Mid2-1;
}
// if data is less than Mid index data then search in before index number of current
else if(arr[Mid1]>to_search)
{
Mid2=Mid1-1;
Mid1=l;
}
// if data is less than Mid index data then search in after index number of current
else if(arr[Mid2]<to_search)
{
Mid1=Mid2+1;
Mid2=r;
}
// recursive calling the function
return Ternary_search(arr,Mid1,Mid2,to_search);
}
// driver code
int main()
{
int n,*arr,i,to_search,res;
cout<<"Enter size: ";
// taking input
cin>>n;
// if size is negative then make it positive one
if(n<0)
n=n*-1;
// dynamically allocating array
arr=(int*)malloc(sizeof(int)*n);
for(i=0;i<n;i++)
{
cin>>arr[i];
}
cout<<"Enter the number to search: ";
// taking data to search
cin>>to_search;
// calling function to search index of given number and assign it in res
res=Ternary_search(arr,0,n,to_search);
// if res contain -1 that means data not found
if(res==-1)
cout<<"Data Not Found";
else
cout<<"Data found at index no: "<<res;
return 0;
}
/*
Input: Enter size: 5
1,2,3,4,5
Enter the number to search: 2
Output: Data found at index no: 1
Time complexity: O(log3 n)
Space complexity: O(1)
*/