You are on page 1of 21

LINEAR SEARCH

2001 2003 2005 2007 2009 2011 2013

int A 23 40 50 60 10 30 80
1 2 3 4 5 6 7
LINEAR SEARCH
2001 2003 2005 2007 2009 2011 2013

int A 23 40 50 60 10 30 80
1 2 3 4 5 6 7

Element to be searched = 10
LINEAR SEARCH
2001 2003 2005 2007 2009 2011 2013

int A 23 40 50 60 10 30 80
1 2 3 4 5 6 7

Element to be searched = 10

Elements of Array :
A[1] = 23
A[2] = 40
A[3] = 50
A[4] = 60
A[5] = 10
A[6] = 30
A[7] = 80
LINEAR SEARCH
2001 2003 2005 2007 2009 2011 2013

int A 23 40 50 60 10 30 80
1 2 3 4 5 6 7

Element to be searched = 10

Elements of Array :
A[1] = 23
A[2] = 40
A[3] = 50 Element = 10
A[4] = 60
A[5] = 10
A[6] = 30
A[7] = 80
LINEAR SEARCH
2001 2003 2005 2007 2009 2011 2013

int A 23 40 50 60 10 30 80
1 2 3 4 5 6 7

Element to be searched = 10

Elements of Array :
A[1] = 23
A[2] = 40
A[3] = 50 Element = 10
A[4] = 60
A[5] = 10
A[6] = 30
A[7] = 80
LINEAR SEARCH
2001 2003 2005 2007 2009 2011 2013

int A 23 40 50 60 10 30 80
1 2 3 4 5 6 7

Element to be searched = 10

Elements of Array :
A[1] = 23
A[2] = 40
A[3] = 50 Element = 10
A[4] = 60
A[5] = 10
A[6] = 30
A[7] = 80
LINEAR SEARCH
2001 2003 2005 2007 2009 2011 2013

int A 23 40 50 60 10 30 80
1 2 3 4 5 6 7

Element to be searched = 10

Elements of Array :
A[1] = 23
A[2] = 40
A[3] = 50 Element = 10
A[4] = 60
A[5] = 10
A[6] = 30
A[7] = 80
LINEAR SEARCH
2001 2003 2005 2007 2009 2011 2013

int A 23 40 50 60 10 30 80
1 2 3 4 5 6 7

Element to be searched = 10

Elements of Array :
A[1] = 23
A[2] = 40
A[3] = 50 Element = 10
A[4] = 60
A[5] = 10
A[6] = 30
A[7] = 80
LINEAR SEARCH
2001 2003 2005 2007 2009 2011 2013

int A 23 40 50 60 10 30 80
1 2 3 4 5 6 7

Element to be searched = 10

Elements of Array :
A[1] = 23
A[2] = 40
A[3] = 50 Element = 10
A[4] = 60
A[5] = 10
A[6] = 30
A[7] = 80
LINEAR SEARCH
2001 2003 2005 2007 2009 2011 2013

int A 23 40 50 60 10 30 80
1 2 3 4 5 6 7

Element to be searched = 10

Elements of Array : Read Element


A[1] = 23
A[2] = 40 For i = 1 to 7
A[3] = 50 Element = 10 {
A[4] = 60 if(A[i]==Element
A[5] = 10 Print “Item Found”
A[6] = 30 else
A[7] = 80 Print “Item not Found”
}
LINEAR SEARCH
2001 2003 2005 2007 2009 2011 2013

int A 23 40 50 60 10 30 80
1 2 3 4 5 6 7

Element to be searched = 10

Elements of Array : Read Element


A[1] = 23
A[2] = 40 For i = 1 to 7
A[3] = 50 Element = 10 {
A[4] = 60 if(A[i]==Element
A[5] = 10 Print “Item Found”
A[6] = 30 else
A[7] = 80 Print “Item not Found”
}
LINEAR SEARCH
2001 2003 2005 2007 2009 2011 2013

int A 23 40 50 60 10 30 80
1 2 3 4 5 6 7

Element to be searched = 10

Elements of Array : Read Element


A[1] = 23
A[2] = 40 For i = 1 to 7
A[3] = 50 Element = 10 {
A[4] = 60 if(A[i]==Element
A[5] = 10 Print “Item Found”
A[6] = 30 else
A[7] = 80 Print “Item not Found”
}
LINEAR SEARCH
2001 2003 2005 2007 2009 2011 2013

int A 23 40 50 60 10 30 80
1 2 3 4 5 6 7

Element to be searched = 10

Elements of Array : flag = 0;


A[1] = 23 For i = 1 to 7
A[2] = 40 {
A[3] = 50 Element = 10 if(A[i]==Element
A[4] = 60 {
A[5] = 10 loc = I;
A[6] = 30 flag = 1 and break
A[7] = 80 }
}
if flag = 0, then
Print “Item not Found”
Else
Print “Item Found”
LINEAR SEARCH
Linear_Search (A[], LB, UB, ITEM, LOC)
LINEAR SEARCH
Linear_Search (A[], LB, UB, ITEM, LOC)

Step 1 : Set flag=0


LINEAR SEARCH
Linear_Search (A[], LB, UB, ITEM, LOC)

Step 1 : Set flag=0

Step 2 : Repeat steps 2 & 3 For i = LB to UB


LINEAR SEARCH
Linear_Search (A[], LB, UB, ITEM, LOC)

Step 1 : Set flag=0

Step 2 : Repeat steps 2 & 3 For i = LB to UB

Step 3 : if (A[i] = ITEM) Then


LINEAR SEARCH
Linear_Search (A[], LB, UB, ITEM, LOC)

Step 1 : Set flag=0

Step 2 : Repeat steps 2 & 3 For i = LB to UB

Step 3 : if (A[i] = ITEM) Then

Set flag = 1
Set Loc = i
Break;
[end of loop step 2]
LINEAR SEARCH
Linear_Search (A[], LB, UB, ITEM, LOC)

Step 1 : Set flag=0

Step 2 : Repeat steps 2 & 3 For i = LB to UB

Step 3 : if (A[i] = ITEM) Then

Set flag = 1
Set Loc = i
Break;
[end of loop step 2]
Step 4 : if (flag = 1) Then
Print(“Item found at location” + Loc)
else
Print(“Item not found”)
LINEAR SEARCH
Linear_Search (A[], LB, UB, ITEM, LOC)

Step 1 : Set flag=0

Step 2 : Repeat steps 2 & 3 For i = LB to UB

Step 3 : if (A[i] = ITEM) Then

Set flag = 1
Set Loc = i
Break;
[end of loop step 2]
Step 4 : if (flag = 1) Then
Print(“Item found at location” + Loc)
else
Print(“Item not found”)

Step 5 : EXIT
COMPLEXITY

Best Case: Item found at first location i.e., O(1).


Worst Case: Item not found i.e., O(n) for traversing n
elements.
Average Case: Item found in mid of the list i.e.,
O(n/2) = O(n).

Disadvantages

Complexity is O(n) in Worst Case

Consumes large time in case of large


number of elements

You might also like