|
-
March 1st, 2010, 12:39 PM
#1
ternary search algorithm
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?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|