SlideShare a Scribd company logo
1 of 12
LINEAR SEARCHING
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
DEPARTMENT: COMPUTER SCIENCE & ENGINEERING(CYBER SECURITY)
NAME OF THE STUDENT: T. TAJESWAR RAO
UNIVERSITY ROLL NO: 27631722026
PAPER NAME: DATA STRUCTURE & ALGORITHMS
PAPER CODE: PCC-CS301
FACULTY NAME:
INTRODUCTION
A linear search is the simplest approach employed to search for an
element in a data set. Linear Search is defined as a sequential search
algorithm that starts at one end and goes through each element of
a list until the desired element is found, otherwise the search
continues till the end of the data set. The search is finished and
terminated once the target element is located. If it finds no match,
the algorithm must terminate its execution and return an
appropriate result.
HOW LINEAR SEARCH
WORKS
In Linear Search Algorithm,
Every element is considered as a potential match for the
key and checked for the same.
If any element is found equal to the key, the search is
successful and the index of that element is returned.
If no element is found equal to the key, the search
yields “No match found”.
For example: Consider the array arr[] = {10, 50, 30, 70, 80,
20, 90, 40} and key = 30
Step 1: Start from the first element (index 0) and compare key with each
element (arr[i]).
• Comparing key with first element arr[0]. SInce not equal, the iterator
moves to the next element as a potential match.
• Comparing key with next element arr[1]. SInce not equal, the iterator
moves to the next element as a potential match.
Step 2: Now when comparing arr[2] with key, the value matches. So the
Linear Search Algorithm will yield a successful message and return the
index of the element when key is found (here 2).
ALGORITHM
 Define a function named search that takes an integer array arr[], an integer N, and an
integer x as arguments.
 Inside the search function, use a for loop to iterate over the array from index 0 to N-1.
 Inside the for loop, check if the current element of the array is equal to the value of x.
 If the current element is equal to x, return the index of the current element.
 If the loop finishes without finding the element, return -1.
 In the main function, define an integer array arr[] and initialize it with some values.
 Define an integer x and set its value to the element we want to search for.
 Define an integer N and set its value to the size of the array arr[].
 Call the search function with arr, N, and x as arguments and store the returned value in
an integer variable named result.
 Use a ternary operator to check if result is equal to -1.
 If result is equal to -1, print "Element is not present in array".
 If result is not equal to -1, print "Element is present at index result".
Source Code:
// C code to linearly search x in arr[].
#include <stdio.h>
int search(int arr[], int N, int x)
{
for (int i = 0; i < N; i++)
if (arr[i] == x)
return i;
return -1;
}
// Driver code
int main(void)
{
int arr[] = { 2, 3, 4, 10, 40 };
int x = 10;
int N = sizeof(arr) / sizeof(arr[0]);
// Function call
int result = search(arr, N, x);
(result == -1)
? printf("Element is not present in array")
: printf("Element is present at index %d", result);
return 0;
}
COMPLEXITY OF LINEAR
SEARCH
Time Complexity:
Best Case: In the best case, the key might be
present at the first index. So the best case
complexity is O(1)
Worst Case: In the worst case, the key might be
present at the last index i.e., opposite to the end
from which the search has started in the list. So
the worst-case complexity is O(N) where N is the
size of the list.
ADVANTAGES OF LINEAR
SEARCH
• Linear search can be used irrespective of whether the
array is sorted or not. It can be used on arrays of any
data type.
• Does not require any additional memory.
• It is a well-suited algorithm for small datasets.
DRAWBACKS OF LINEAR SEARCH
 Linear search has a time complexity of O(N), which in
turn makes it slow for large datasets.
 Not suitable for large arrays.
CONCLUSION
In conclusion, implementing linear search in a project can be a simple and
effective way to search for an element within an array. While it may not be the
most efficient search algorithm for large datasets, it can still be useful for
smaller arrays or when the data is unsorted. Understanding the basics of linear
search can also provide a foundation for learning more complex search
algorithms. Overall, incorporating linear search into a project can improve its
functionality and provide a valuable learning experience.
REFERENCES:
• www.geeksforgeeks.org
• www.Wikipedia.com
• www.tutorialspoint.com
• www.simplilearn.com
• www.javatpoint.com

