SlideShare a Scribd company logo
1 of 16
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

Selection sort
Selection sortSelection sort
Selection sort
Jay Patel
 

What's hot (20)

Linear Search & Binary Search
Linear Search & Binary SearchLinear Search & Binary Search
Linear Search & Binary Search
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMS
 
sorting and its types
sorting and its typessorting and its types
sorting and its types
 
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...
 
Selection sort
Selection sortSelection sort
Selection sort
 
3.2 insertion sort
3.2 insertion sort3.2 insertion sort
3.2 insertion sort
 
Searching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And AlgorithmSearching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And Algorithm
 
Binary search
Binary searchBinary search
Binary search
 
Tries
TriesTries
Tries
 
Selection sort 1
Selection sort 1Selection sort 1
Selection sort 1
 
Binary search
Binary searchBinary search
Binary search
 
Searching linear & binary search
Searching linear & binary searchSearching linear & binary search
Searching linear & binary search
 
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
 
Binary search tree in data structures
Binary search tree in  data structuresBinary search tree in  data structures
Binary search tree in data structures
 
Searching algorithm
Searching algorithmSearching algorithm
Searching algorithm
 
Quick Sort , Merge Sort , Heap Sort
Quick Sort , Merge Sort ,  Heap SortQuick Sort , Merge Sort ,  Heap Sort
Quick Sort , Merge Sort , Heap Sort
 
Ppt bubble sort
Ppt bubble sortPpt bubble sort
Ppt bubble sort
 
Binary search algorithm
Binary search algorithmBinary search algorithm
Binary search algorithm
 
Stacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURESStacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURES
 
linear search and binary search
linear search and binary searchlinear search and binary search
linear search and binary search
 

Similar to Linear Search

DS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptxDS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptx
prakashvs7
 
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
 
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
 

Similar to Linear Search (20)

SEARCHING
SEARCHINGSEARCHING
SEARCHING
 
Dsa – data structure and algorithms searching
Dsa – data structure and algorithms   searchingDsa – data structure and algorithms   searching
Dsa – data structure and algorithms searching
 
Lecture_Oct26.pptx
Lecture_Oct26.pptxLecture_Oct26.pptx
Lecture_Oct26.pptx
 
Binary Search
Binary SearchBinary Search
Binary Search
 
Searching & Sorting Algorithms
Searching & Sorting AlgorithmsSearching & Sorting Algorithms
Searching & Sorting Algorithms
 
L1803016468
L1803016468L1803016468
L1803016468
 
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
 
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
 
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
 
Chapter3.pptx
Chapter3.pptxChapter3.pptx
Chapter3.pptx
 
Sorting
SortingSorting
Sorting
 
my docoment
my docomentmy docoment
my docoment
 
Searching
Searching Searching
Searching
 
Sorting
SortingSorting
Sorting
 
Searching techniques
Searching techniquesSearching techniques
Searching techniques
 
Binary.pptx
Binary.pptxBinary.pptx
Binary.pptx
 
Algorithms - Aaron Bloomfield
Algorithms - Aaron BloomfieldAlgorithms - Aaron Bloomfield
Algorithms - Aaron Bloomfield
 

Recently uploaded

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Call Girls in Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in  Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in  Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Recently uploaded (20)

How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
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Ă...
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111
 
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
 
Call Girls in Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in  Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in  Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Basic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationBasic Intentional Injuries Health Education
Basic Intentional Injuries Health Education
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
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
 
Philosophy of china and it's charactistics
Philosophy of china and it's charactisticsPhilosophy of china and it's charactistics
Philosophy of china and it's charactistics
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
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
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 

Linear Search

  • 2. 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
  • 3. 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
  • 4. 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
  • 5. Is the first value 3? No, not it. Is it the next element? Is the second value 3? Not there either. The next element?
  • 6. 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.
  • 7. 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
  • 8. 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
  • 9. 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
  • 10. 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
  • 11. 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
  • 12. 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).
  • 13. 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
  • 14. 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
  • 15. 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