SlideShare a Scribd company logo
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.
Ad

More Related Content

What's hot (20)

Binary search
Binary searchBinary search
Binary search
AparnaKumari31
 
Quick Sort
Quick SortQuick Sort
Quick Sort
Shweta Sahu
 
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
Umesh Kumar
 
Searching & Sorting Algorithms
Searching & Sorting AlgorithmsSearching & Sorting Algorithms
Searching & Sorting Algorithms
Rahul Jamwal
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
Gurukul Kangri Vishwavidyalaya - Faculty of Engineering and Technology
 
Arrays
ArraysArrays
Arrays
SARITHA REDDY
 
Binary search tree operations
Binary search tree operationsBinary search tree operations
Binary search tree operations
Kamran Zafar
 
linked lists in data structures
linked lists in data structureslinked lists in data structures
linked lists in data structures
DurgaDeviCbit
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
sagar yadav
 
Polynomial reppresentation using Linkedlist-Application of LL.pptx
Polynomial reppresentation using Linkedlist-Application of LL.pptxPolynomial reppresentation using Linkedlist-Application of LL.pptx
Polynomial reppresentation using Linkedlist-Application of LL.pptx
Albin562191
 
Linear search in ds
Linear search in dsLinear search in ds
Linear search in ds
chauhankapil
 
linear search and binary search
linear search and binary searchlinear search and binary search
linear search and binary search
Zia Ush Shamszaman
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMS
Gokul Hari
 
Searching
SearchingSearching
Searching
Ashim Lamichhane
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
Apurbo Datta
 
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data Structures
SHAKOOR AB
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked List
ManishPrajapati78
 
Tree - Data Structure
Tree - Data StructureTree - Data Structure
Tree - Data Structure
Ashim Lamichhane
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
Drishti Bhalla
 
Binary search
Binary searchBinary search
Binary search
Gaurav Solanki
 
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
Umesh Kumar
 
Searching & Sorting Algorithms
Searching & Sorting AlgorithmsSearching & Sorting Algorithms
Searching & Sorting Algorithms
Rahul Jamwal
 
Binary search tree operations
Binary search tree operationsBinary search tree operations
Binary search tree operations
Kamran Zafar
 
linked lists in data structures
linked lists in data structureslinked lists in data structures
linked lists in data structures
DurgaDeviCbit
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
sagar yadav
 
Polynomial reppresentation using Linkedlist-Application of LL.pptx
Polynomial reppresentation using Linkedlist-Application of LL.pptxPolynomial reppresentation using Linkedlist-Application of LL.pptx
Polynomial reppresentation using Linkedlist-Application of LL.pptx
Albin562191
 
Linear search in ds
Linear search in dsLinear search in ds
Linear search in ds
chauhankapil
 
linear search and binary search
linear search and binary searchlinear search and binary search
linear search and binary search
Zia Ush Shamszaman
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMS
Gokul Hari
 
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data Structures
SHAKOOR AB
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked List
ManishPrajapati78
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
Drishti Bhalla
 

Similar to Linear search algorithm (20)

27631722026_T._TAJESWAR_RAO_PCC-CS301_CSE(CS).pptx
27631722026_T._TAJESWAR_RAO_PCC-CS301_CSE(CS).pptx27631722026_T._TAJESWAR_RAO_PCC-CS301_CSE(CS).pptx
27631722026_T._TAJESWAR_RAO_PCC-CS301_CSE(CS).pptx
AyanMandal44
 
Searching
SearchingSearching
Searching
A. S. M. Shafi
 
placement preparation for Array Searching.pptx
placement preparation for Array Searching.pptxplacement preparation for Array Searching.pptx
placement preparation for Array Searching.pptx
upasnatiwari10
 
Sorting and searching arrays binary search algorithm
Sorting and searching arrays binary search algorithmSorting and searching arrays binary search algorithm
Sorting and searching arrays binary search algorithm
David Burks-Courses
 
PPT.pptx Searching and Sorting Techniques
PPT.pptx Searching and Sorting TechniquesPPT.pptx Searching and Sorting Techniques
PPT.pptx Searching and Sorting Techniques
Vaibhav Parjane
 
Searching and Sorting Algorithms in Data Structures
Searching and Sorting Algorithms  in Data StructuresSearching and Sorting Algorithms  in Data Structures
Searching and Sorting Algorithms in Data Structures
poongothai11
 
SEARCHING
SEARCHINGSEARCHING
SEARCHING
SWATHIR72
 