More Related Content

Similar to 27631722026_T._TAJESWAR_RAO_PCC-CS301_CSE(CS).pptx

Linear Search
Linear SearchLinear Search
Linear SearchSWATHIR72
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdfharamaya university
 
21CS32 DS Module 1 PPT.pptx
21CS32 DS Module 1 PPT.pptx21CS32 DS Module 1 PPT.pptx
21CS32 DS Module 1 PPT.pptxreddy19841
 
Data structures arrays
Data structures   arraysData structures   arrays
Data structures arraysmaamir farooq
 
Searching Sorting
Searching SortingSearching Sorting
Searching Sortingguest2cb109
 
DS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptxDS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptxprakashvs7
 
Data Structures_ Sorting & Searching
Data Structures_ Sorting & SearchingData Structures_ Sorting & Searching
Data Structures_ Sorting & SearchingThenmozhiK5
 
PPT.pptx Searching and Sorting Techniques
PPT.pptx Searching and Sorting TechniquesPPT.pptx Searching and Sorting Techniques
PPT.pptx Searching and Sorting TechniquesVaibhav Parjane
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsDrishti Bhalla
 
unit II_2_i.pptx
unit II_2_i.pptxunit II_2_i.pptx
unit II_2_i.pptxHODElex
 
Searching techniques with progrms
Searching techniques with progrmsSearching techniques with progrms
Searching techniques with progrmsMisssaxena
 
Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0BG Java EE Course
 

Similar to 27631722026_T._TAJESWAR_RAO_PCC-CS301_CSE(CS).pptx (20)

Linear Search
Linear SearchLinear Search
Linear Search
 
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
 
21CS32 DS Module 1 PPT.pptx
21CS32 DS Module 1 PPT.pptx21CS32 DS Module 1 PPT.pptx
21CS32 DS Module 1 PPT.pptx
 
Searching
SearchingSearching
Searching
 
Data structures arrays
Data structures   arraysData structures   arrays
Data structures arrays
 
9 Arrays
9 Arrays9 Arrays
9 Arrays
 
Searching Sorting
Searching SortingSearching Sorting
Searching Sorting
 
Chapter 14
Chapter 14Chapter 14
Chapter 14
 
DS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptxDS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptx
 
Data Structures_ Sorting & Searching
Data Structures_ Sorting & SearchingData Structures_ Sorting & Searching
Data Structures_ Sorting & Searching
 
PPT.pptx Searching and Sorting Techniques
PPT.pptx Searching and Sorting TechniquesPPT.pptx Searching and Sorting Techniques
PPT.pptx Searching and Sorting Techniques
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
 
Linear search
Linear searchLinear search
Linear search
 
Chap10
Chap10Chap10
Chap10
 
unit II_2_i.pptx
unit II_2_i.pptxunit II_2_i.pptx
unit II_2_i.pptx
 
Searching techniques with progrms
Searching techniques with progrmsSearching techniques with progrms
Searching techniques with progrms
 
Lecture3b searching
Lecture3b searchingLecture3b searching
Lecture3b searching
 
Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0
 
Searching techniques
Searching techniquesSearching techniques
Searching techniques
 

Recently uploaded

General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxannathomasp01
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
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.pdfAdmir Softic
 
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.pptxDr. Ravikiran H M Gowda
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
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)Jisc
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
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.docxRamakrishna Reddy Bijjam
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
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 17Celine George
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...Amil baba
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 

Recently uploaded (20)

General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
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
 
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
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
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)
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
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
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
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
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 

