1 Reply Latest reply on Mar 3, 2010 1:26 PM by 791266

    ternary search algorithm

      any1 no how we can improve a ternary search algorithm?

      ternary(V, s, e)
      if s > e
      return -1
      m1 ← (e-s)/3 + s
      m2 ← 2*(e-s)/3 + s
      if V = A[ m1 ]
      return m1
      else if V = A[ m2 ]
      return m2
      else if V < A[ m1 ]
      return ternary(V, s, m1-1)
      else if V < A[ m2 ]
      return ternary(V, m1+1, m2-1)
      return ternary(V, m2+1, e)

      i read about it in wikipedia so i no what it does but surely i can improve it right? I considered skipping the first two if statments but it didnt make sense as i went into it. why go through the elements in any of the 3 segments if v is already in m1/m2.

      So any ideas?