0% found this document useful (0 votes)
48 views

Linear Search

The document describes the linear search algorithm to search for an element in an array. It initializes a flag to 0, then iterates through each element of the array from index 1 to the last index. If the current element matches the search item, it sets the flag to 1, stores the index, and breaks out of the loop. After the loop, it checks the flag and prints whether the item was found or not found along with the index if found. The time complexity is O(1) in the best case, O(n) in the worst case, and on average O(n).
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Linear Search

The document describes the linear search algorithm to search for an element in an array. It initializes a flag to 0, then iterates through each element of the array from index 1 to the last index. If the current element matches the search item, it sets the flag to 1, stores the index, and breaks out of the loop. After the loop, it checks the flag and prints whether the item was found or not found along with the index if found. The time complexity is O(1) in the best case, O(n) in the worst case, and on average O(n).
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 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