27631722026_T._TAJESWAR_RAO_PCC-CS301_CSE(CS).pptx

  • 1. LINEAR SEARCHING BUDGE BUDGE INSTITUTE OF TECHNOLOGY DEPARTMENT: COMPUTER SCIENCE & ENGINEERING(CYBER SECURITY) NAME OF THE STUDENT: T. TAJESWAR RAO UNIVERSITY ROLL NO: 27631722026 PAPER NAME: DATA STRUCTURE & ALGORITHMS PAPER CODE: PCC-CS301 FACULTY NAME:
  • 2. INTRODUCTION A linear search is the simplest approach employed to search for an element in a data set. Linear Search is defined as a sequential search algorithm that starts at one end and goes through each element of a list until the desired element is found, otherwise the search continues till the end of the data set. The search is finished and terminated once the target element is located. If it finds no match, the algorithm must terminate its execution and return an appropriate result.
  • 3. HOW LINEAR SEARCH WORKS In Linear Search Algorithm, Every element is considered as a potential match for the key and checked for the same. If any element is found equal to the key, the search is successful and the index of that element is returned. If no element is found equal to the key, the search yields “No match found”. For example: Consider the array arr[] = {10, 50, 30, 70, 80, 20, 90, 40} and key = 30
  • 4. Step 1: Start from the first element (index 0) and compare key with each element (arr[i]). • Comparing key with first element arr[0]. SInce not equal, the iterator moves to the next element as a potential match. • Comparing key with next element arr[1]. SInce not equal, the iterator moves to the next element as a potential match.
  • 5. Step 2: Now when comparing arr[2] with key, the value matches. So the Linear Search Algorithm will yield a successful message and return the index of the element when key is found (here 2).
  • 6. ALGORITHM  Define a function named search that takes an integer array arr[], an integer N, and an integer x as arguments.  Inside the search function, use a for loop to iterate over the array from index 0 to N-1.  Inside the for loop, check if the current element of the array is equal to the value of x.  If the current element is equal to x, return the index of the current element.  If the loop finishes without finding the element, return -1.  In the main function, define an integer array arr[] and initialize it with some values.  Define an integer x and set its value to the element we want to search for.  Define an integer N and set its value to the size of the array arr[].  Call the search function with arr, N, and x as arguments and store the returned value in an integer variable named result.  Use a ternary operator to check if result is equal to -1.  If result is equal to -1, print "Element is not present in array".  If result is not equal to -1, print "Element is present at index result".
  • 7. Source Code: // C code to linearly search x in arr[]. #include <stdio.h> int search(int arr[], int N, int x) { for (int i = 0; i < N; i++) if (arr[i] == x) return i; return -1; } // Driver code int main(void) { int arr[] = { 2, 3, 4, 10, 40 }; int x = 10; int N = sizeof(arr) / sizeof(arr[0]); // Function call int result = search(arr, N, x); (result == -1) ? printf("Element is not present in array") : printf("Element is present at index %d", result); return 0; }
  • 8. COMPLEXITY OF LINEAR SEARCH Time Complexity: Best Case: In the best case, the key might be present at the first index. So the best case complexity is O(1) Worst Case: In the worst case, the key might be present at the last index i.e., opposite to the end from which the search has started in the list. So the worst-case complexity is O(N) where N is the size of the list.
  • 9. ADVANTAGES OF LINEAR SEARCH • Linear search can be used irrespective of whether the array is sorted or not. It can be used on arrays of any data type. • Does not require any additional memory. • It is a well-suited algorithm for small datasets.
  • 10. DRAWBACKS OF LINEAR SEARCH  Linear search has a time complexity of O(N), which in turn makes it slow for large datasets.  Not suitable for large arrays.
  • 11. CONCLUSION In conclusion, implementing linear search in a project can be a simple and effective way to search for an element within an array. While it may not be the most efficient search algorithm for large datasets, it can still be useful for smaller arrays or when the data is unsorted. Understanding the basics of linear search can also provide a foundation for learning more complex search algorithms. Overall, incorporating linear search into a project can improve its functionality and provide a valuable learning experience.
  • 12. REFERENCES: • www.geeksforgeeks.org • www.Wikipedia.com • www.tutorialspoint.com • www.simplilearn.com • www.javatpoint.com