You are on page 1of 6

Data Structure ( Unit – 5 )

Linear_Search (A, N, ITEM)


Description : Here A is an array having N elements. ITEM is the value to be searched.
Step 1 : Repeat For J = 1 to N
Step 2 : If (ITEM == A[J]) Then
Print : ITEM found at location J
Return
[End of If and End of For]
Step 3 : If (J > N) Then
Print : ITEM doesn’t exist
[End of If]
Step 4 : Exit

LINEAR_SEARCH(A, N, VAL)
Description : Here A is an array having N elements. VAL is the value to be searched.
Step 1 : [INITIALIZE] SET POS = -1
Step 2 : [INITIALIZE] SET I = 1
Step 3 : Repeat Step 4 While I <= N
Step 4 : IF A[I] = VAL
SET POS = I
PRINT POS
Go to Step 6
[END OF IF]
SET I = I + 1
[END OF LOOP]
Step 5 : IF POS = –1
PRINT "VALUE IS NOT PRESENTIN THE ARRAY"
[END OF IF]
Step 6 : EXIT

Prof. Arpita R. Ojha Page 1


Data Structure ( Unit – 5 )

Binary_Search (A, N, ITEM, BEG, END, MID)

Description : Here A is a sorted array having N elements. ITEM is the value to be

searched. BEG denotes first element and END denotes last element in the array. MID

denotes the middle value.

Step 1 : Set BEG = 1 and END = N

Step 2 : Set MID = (BEG + END) / 2

Step 3 : Repeat While (BEG <= END) and (A[MID] ≠ ITEM)

Step 4 : If (ITEM < A[MID]) Then

Set END = MID – 1

Else

Set BEG = MID + 1

[End of If]

Step 5 : Set MID = (BEG + END) / 2

[End of While Loop]

Step 6 : If (A[MID] == ITEM) Then

Print : ITEM exists at location MID

Else

Print : ITEM doesn’t exist

[End of If]

Step 7 : Exit

Prof. Arpita R. Ojha Page 2


Data Structure ( Unit – 5 )

BINARY_SEARCH(A, lower_bound, upper_bound, VAL)

Description : Here A is a sorted array having N elements. ITEM is the value to be

searched. LB denotes first element and UB denotes last element in the array. MID

denotes the middle value.

Step 1 : [INITIALIZE] SET BEG = LB

END = UB, POS = - 1

Step 2 : Repeat Steps 3 and 4 while BEG <= END

Step 3 : SET MID = (BEG + END)/2

Step 4 : IF A[MID] = VAL

SET POS = MID

PRINT POS

Go to Step 6

ELSE IF A[MID] > VAL

SET END = MID - 1

ELSE

SET BEG = MID + 1

[END OF IF]

[END OF LOOP]

Step 5 : IF POS = -1

PRINT “VALUE IS NOT PRESENT IN THE ARRAY”

[END OF IF]

Step 6 : EXIT

Prof. Arpita R. Ojha Page 3


Data Structure ( Unit – 5 )

Bubble_Sort (A[], N)

Description: Here A is an unsorted array having N elements.

Step 1 : Repeat For J = 0 to N

Step 2 : Repeat For K = 1 to N-J

Step 3 : If (A[K] > A[K+1]) Then

Interchange A[K] and A[K+1]

[End of If]

[End of Step 2 For Loop]

[End of Step 1 For Loop]

Step 4 : Exit

BUBBLE_SORT(A, N)

Description: Here A is an unsorted array having N elements.

Step 1 : Repeat Step 2 For 1 = to N-1

Step 2 : Repeat For J = to N – I

Step 3 : IF A[J] > A[J+ 1]

SWAP A[J] and A[J+1]

[END OF INNER LOOP]

[END OF OUTER LOOP]

Step 4 : EXIT

Prof. Arpita R. Ojha Page 4


Data Structure ( Unit – 5 )

Insertion_Sort (A[], N)
Description : Here A is an unsorted array having N elements.

Step 1 : Repeat For J = 2 to N

Step 2 : Set TEMP = A[J]

Step 3 : Set K = J - 1

Step 4 : Repeat While (K >= 1) and (A[K] > TEMP)

Step 5 : Set A[K+1] = A[K]

Step 6 : Set K = K - 1

[End of While Loop]

Step 7 : Set A[K+1] = TEMP


[End of For Loop]

Step 8 : Exit

INSERTION_SORT (ARR, N)

Step 1 : Repeat Steps 2 to 5 for K = 1 to N – 1

Step 2 : SET TEMP = ARR[K]

Step 3 : SET J = K - 1

Step 4 : Repeat while TEMP <= ARR[J]

SET ARR[J + 1] = ARR[J]

SET J = J - 1
[END OF INNER LOOP]

Step 5 : SET ARR[J + 1] = TEMP


[END OF LOOP]

Step 6 : EXIT
Prof. Arpita R. Ojha Page 5
Data Structure ( Unit – 5 )

Selection_Sort (A[], N)
Description: Here A is an unsorted array having N elements.

Step 1 : Repeat For J = 1 to N

Step 2 : Set MIN = J

Step 3 : Repeat For K = J+1 to N

Step 4 : If (A[K] < A[MIN]) Then

Step 5 : Set MIN = K

[End of If]
[End of Step 3 For Loop]

Step 6 : Interchange A[J] and A[MIN]

[End of Step 1 For Loop]

Step 7 : Exit

SELECTION_SORT(ARR, N)

Step 1 : Set MIN = ARR[K]

Step 2 : Set POS = K

Step 3 : Repeat for J = K + 1 to N

If MIN > ARR[J]

Set MIN = ARR[J]

Set POS = J
[END OF IF]

Step 3 : SWAP ARR[K] with ARR[POS]


[END OF LOOP]

Step 4 : EXIT
Prof. Arpita R. Ojha Page 6

You might also like