SlideShare a Scribd company logo
8
Most read
LINEAR SEARCH ALGORITHM
Algorithm involves checking all the elements
of the array(or any other structure) one by
one and in sequence until the desired result
is found.
Daily life example
If you are asked to find the name of the person having
phone number say “1234” with the help of a telephone
directory .
Since telephone directory is sorted by name not by
numbers,we have to go through each and every number
of the directory
Best case
● If the first number in the directory is the number you
were searching for ,then lucky you!!.
● Since you have found it on the very first page,now its
not important for you that how many pages are there in
the directory.
● Whether if it is of 1000 pages or 2000 pages it will take
u same time to find you the number ,if it is at the very
beginning .
● So it does not depends on no. on elements in the
directory.Hence constant time .
InBig O notation : 0(1)
Worst Case
It may happen that the number you are searching for is the last
number of directory or if it is not in the directory at all.
In that case you have to search the whole directory.
Now number of elements will matter to you.if there are 500 pages
,you have to search 500;if it has 1000 you have to search 1000.
Your search time is proportional to number of elements in the
directory. In big O notation O(n)
No of elements

No of comparisons to be done

15

15

600

600
Pseudocode
For all elements
Check if it is equal to element being searched
for.
If it is ,return its position.
else continue.
C++ code [Iterative]
void iterSearch(const double data [ ],int n,double key) //const for safety ,we want to keep array unchanged

{
for(int i=0;i<=n-1;i++)

//looping through all elements of the array

{
if(data[i]==key)
{
cout<<key<<" found at index "<<i<<" of the array"<<endl;
break;

//if element is found,come out of the loop

}
if(i==n-1)

//searched through the array,still not found

cout<<"n Element not found n";
}
}
Recursive Linear Search
bool recursiveSearch (const double data[ ],int n,double key)

{
static int i=0;

//static will prevent i being initialised to 0 every time we enter the function

if(data[i]==key)
{

cout<<key<<" found at index "<<i<<" of the array"<<endl;
return true; //this will end recursion which is desired as the element has been found

}
else

{ ++i;
if(i==n)
{
cout<<"nElemnent not foundn";
}
recursiveSearch(data,n,key);
}
}

i=0;

return false; //i=0 to reset i for next search.
Discussions
1.Sorted array is not needed.
2.Works fine for small number of elements .Search time
increases with number of elements.
3.Elements with higher probability of being searched
should be kept in the beginning.
4.Trick: Get completely rid of the end-check, namely
putting a sentinel value after the end of the last valid
array element.

More Related Content

PDF
Sorting Algorithms
PPTX
single linked list
PPTX
Linear and Binary search
PPTX
Linear Search Presentation
PPTX
Linear search-and-binary-search
PDF
linear search and binary search
PPTX
sorting and its types
PDF
sparse matrix in data structure
Sorting Algorithms
single linked list
Linear and Binary search
Linear Search Presentation
Linear search-and-binary-search
linear search and binary search
sorting and its types
sparse matrix in data structure

What's hot (20)

PPTX
Linked List
PPT
Queue data structure
PPTX
stack & queue
PDF
linked lists in data structures
PPTX
Linear Search Data Structure
PPTX
Analysis of algorithm
PPTX
Doubly Linked List
PPTX
Priority Queue in Data Structure
PPTX
Binary search
PPT
Linked list
PDF
Binary Search - Design & Analysis of Algorithms
PPTX
Data Structures - Lecture 9 [Stack & Queue using Linked List]
PPTX
Insertion sort
PDF
PPTX
Sorting Algorithms
PPT
Data Structure and Algorithms Binary Search Tree
PDF
Red black tree
PPTX
Doubly linked list (animated)
PPT
Selection sort
PPT
SEARCHING AND SORTING ALGORITHMS
Linked List
Queue data structure
stack & queue
linked lists in data structures
Linear Search Data Structure
Analysis of algorithm
Doubly Linked List
Priority Queue in Data Structure
Binary search
Linked list
Binary Search - Design & Analysis of Algorithms
Data Structures - Lecture 9 [Stack & Queue using Linked List]
Insertion sort
Sorting Algorithms
Data Structure and Algorithms Binary Search Tree
Red black tree
Doubly linked list (animated)
Selection sort
SEARCHING AND SORTING ALGORITHMS
Ad

Similar to Linear search algorithm (20)

PPTX
27631722026_T._TAJESWAR_RAO_PCC-CS301_CSE(CS).pptx
PPTX
linear search.pptxtttttttttttttttttttttt
PPTX
linear search.pptxtttttttttttttttttttttt
PPTX
linear search o power point presentation
PPTX
Linear Search
PDF
Searching
PPTX
placement preparation for Array Searching.pptx
PPTX
Sorting and searching arrays binary search algorithm
PPTX
PPT.pptx Searching and Sorting Techniques
PPTX
Searching and Sorting Algorithms in Data Structures
PPTX
SEARCHING
PPT
1 class linear and Binary search (3).ppt
PPTX
Linear Search in Oops.pptx
PPTX
arrays in c
PPTX
21CS32 DS Module 1 PPT.pptx
PPTX
Data structure chapter 2 Time complexity of known algorithms.pptx
PDF
Searching and sorting by B kirron Reddi
PPT
Chapter three data structure and algorithms qaybta quee
PPTX
Program Practical to operations on Array
DOCX
PPS 5.5.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...
27631722026_T._TAJESWAR_RAO_PCC-CS301_CSE(CS).pptx
linear search.pptxtttttttttttttttttttttt
linear search.pptxtttttttttttttttttttttt
linear search o power point presentation
Linear Search
Searching
placement preparation for Array Searching.pptx
Sorting and searching arrays binary search algorithm
PPT.pptx Searching and Sorting Techniques
Searching and Sorting Algorithms in Data Structures
SEARCHING
1 class linear and Binary search (3).ppt
Linear Search in Oops.pptx
arrays in c
21CS32 DS Module 1 PPT.pptx
Data structure chapter 2 Time complexity of known algorithms.pptx
Searching and sorting by B kirron Reddi
Chapter three data structure and algorithms qaybta quee
Program Practical to operations on Array
PPS 5.5.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...
Ad

