consider linear search Observe that if the sequence A is sor

consider linear search. Observe that if the sequence A is sorted, we can check the midpoint of the sequence against v and eliminate half of the sequence from further consideration. The binary search algorithm repeats this procedure, halving the size of the remaining portion of thesequence each time. Write pseudo code, either iterative or recurrsive, for binary search.

Solution

Linear search pseudo code:
============================================

BINARY_SEARCH(A,N,ITEM,POS)

This procedure search an element ITEM in a sorted array A of Size N using binary search technique. If ITEM is found it will assign to its position in pos
otherwise it will assign 0.

1. set low:1, high:N
2. repeat step 3 & 4 while low<=high:
3. set MID:=INT((low+high)/2)
4. IF A[MID]==ITEM, THEN:
(a) set pos:=MID
(b) return
ELSE IF ITEM>A[MID], THEN:
set: low:=MID+1
ELSE
set high:=MID-1

[End of IF structure]
[END of step 2 loop]
5. set pos:=0
6. return

Thanks a lot

consider linear search. Observe that if the sequence A is sorted, we can check the midpoint of the sequence against v and eliminate half of the sequence from fu

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site