Searching elements of an array using binary search





Algorithm:



(Binary Search) BINARY(DATA, LB, UB, ITEM, LOC)

Step1: [Initialize segment variables.]
       Set BEG=LB, END = UB and MID = INT((BEG + END)/2).

Step2: Repeat Steps 3 and 4 while BEG<=END and DATA[MID]!=ITEM.

Step3: If ITEM<DATA[MID], then:
         Set END = MID - 1.
       Else:
         Set BEG = MID +1.
       [End of If structure.]

Step4: Set MID = INT((BEG + END)/2).
      [End of Step 2 loop.]

Step5: If DATA[MID]=ITEM, then:
         Set LOC = MID.
       Else
         Set LOC=NULL
       [End of If structure.]

Step6: Exit.


Comments

Popular posts from this blog

Node.js Cheat Sheet

Codeigniter ! Simple But Powerful

Enhancing Supply Chain Security and Cloud-Native Development with Chainguard