1 Reply Latest reply: Mar 3, 2010 7:26 AM by 791266 RSS

    ternary search algorithm

    843853
      any1 no how we can improve a ternary search algorithm?

      ternary(V, s, e)
      if s > e
      return -1
      else
      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)
      else
      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?