More from NeoClassical (6)

PPTX
PPTX
PPTX
Dual nature of matter
PPTX
PPTX
Vectors and 3 d
PPTX
Alternating current
Dual nature of matter
Vectors and 3 d
Alternating current

Recently uploaded (20)

PDF
Cell Biology Basics: Cell Theory, Structure, Types, and Organelles | BS Level...
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
The Final Stretch: How to Release a Game and Not Die in the Process.
PDF
Electrolyte Disturbances and Fluid Management A clinical and physiological ap...
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Insiders guide to clinical Medicine.pdf
PPTX
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
PPTX
How to Manage Loyalty Points in Odoo 18 Sales
PPTX
UNDER FIVE CLINICS OR WELL BABY CLINICS.pptx
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
Cardiovascular Pharmacology for pharmacy students.pptx
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
How to Manage Bill Control Policy in Odoo 18
PPTX
Introduction and Scope of Bichemistry.pptx
PDF
Types of Literary Text: Poetry and Prose
PPTX
How to Manage Starshipit in Odoo 18 - Odoo Slides
PPTX
NOI Hackathon - Summer Edition - GreenThumber.pptx
PPTX
Revamp in MTO Odoo 18 Inventory - Odoo Slides
PDF
LDMMIA Reiki Yoga S2 L3 Vod Sample Preview
Cell Biology Basics: Cell Theory, Structure, Types, and Organelles | BS Level...
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
The Final Stretch: How to Release a Game and Not Die in the Process.
Electrolyte Disturbances and Fluid Management A clinical and physiological ap...
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Insiders guide to clinical Medicine.pdf
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
How to Manage Loyalty Points in Odoo 18 Sales
UNDER FIVE CLINICS OR WELL BABY CLINICS.pptx
human mycosis Human fungal infections are called human mycosis..pptx
Cardiovascular Pharmacology for pharmacy students.pptx
Abdominal Access Techniques with Prof. Dr. R K Mishra
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
How to Manage Bill Control Policy in Odoo 18
Introduction and Scope of Bichemistry.pptx
Types of Literary Text: Poetry and Prose
How to Manage Starshipit in Odoo 18 - Odoo Slides
NOI Hackathon - Summer Edition - GreenThumber.pptx
Revamp in MTO Odoo 18 Inventory - Odoo Slides
LDMMIA Reiki Yoga S2 L3 Vod Sample Preview

Linear search algorithm

  • 1. LINEAR SEARCH ALGORITHM Algorithm involves checking all the elements of the array(or any other structure) one by one and in sequence until the desired result is found.
  • 2. Daily life example If you are asked to find the name of the person having phone number say “1234” with the help of a telephone directory . Since telephone directory is sorted by name not by numbers,we have to go through each and every number of the directory
  • 3. Best case ● If the first number in the directory is the number you were searching for ,then lucky you!!. ● Since you have found it on the very first page,now its not important for you that how many pages are there in the directory. ● Whether if it is of 1000 pages or 2000 pages it will take u same time to find you the number ,if it is at the very beginning . ● So it does not depends on no. on elements in the directory.Hence constant time . InBig O notation : 0(1)
  • 4. Worst Case It may happen that the number you are searching for is the last number of directory or if it is not in the directory at all. In that case you have to search the whole directory. Now number of elements will matter to you.if there are 500 pages ,you have to search 500;if it has 1000 you have to search 1000. Your search time is proportional to number of elements in the directory. In big O notation O(n) No of elements No of comparisons to be done 15 15 600 600
  • 5. Pseudocode For all elements Check if it is equal to element being searched for. If it is ,return its position. else continue.
  • 6. C++ code [Iterative] void iterSearch(const double data [ ],int n,double key) //const for safety ,we want to keep array unchanged { for(int i=0;i<=n-1;i++) //looping through all elements of the array { if(data[i]==key) { cout<<key<<" found at index "<<i<<" of the array"<<endl; break; //if element is found,come out of the loop } if(i==n-1) //searched through the array,still not found cout<<"n Element not found n"; } }
  • 7. Recursive Linear Search bool recursiveSearch (const double data[ ],int n,double key) { static int i=0; //static will prevent i being initialised to 0 every time we enter the function if(data[i]==key) { cout<<key<<" found at index "<<i<<" of the array"<<endl; return true; //this will end recursion which is desired as the element has been found } else { ++i; if(i==n) { cout<<"nElemnent not foundn"; } recursiveSearch(data,n,key); } } i=0; return false; //i=0 to reset i for next search.
  • 8. Discussions 1.Sorted array is not needed. 2.Works fine for small number of elements .Search time increases with number of elements. 3.Elements with higher probability of being searched should be kept in the beginning. 4.Trick: Get completely rid of the end-check, namely putting a sentinel value after the end of the last valid array element.