SlideShare ist ein Scribd-Unternehmen logo
1 von 8
Downloaden Sie, um offline zu lesen
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.

Weitere ähnliche Inhalte

Was ist angesagt?

Selection sort
Selection sortSelection sort
Selection sort
Jay Patel
 
Introduction to stack
Introduction to stackIntroduction to stack
Introduction to stack
vaibhav2910
 

Was ist angesagt? (20)

Binary search algorithm
Binary search algorithmBinary search algorithm
Binary search algorithm
 
Linear Search
Linear SearchLinear Search
Linear Search
 
Selection sort
Selection sortSelection sort
Selection sort
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursion
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
 
Dsa – data structure and algorithms searching
Dsa – data structure and algorithms   searchingDsa – data structure and algorithms   searching
Dsa – data structure and algorithms searching
 
Selection sort
Selection sortSelection sort
Selection sort
 
B and B+ tree
B and B+ treeB and B+ tree
B and B+ tree
 
Hashing in datastructure
Hashing in datastructureHashing in datastructure
Hashing in datastructure
 
Algorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching AlgorithmsAlgorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching Algorithms
 
Multi ways trees
Multi ways treesMulti ways trees
Multi ways trees
 
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data Structures
 
sparse matrix in data structure
sparse matrix in data structuresparse matrix in data structure
sparse matrix in data structure
 
Introduction to stack
Introduction to stackIntroduction to stack
Introduction to stack
 
Python set
Python setPython set
Python set
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeData Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search Tree
 
Stack
StackStack
Stack
 
linked list in Data Structure, Simple and Easy Tutorial
linked list in Data Structure, Simple and Easy Tutoriallinked list in Data Structure, Simple and Easy Tutorial
linked list in Data Structure, Simple and Easy Tutorial
 
sorting and its types
sorting and its typessorting and its types
sorting and its types
 

Ähnlich wie Linear search algorithm

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
 
21CS32 DS Module 1 PPT.pptx
21CS32 DS Module 1 PPT.pptx21CS32 DS Module 1 PPT.pptx
21CS32 DS Module 1 PPT.pptx
reddy19841
 
Algorithm & data structures lec4&5
Algorithm & data structures lec4&5Algorithm & data structures lec4&5
Algorithm & data structures lec4&5
Abdul Khan
 
Please Please Please Read the instructions and do everything li.docx
Please Please Please Read the instructions and do everything li.docxPlease Please Please Read the instructions and do everything li.docx
Please Please Please Read the instructions and do everything li.docx
tienmixon
 

Ähnlich wie 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
 
Searching
SearchingSearching
Searching
 
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
 
PPT.pptx Searching and Sorting Techniques
PPT.pptx Searching and Sorting TechniquesPPT.pptx Searching and Sorting Techniques
PPT.pptx Searching and Sorting Techniques
 
SEARCHING
SEARCHINGSEARCHING
SEARCHING
 
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
 
Linear Search in Oops.pptx
Linear Search in Oops.pptxLinear Search in Oops.pptx
Linear Search in Oops.pptx
 
arrays in c
arrays in carrays in c
arrays in c
 
21CS32 DS Module 1 PPT.pptx
21CS32 DS Module 1 PPT.pptx21CS32 DS Module 1 PPT.pptx
21CS32 DS Module 1 PPT.pptx
 
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
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
 
Searching in c language
Searching in c languageSearching in c language
Searching in c language
 
Binary search
Binary searchBinary search
Binary search
 
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
 
4.1 sequentioal search
4.1 sequentioal search4.1 sequentioal search
4.1 sequentioal search
 
Algorithm & data structures lec4&5
Algorithm & data structures lec4&5Algorithm & data structures lec4&5
Algorithm & data structures lec4&5
 
Please Please Please Read the instructions and do everything li.docx
Please Please Please Read the instructions and do everything li.docxPlease Please Please Read the instructions and do everything li.docx
Please Please Please Read the instructions and do everything li.docx
 
Algorithm and Programming (Searching)
Algorithm and Programming (Searching)Algorithm and Programming (Searching)
Algorithm and Programming (Searching)
 
Data structures and algorithms arrays
Data structures and algorithms   arraysData structures and algorithms   arrays
Data structures and algorithms arrays
 
Rahat &amp; juhith
Rahat &amp; juhithRahat &amp; juhith
Rahat &amp; juhith
 

Mehr von NeoClassical (6)

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

Kürzlich hochgeladen

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
MateoGardella
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 

Kürzlich hochgeladen (20)

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 

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.