SlideShare a Scribd company logo
1 of 17
UNIT I- LINEAR SEARCH
Presented By
Ms. R.SWATHI
ASSISTANT PROFESSOR
CSE
SRMIST RAMAPURAM
Searching Algorithms
Linear Search
Data Search − Consider an inventory of 1 million(106) items of a
store. If the application is to search an item, it has to
search an item in 1 million(106) items every time slowing
down the search. As data grows, search will become slower.
This is why searching algorithms are important
Need for Searching Algorithms
1. Linear search or sequential search is a method for finding a
particular value in a list that checks each element in sequence
until the desired element is found or the list is exhausted.
2. The list need not be ordered.
3. It is very simple and works as follows: We keep on comparing
each element with the element to search until the desired
element is found or list ends.
Linear search
This is the array we are going to perform linear search
Let’s search for the number 3. We start at the beginning
and check the first element in the array.
Example
Is the first value 3? No, not it. Is it the next
element?
Is the second value 3?
Not there either. The next element?
Is the third value 3?
Not there either. Next?
Is the fourth value 3? Yes!
We found it!!! Now you understand the idea of
linear searching; we go through each element,
in order, until we find the correct value.
Linear search - Pseudocode:
# Input: Array D, integer key
# Output: first index of key in D, or -1 if not found
For i = 0 to last index of D:
if D[i] equals key:
return i
return -1
Time Complexity
In the linear search problem, the best case occurs when Target is
present at the first location. The number of operations in the
best case is constant (not dependent on n). So time complexity in
the best case would be O(1)
Best case Analysis
Time Complexity
For Linear Search, the worst case happens when the element to be
searched is not present in the array or it is present at the end of
the array.
In this scenario the search() compares it with all the elements of
array one by one. Therefore, the worst case time complexity of
linear search would be O(n).
Worst case Analysis
Time Complexity
The key is equally likely to be in any position in the array
If the key is in the first array position: 1 comparison
If the key is in the second array position: 2 comparisons ...
If the key is in the ith postion: i comparisons ...
So average all these possibilities:
(1+2+3+...+n)/n = [n(n+1)/2] /n = (n+1)/2 comparisons.
The average number of comparisons is (n+1)/2 = O(n).
Average case Analysis
Time Complexity
In average case analysis, we take all possible inputs and calculate
computing time for all of the inputs. Sum all the calculated values and
divide the sum by total number of inputs.
We must know distribution of cases. For the linear search problem, let us
assume that all cases are uniformly distributed (including the case of
target not being present in array).
Average case Analysis
Space Complexity
This type of search requires only a single unit of memory to store the
element being searched. This is not relevant to the size of the input
Array.
Hence, the Space Complexity of Linear Search is O(1).
1. What is the best case for linear search?
a) O(nlogn)
b) O(logn)
c) O(n)
d) O(1)
2. What is the worst case for linear search?
a) O(nlogn)
b) O(logn)
c) O(n)
d) O(1)
QUIZ
1. D
2. C
3. What is the best case and worst case complexity of ordered linear
search?
a) O(nlogn), O(logn)
b) O(logn), O(nlogn)
c) O(n), O(1)
d) O(1), O(n)
4. Which of the following is a disadvantage of linear search?
a) Requires more space
b) Greater time complexities compared to other searching algorithms
c) Not easy to understand
d) Not easy to implement
3. D
4. B
5. The array is as follows: 1,2,3,6,8,10. At what time the element 6
is found? (By using linear search algorithm)
a) 4th call
b) 3rd call
c) 6th call
d) 5th call
5. A
Thank you

More Related Content

What's hot

What's hot (19)

Searching linear & binary search
Searching linear & binary searchSearching linear & binary search
Searching linear & binary search
 
Linear Search Data Structure
Linear Search Data StructureLinear Search Data Structure
Linear Search Data Structure
 
Searching algorithms
Searching algorithmsSearching algorithms
Searching algorithms
 
Binary search algorithm
Binary search algorithmBinary search algorithm
Binary search algorithm
 
Sequential & binary, linear search
Sequential & binary, linear searchSequential & binary, linear search
Sequential & binary, linear search
 
Algorithms
AlgorithmsAlgorithms
Algorithms
 
Searching
SearchingSearching
Searching
 
Searching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data StructureSearching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data Structure
 
Linear Search Presentation
Linear Search PresentationLinear Search Presentation
Linear Search Presentation
 
Searching techniques
Searching techniquesSearching techniques
Searching techniques
 
Sorting
SortingSorting
Sorting
 
Binary search
Binary searchBinary search
Binary search
 
Selection sort
Selection sortSelection sort
Selection sort
 
Cs419 lec8 top-down parsing
Cs419 lec8    top-down parsingCs419 lec8    top-down parsing
Cs419 lec8 top-down parsing
 
Boyer moore algorithm
Boyer moore algorithmBoyer moore algorithm
Boyer moore algorithm
 
LINEAR SEARCH
LINEAR SEARCHLINEAR SEARCH
LINEAR SEARCH
 
Data structure unit I part B
Data structure unit I part BData structure unit I part B
Data structure unit I part B
 
Unit 2 application of stack
Unit 2  application of stack Unit 2  application of stack
Unit 2 application of stack
 
String matching, naive,
String matching, naive,String matching, naive,
String matching, naive,
 

Similar to SEARCHING

DS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptxDS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptx
prakashvs7
 
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
 
Algorithm 8th lecture linear & binary search(2).pptx
Algorithm 8th lecture linear & binary search(2).pptxAlgorithm 8th lecture linear & binary search(2).pptx
Algorithm 8th lecture linear & binary search(2).pptx
Aftabali702240
 
