let T be a heap sorting n keys. Give an efficient algorithm for reporting all the keys in T that are smaller than or equal to a given quary key x (which is not necessarily in T). Keys do not need to be reported in sorted order. The algorithm should run in O(k) time where k is the number of keys reported.
p1 point to head, p2 points to tail
while(p1< p2)
{
if(*p1< k&&*p2>k) {p1++;p2--;}
if(*p1>k&&*p2< k) {swap(*p1,*p2);p1++;p2--;}
if(*p1< k&&*p2< k) {p1++;}
if(*p1>k&&*p2>k) {p2--;}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment