answersLogoWhite

0

How do you write c code for ternary search?

Updated: 8/18/2019
User Avatar

Wiki User

12y ago

Best Answer

this procedure work for ternary search

int tsearch(int *a,int i,int j,int k)

{

int m1,m2,len;

len = j - i + 1 ;

m1=i + (int)floor((float)(len))/3;

m2=i + (int)ceil((float)(len))/3;

if(k==a[m1])

{

printf("\nno found at %d",m1);

return m1;

}

else if(k==a[m2])

{

printf("\nno found at %d",m2);

return m2;

}

if(len!= 0) {

if(k<a[m1])

return(tsearch(a,i,m1-1,k));

if(k>a[m2])

return(tsearch(a,m2+1,j,k)); }

else

return -1 ;

}

User Avatar

Wiki User

12y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you write c code for ternary search?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How can you download c compiler?

write a c program to fine largest/smallest of 3no (using ?:ternary operator/conditional operator)


Write a c plus plus program to find the largest among three numbers using ternary operators?

R = (A &gt; B &amp;&amp; A &gt; C) ? A : (B &gt; C) ? B : C; // parentheses not necessary - for clarity only


Best-First Search with C Code?

execution


How do you write a algorithm for finding maximum of 2 numbers in c?

You can use the ternary operator, in an expression such as: result = a &gt; b ? a : b; This is equivalent to: if (a &gt; b) result = a; else result = b;


Write a programme to find greater among two numbers in c plus plus?

You could use an if, but the ternary operator is especially compact for this purpose: result = a &gt; b ? a : b;


What is syntax in c?

syntax is the way you write your code in it defines the meaning of keywords &amp; how to write


What is ternary operation in c language?

exp ? exp : exp


Enumerate the types of selection constructs in c plus plus?

Selection constructs in C++if...elseswitch/caseconditional ternary operator (?:)


What is template in C?

C does not support templates. Templates are supported by C++ and make it possible to write generic code.


Write c code for extened euclid algorithm?

This is a request, not a question.


What is Rondo Ternary and Binary form?

In music Binary, Ternary and Rondo are like the layout of a song. So say A represents a verse, B a chorus and C would represent a Bridge! Binary would be A,B (Verse, chorus) Ternary would be A,B,A (Verse, chorus, verse) Then Rondo would be A,B,A,C,A (Verse, chorus, verse, bridge, verse)


What is condional operator?

Conditional Operator- Its the only ternary operator in c/c++.- Its syntax is-(condition)?statement1:statement2;-Shruti Jain