Searching Sorting
Searching SortingSearching Sorting
Searching Sorting
guest2cb109
 
Selection sort
Selection sortSelection sort
Selection sort
asra khan
 

Similar to SEARCHING (20)

Linear Search
Linear SearchLinear Search
Linear Search
 
Searching & Sorting Algorithms
Searching & Sorting AlgorithmsSearching & Sorting Algorithms
Searching & Sorting Algorithms
 
Binary Search
Binary SearchBinary Search
Binary Search
 
L1803016468
L1803016468L1803016468
L1803016468
 
Lecture_Oct26.pptx
Lecture_Oct26.pptxLecture_Oct26.pptx
Lecture_Oct26.pptx
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
 
Data Structures_ Sorting & Searching
Data Structures_ Sorting & SearchingData Structures_ Sorting & Searching
Data Structures_ Sorting & Searching
 
DS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptxDS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptx
 
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
 
Algorithm 8th lecture linear & binary search(2).pptx
Algorithm 8th lecture linear & binary search(2).pptxAlgorithm 8th lecture linear & binary search(2).pptx
Algorithm 8th lecture linear & binary search(2).pptx
 
Sorting
SortingSorting
Sorting
 
my docoment
my docomentmy docoment
my docoment
 
Chapter3.pptx
Chapter3.pptxChapter3.pptx
Chapter3.pptx
 
Searching techniques
Searching techniquesSearching techniques
Searching techniques
 
Sorting
SortingSorting
Sorting
 
Binary.pptx
Binary.pptxBinary.pptx
Binary.pptx
 
Searching
Searching Searching
Searching
 
Searching Sorting
Searching SortingSearching Sorting
Searching Sorting
 
Selection sort
Selection sortSelection sort
Selection sort
 

Recently uploaded

Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
AnaAcapella
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
EADTU
 

Recently uploaded (20)

dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learning
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Our Environment Class 10 Science Notes pdf
Our Environment Class 10 Science Notes pdfOur Environment Class 10 Science Notes pdf
Our Environment Class 10 Science Notes pdf
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
Economic Importance Of Fungi In Food Additives
Economic Importance Of Fungi In Food AdditivesEconomic Importance Of Fungi In Food Additives
Economic Importance Of Fungi In Food Additives
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfUGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
 

SEARCHING

  • 1. UNIT I- LINEAR SEARCH Presented By Ms. R.SWATHI ASSISTANT PROFESSOR CSE SRMIST RAMAPURAM
  • 3. Data Search − Consider an inventory of 1 million(106) items of a store. If the application is to search an item, it has to search an item in 1 million(106) items every time slowing down the search. As data grows, search will become slower. This is why searching algorithms are important Need for Searching Algorithms
  • 4. 1. Linear search or sequential search is a method for finding a particular value in a list that checks each element in sequence until the desired element is found or the list is exhausted. 2. The list need not be ordered. 3. It is very simple and works as follows: We keep on comparing each element with the element to search until the desired element is found or list ends. Linear search
  • 5. This is the array we are going to perform linear search Let’s search for the number 3. We start at the beginning and check the first element in the array. Example
  • 6. Is the first value 3? No, not it. Is it the next element? Is the second value 3? Not there either. The next element?
  • 7. Is the third value 3? Not there either. Next? Is the fourth value 3? Yes! We found it!!! Now you understand the idea of linear searching; we go through each element, in order, until we find the correct value.
  • 8. Linear search - Pseudocode: # Input: Array D, integer key # Output: first index of key in D, or -1 if not found For i = 0 to last index of D: if D[i] equals key: return i return -1
  • 9. Time Complexity In the linear search problem, the best case occurs when Target is present at the first location. The number of operations in the best case is constant (not dependent on n). So time complexity in the best case would be O(1) Best case Analysis
  • 10. Time Complexity For Linear Search, the worst case happens when the element to be searched is not present in the array or it is present at the end of the array. In this scenario the search() compares it with all the elements of array one by one. Therefore, the worst case time complexity of linear search would be O(n). Worst case Analysis
  • 11. Time Complexity The key is equally likely to be in any position in the array If the key is in the first array position: 1 comparison If the key is in the second array position: 2 comparisons ... If the key is in the ith postion: i comparisons ... So average all these possibilities: (1+2+3+...+n)/n = [n(n+1)/2] /n = (n+1)/2 comparisons. The average number of comparisons is (n+1)/2 = O(n). Average case Analysis
  • 12. Time Complexity In average case analysis, we take all possible inputs and calculate computing time for all of the inputs. Sum all the calculated values and divide the sum by total number of inputs. We must know distribution of cases. For the linear search problem, let us assume that all cases are uniformly distributed (including the case of target not being present in array). Average case Analysis
  • 13. Space Complexity This type of search requires only a single unit of memory to store the element being searched. This is not relevant to the size of the input Array. Hence, the Space Complexity of Linear Search is O(1).
  • 14. 1. What is the best case for linear search? a) O(nlogn) b) O(logn) c) O(n) d) O(1) 2. What is the worst case for linear search? a) O(nlogn) b) O(logn) c) O(n) d) O(1) QUIZ 1. D 2. C
  • 15. 3. What is the best case and worst case complexity of ordered linear search? a) O(nlogn), O(logn) b) O(logn), O(nlogn) c) O(n), O(1) d) O(1), O(n) 4. Which of the following is a disadvantage of linear search? a) Requires more space b) Greater time complexities compared to other searching algorithms c) Not easy to understand d) Not easy to implement 3. D 4. B
  • 16. 5. The array is as follows: 1,2,3,6,8,10. At what time the element 6 is found? (By using linear search algorithm) a) 4th call b) 3rd call c) 6th call d) 5th call 5. A