1 class linear and Binary search (3).ppt
1 class linear and Binary search (3).ppt1 class linear and Binary search (3).ppt
1 class linear and Binary search (3).ppt
KanchanRaut13
 
Linear Search in Oops.pptx
Linear Search in Oops.pptxLinear Search in Oops.pptx
Linear Search in Oops.pptx
Ashish Sadavarti
 
arrays in c
arrays in carrays in c
arrays in c
vidhi mehta
 
21CS32 DS Module 1 PPT.pptx
21CS32 DS Module 1 PPT.pptx21CS32 DS Module 1 PPT.pptx
21CS32 DS Module 1 PPT.pptx
reddy19841
 
Data structure chapter 2 Time complexity of known algorithms.pptx
Data structure chapter 2 Time complexity of known algorithms.pptxData structure chapter 2 Time complexity of known algorithms.pptx
Data structure chapter 2 Time complexity of known algorithms.pptx
fikadumeuedu
 
Searching and sorting by B kirron Reddi
Searching and sorting by B kirron ReddiSearching and sorting by B kirron Reddi
Searching and sorting by B kirron Reddi
B.Kirron Reddi
 
Chapter three data structure and algorithms qaybta quee
Chapter three data structure and algorithms qaybta queeChapter three data structure and algorithms qaybta quee
Chapter three data structure and algorithms qaybta quee
habdi203062
 
Program Practical to operations on Array
Program Practical to operations on ArrayProgram Practical to operations on Array
Program Practical to operations on Array
bhargavidalal5
 
PPS 5.5.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...
PPS 5.5.BASIC ALGORITHMS  SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...PPS 5.5.BASIC ALGORITHMS  SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...
PPS 5.5.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...
Sitamarhi Institute of Technology
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
haramaya university
 
Searching in c language
Searching in c languageSearching in c language
Searching in c language
CHANDAN KUMAR
 
Chapter 4: basic search algorithms data structure
Chapter 4: basic search algorithms data structureChapter 4: basic search algorithms data structure
Chapter 4: basic search algorithms data structure
Mahmoud Alfarra
 
4.1 sequentioal search
4.1 sequentioal search4.1 sequentioal search
4.1 sequentioal search
Krish_ver2
 
27631722026_T._TAJESWAR_RAO_PCC-CS301_CSE(CS).pptx
27631722026_T._TAJESWAR_RAO_PCC-CS301_CSE(CS).pptx27631722026_T._TAJESWAR_RAO_PCC-CS301_CSE(CS).pptx
27631722026_T._TAJESWAR_RAO_PCC-CS301_CSE(CS).pptx
AyanMandal44
 
placement preparation for Array Searching.pptx
placement preparation for Array Searching.pptxplacement preparation for Array Searching.pptx
placement preparation for Array Searching.pptx
upasnatiwari10
 
Sorting and searching arrays binary search algorithm
Sorting and searching arrays binary search algorithmSorting and searching arrays binary search algorithm
Sorting and searching arrays binary search algorithm
David Burks-Courses
 
PPT.pptx Searching and Sorting Techniques
PPT.pptx Searching and Sorting TechniquesPPT.pptx Searching and Sorting Techniques
PPT.pptx Searching and Sorting Techniques
Vaibhav Parjane
 
Searching and Sorting Algorithms in Data Structures
Searching and Sorting Algorithms  in Data StructuresSearching and Sorting Algorithms  in Data Structures
Searching and Sorting Algorithms in Data Structures
poongothai11
 
1 class linear and Binary search (3).ppt
1 class linear and Binary search (3).ppt1 class linear and Binary search (3).ppt
1 class linear and Binary search (3).ppt
KanchanRaut13
 
Linear Search in Oops.pptx
Linear Search in Oops.pptxLinear Search in Oops.pptx
Linear Search in Oops.pptx
Ashish Sadavarti
 
21CS32 DS Module 1 PPT.pptx
21CS32 DS Module 1 PPT.pptx21CS32 DS Module 1 PPT.pptx
21CS32 DS Module 1 PPT.pptx
reddy19841
 
Data structure chapter 2 Time complexity of known algorithms.pptx
Data structure chapter 2 Time complexity of known algorithms.pptxData structure chapter 2 Time complexity of known algorithms.pptx
Data structure chapter 2 Time complexity of known algorithms.pptx
fikadumeuedu
 
Searching and sorting by B kirron Reddi
Searching and sorting by B kirron ReddiSearching and sorting by B kirron Reddi
Searching and sorting by B kirron Reddi
B.Kirron Reddi
 
