title: Ternary search in C
body: <p>I want to do a ternary search for integers in C ... I have tried it...but it's not working well for particular cases. Please help me to remove the bugs from the following program--</p>
  
  <p>My attempt:</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;conio.h&gt;
  void  tsearch(int *a,int i,int j,int k);
  main() {
    int a[30],n,i,k;
          printf("\nEnter n:");
    scanf("%d",&amp;n);
    printf("\nEnter nos in ascending order:");
    for(i=0;i&lt;n;i++)
        scanf("%d",&amp;a[i]);
    printf("Enter no to search:");
    scanf("%d",&amp;k);
    tsearch(a,0,n-1,k);
  
    getch();
  }
  
  void tsearch(int *a,int i,int j,int k) {
    int m1,m2;
    m1=(i+j)/3;
    m2=2*(i+j)/3;
    if(k==a[m1])
     {
      printf("\nno found at %d",m1);
      return;
     }
    else  if(k==a[m2])
     {
      printf("\nno found at %d",m2);
      return;
     }
    if(k&lt;a[m1])
      return(tsearch(a,i,m1-1,k));
    if(k&gt;a[m2])
      return(tsearch(a,m2+1,j,k));
    else   
      return(tsearch(a,m1+1,m2-1,k));
  }   
  </code></pre>
  
  <hr>
  
  <p>It (only) terminates if the number to be searched is present in one of last 2-3 locations (of array). Where i have committed the mistake???</p>