# SEARCHING ALGORITHMS

Write an algorithm of linear search in a DATA (linear array) with n elements.
This Algorithm finds the LOC of ITEM in DATA

1. Start

2. Set LOC:=1

3. [Search for ITEM]

Repeat while LOC <= N:

If DATA[LOC] = ITEM, Then:

Write: “Element is at the location”, LOC

Return

[End of if structure]

Set LOC := LOC + 1

[End of Loop]

4. [Unsuccessful] If LOC = N+1 , then:

Set LOC := 0

5. Return

Write an algorithm of Binary search in a DATA (Sorted array) with n elements.

Here DATA is a sorted array with lower bound LB and upper bound UB, and the ITEM is a given item of information. The variables BEG, END and MID denote, beginning, end and middle location of a segment of elements of DATA. This algorithm finds the location LOC of ITEM in DATA or set LOC=NULL

1. Start

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

3. Repeat while BEG <= END and DATA[MID] != ITEM

If ITEM < DATA[MID], then:

Set END := MID – 1

Else:

Set BEG := MID + 1

[End of if structure]

4. Set MID := INT (( BEG + END) / 2)
[End of step 3 loop]

5. If DATA[MID] = ITEM, then:

Set LOC := MID

Else :

Set LOC := NULL

[End of if structure]

6. Return