Chapter three data structure and algorithms qaybta quee
Chapter three data structure and algorithms qaybta queeChapter three data structure and algorithms qaybta quee
Chapter three data structure and algorithms qaybta quee
habdi203062
 
Program Practical to operations on Array
Program Practical to operations on ArrayProgram Practical to operations on Array
Program Practical to operations on Array
bhargavidalal5
 
PPS 5.5.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...
PPS 5.5.BASIC ALGORITHMS  SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...PPS 5.5.BASIC ALGORITHMS  SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...
PPS 5.5.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...
Sitamarhi Institute of Technology
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
haramaya university
 
Searching in c language
Searching in c languageSearching in c language
Searching in c language
CHANDAN KUMAR
 
Chapter 4: basic search algorithms data structure
Chapter 4: basic search algorithms data structureChapter 4: basic search algorithms data structure
Chapter 4: basic search algorithms data structure
Mahmoud Alfarra
 
4.1 sequentioal search
4.1 sequentioal search4.1 sequentioal search
4.1 sequentioal search
Krish_ver2
 
Ad

More from NeoClassical (6)

Nuclei
NucleiNuclei
Nuclei
NeoClassical
 
Atoms
AtomsAtoms
Atoms
NeoClassical
 
Dual nature of matter
Dual nature of matterDual nature of matter
Dual nature of matter
NeoClassical
 
Circle
CircleCircle
Circle
NeoClassical
 
Vectors and 3 d
Vectors and 3 dVectors and 3 d
Vectors and 3 d
NeoClassical
 
Alternating current
Alternating currentAlternating current
Alternating current
NeoClassical
 
Ad

Recently uploaded (20)

How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
Celine George
 
Final Evaluation.docx...........................
Final Evaluation.docx...........................Final Evaluation.docx...........................
Final Evaluation.docx...........................
l1bbyburrell
 
spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)
Mohamed Rizk Khodair
 
Origin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theoriesOrigin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theories
PrachiSontakke5
 
UPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guideUPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guide
abmerca
 
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptxANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
Mayuri Chavan
 
Cultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptxCultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptx
UmeshTimilsina1
 
Overview Well-Being and Creative Careers
Overview Well-Being and Creative CareersOverview Well-Being and Creative Careers
Overview Well-Being and Creative Careers
University of Amsterdam
 
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living WorkshopLDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDM Mia eStudios
 
antiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidenceantiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidence
PrachiSontakke5
 
Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)
Mohamed Rizk Khodair
 
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon DolabaniHistory Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
fruinkamel7m
 
All About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdfAll About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdf
TechSoup
 
The role of wall art in interior designing
The role of wall art in interior designingThe role of wall art in interior designing
The role of wall art in interior designing
meghaark2110
 
Module 1: Foundations of Research
Module 1: Foundations of ResearchModule 1: Foundations of Research
Module 1: Foundations of Research
drroxannekemp
 
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Association for Project Management
 
Cultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptxCultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptx
UmeshTimilsina1
 
*"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"**"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"*
Arshad Shaikh
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
Nguyen Thanh Tu Collection
 
Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...
parmarjuli1412
 
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
Celine George
 
Final Evaluation.docx...........................
Final Evaluation.docx...........................Final Evaluation.docx...........................
Final Evaluation.docx...........................
l1bbyburrell
 
spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)
Mohamed Rizk Khodair
 
Origin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theoriesOrigin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theories
PrachiSontakke5
 
UPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guideUPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guide
abmerca
 
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptxANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
Mayuri Chavan
 
Cultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptxCultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptx
UmeshTimilsina1
 
Overview Well-Being and Creative Careers
Overview Well-Being and Creative CareersOverview Well-Being and Creative Careers
Overview Well-Being and Creative Careers
University of Amsterdam
 
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living WorkshopLDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDM Mia eStudios
 
antiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidenceantiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidence
PrachiSontakke5
 
Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)
Mohamed Rizk Khodair
 
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon DolabaniHistory Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
fruinkamel7m
 
All About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdfAll About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdf
TechSoup
 
The role of wall art in interior designing
The role of wall art in interior designingThe role of wall art in interior designing
The role of wall art in interior designing
meghaark2110
 
Module 1: Foundations of Research
Module 1: Foundations of ResearchModule 1: Foundations of Research
Module 1: Foundations of Research
drroxannekemp
 
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Association for Project Management
 
Cultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptxCultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptx
UmeshTimilsina1
 
*"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"**"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"*
Arshad Shaikh
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
Nguyen Thanh Tu Collection
 
Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...
parmarjuli1412
 

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.
  翻译: