SlideShare a Scribd company logo
Linear Search Algorithm
Searching is the process of finding some particular element in
the list. If the element is present in the list, then the process is
called successful, and the process returns the location of that
element; otherwise, the search is called unsuccessful.
Two popular search methods are Linear Search and Binary
Search.
Linear search is also called as sequential search
algorithm. It is the simplest searching algorithm. In Linear
search, we simply traverse the list completely and match each
element of the list with the item whose location is to be found.
If the match is found, then the location of the item is returned;
otherwise, the algorithm returns NULL.
It is widely used to search an element from the unordered
list, i.e., the list in which items are not sorted.
The steps used in the implementation of Linear Search
are listed as follows -
■ First, we have to traverse the array elements using a
for loop.
■ In each iteration of for loop, compare the search
element with the current array element, and –
■ If the element matches, then return the index of the
corresponding array element.
■ If the element does not match, then move to the next
element.
■ If there is no match or the search element is not
present in the given array, return -1.
Algorithm
Linear_Search(a, n, val) // 'a' is the given array, 'n' is the
size of given array, 'val' is the value to search
Step 1: set pos = -1
Step 2: set i = 1
Step 3: repeat step 4 while i <= n
Step 4: if a[i] == val
set pos = i
print pos
go to step 6
[end of if]
set ii = i + 1
[end of loop]
Step 5: if pos = -1
print "value is not present in the array "
[end of if]
Step 6: exit
Working of Linear search
To understand the working of linear search algorithm,
let's take an unsorted array. It will be easy to understand
the working of linear search with an example.
Let the elements of array are -
Let the element to be searched is K = 41
Now, start from the first element and compare K with
each element of the array.
The value of K, i.e., 41, is not matched with the first
element of the array. So, move to the next element. And
follow the same process until the respective element is
found.
Now, the element to be searched is found. So algorithm will
return the index of the element matched.
Linear Search complexity
Now, let's see the time complexity of linear search in the
best case, average case, and worst case. We will also see
the space complexity of linear search.
1. Time Complexity
Case Time Complexity
Best Case O(1)
Average Case O(n)
Worst Case O(n)
■ Best Case Complexity - In Linear search, best case
occurs when the element we are finding is at the first
position of the array. The best-case time complexity of
linear search is O(1).
■ Average Case Complexity - The average case time
complexity of linear search is O(n).
■ Worst Case Complexity - In Linear search, the worst
case occurs when the element we are looking is present at
the end of the array. The worst-case in linear search could
be when the target element is not present in the given
array, and we have to traverse the entire array. The worst-
case time complexity of linear search is O(n).
The time complexity of linear search is O(n) because every
element in the array is compared only once.
2. Space Complexity
Space Complexity O(1)
The space complexity of linear search is O(1).
Implementation of Linear Search in C
#include <stdio.h>
int linearSearch(int a[], int n, int val)
{
for (int i = 0; i < n; i++)
{
if (a[i] == val)
return i+1;
}
return -1;
}
int main() {
int a[] = {70, 40, 30, 11, 57, 41, 25, 14, 52};
int val = 41;
int n = sizeof(a) / sizeof(a[0]);
int res = linearSearch(a, n, val);
printf("The elements of the array are - ");
for (int i = 0; i < n; i++)
printf("%d ", a[i]);
printf("nElement to be searched is - %d", val);
if (res == -1)
printf("nElement is not present in the array");
else
printf("nElement is present at %d position of array", res);
return 0;
}
Linear Search Algorithm in Data Structure and Algorithm
class LinearSearch {
static int linearSearch(int a[], int n, int val) {
for (int i = 0; i < n; i++)
{
if (a[i] == val)
return i+1;
}
return -1;
}
public static void main(String args[]) {
int a[] = {55, 29, 10, 40, 57, 41, 20, 24, 45};
int val = 10; // value to be searched
int n = a.length;
int res = linearSearch(a, n, val);
System.out.println();
System.out.print("The elements of the array are - ");
for (int i = 0; i < n; i++)
System.out.print(" " + a[i]);
System.out.println();
System.out.println("Element to be searched is - " + val);
if (res == -1)
System.out.println("Element is not present in the array");
else
System.out.println("Element is present at " + res +" position of array");
}
}
Implementation of Linear Search in Java
Linear Search Algorithm in Data Structure and Algorithm

More Related Content

PPTX
Linear and Binary search .pptx
PPTX
27631722026_T._TAJESWAR_RAO_PCC-CS301_CSE(CS).pptx
PPTX
placement preparation for Array Searching.pptx
PPT
Searching in c language
PPTX
Dsa – data structure and algorithms searching
PPTX
Linear and binary search
PPTX
linear search o power point presentation
Linear and Binary search .pptx
27631722026_T._TAJESWAR_RAO_PCC-CS301_CSE(CS).pptx
placement preparation for Array Searching.pptx
Searching in c language
Dsa – data structure and algorithms searching
Linear and binary search
linear search o power point presentation

Similar to Linear Search Algorithm in Data Structure and Algorithm (20)

PPTX
linear search.pptxtttttttttttttttttttttt
PPTX
linear search.pptxtttttttttttttttttttttt
PDF
searching
PPTX
Linear Search
PDF
Searching Algorithm, Linear search algorithm and their time complexity
PDF
programming in C
PPTX
DATA STRUCTURE linear search ALGORITHM-PROGRAM
PPTX
Searching and Sorting Algorithms in Data Structures
PPTX
unit II_2_i.pptx
PPTX
SEARCHING
PPTX
dsc complexity about the error handeling in c plus plus.pptx
DOCX
PPS 5.5.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...
PDF
Searching
PPTX
Presentation on Searching and Sorting in C language.pptx
PPTX
linear-search-and-binary-search-170418023907.pptx
PPTX
Linear Search for design and analysis of algorithm
PPTX
Lecture_Oct26.pptx
PPTX
LINEAR SEARCH-WPS Office. .pptx
PPTX
Data Structures Algorithms - Week 3c - Basic Searching Algorithms.pptx
PPTX
Searching & Algorithms IN DATA STRUCTURES
linear search.pptxtttttttttttttttttttttt
linear search.pptxtttttttttttttttttttttt
searching
Linear Search
Searching Algorithm, Linear search algorithm and their time complexity
programming in C
DATA STRUCTURE linear search ALGORITHM-PROGRAM
Searching and Sorting Algorithms in Data Structures
unit II_2_i.pptx
SEARCHING
dsc complexity about the error handeling in c plus plus.pptx
PPS 5.5.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...
Searching
Presentation on Searching and Sorting in C language.pptx
linear-search-and-binary-search-170418023907.pptx
Linear Search for design and analysis of algorithm
Lecture_Oct26.pptx
LINEAR SEARCH-WPS Office. .pptx
Data Structures Algorithms - Week 3c - Basic Searching Algorithms.pptx
Searching & Algorithms IN DATA STRUCTURES
Ad

Recently uploaded (20)

PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
additive manufacturing of ss316l using mig welding
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PDF
Well-logging-methods_new................
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
composite construction of structures.pdf
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
PPT on Performance Review to get promotions
PPTX
Construction Project Organization Group 2.pptx
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPTX
OOP with Java - Java Introduction (Basics)
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
Lecture Notes Electrical Wiring System Components
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
Digital Logic Computer Design lecture notes
Embodied AI: Ushering in the Next Era of Intelligent Systems
Internet of Things (IOT) - A guide to understanding
Model Code of Practice - Construction Work - 21102022 .pdf
additive manufacturing of ss316l using mig welding
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Well-logging-methods_new................
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
CH1 Production IntroductoryConcepts.pptx
composite construction of structures.pdf
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Operating System & Kernel Study Guide-1 - converted.pdf
PPT on Performance Review to get promotions
Construction Project Organization Group 2.pptx
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
OOP with Java - Java Introduction (Basics)
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Lecture Notes Electrical Wiring System Components
Foundation to blockchain - A guide to Blockchain Tech
Digital Logic Computer Design lecture notes
Ad

Linear Search Algorithm in Data Structure and Algorithm

  • 1. Linear Search Algorithm Searching is the process of finding some particular element in the list. If the element is present in the list, then the process is called successful, and the process returns the location of that element; otherwise, the search is called unsuccessful. Two popular search methods are Linear Search and Binary Search. Linear search is also called as sequential search algorithm. It is the simplest searching algorithm. In Linear search, we simply traverse the list completely and match each element of the list with the item whose location is to be found. If the match is found, then the location of the item is returned; otherwise, the algorithm returns NULL. It is widely used to search an element from the unordered list, i.e., the list in which items are not sorted.
  • 2. The steps used in the implementation of Linear Search are listed as follows - ■ First, we have to traverse the array elements using a for loop. ■ In each iteration of for loop, compare the search element with the current array element, and – ■ If the element matches, then return the index of the corresponding array element. ■ If the element does not match, then move to the next element. ■ If there is no match or the search element is not present in the given array, return -1.
  • 3. Algorithm Linear_Search(a, n, val) // 'a' is the given array, 'n' is the size of given array, 'val' is the value to search Step 1: set pos = -1 Step 2: set i = 1 Step 3: repeat step 4 while i <= n Step 4: if a[i] == val set pos = i print pos go to step 6 [end of if] set ii = i + 1 [end of loop] Step 5: if pos = -1 print "value is not present in the array " [end of if] Step 6: exit
  • 4. Working of Linear search To understand the working of linear search algorithm, let's take an unsorted array. It will be easy to understand the working of linear search with an example. Let the elements of array are -
  • 5. Let the element to be searched is K = 41 Now, start from the first element and compare K with each element of the array. The value of K, i.e., 41, is not matched with the first element of the array. So, move to the next element. And follow the same process until the respective element is found.
  • 6. Now, the element to be searched is found. So algorithm will return the index of the element matched.
  • 7. Linear Search complexity Now, let's see the time complexity of linear search in the best case, average case, and worst case. We will also see the space complexity of linear search. 1. Time Complexity Case Time Complexity Best Case O(1) Average Case O(n) Worst Case O(n)
  • 8. ■ Best Case Complexity - In Linear search, best case occurs when the element we are finding is at the first position of the array. The best-case time complexity of linear search is O(1). ■ Average Case Complexity - The average case time complexity of linear search is O(n). ■ Worst Case Complexity - In Linear search, the worst case occurs when the element we are looking is present at the end of the array. The worst-case in linear search could be when the target element is not present in the given array, and we have to traverse the entire array. The worst- case time complexity of linear search is O(n). The time complexity of linear search is O(n) because every element in the array is compared only once.
  • 9. 2. Space Complexity Space Complexity O(1) The space complexity of linear search is O(1).
  • 10. Implementation of Linear Search in C #include <stdio.h> int linearSearch(int a[], int n, int val) { for (int i = 0; i < n; i++) { if (a[i] == val) return i+1; } return -1; } int main() { int a[] = {70, 40, 30, 11, 57, 41, 25, 14, 52}; int val = 41; int n = sizeof(a) / sizeof(a[0]); int res = linearSearch(a, n, val); printf("The elements of the array are - "); for (int i = 0; i < n; i++) printf("%d ", a[i]); printf("nElement to be searched is - %d", val); if (res == -1) printf("nElement is not present in the array"); else printf("nElement is present at %d position of array", res); return 0; }
  • 12. class LinearSearch { static int linearSearch(int a[], int n, int val) { for (int i = 0; i < n; i++) { if (a[i] == val) return i+1; } return -1; } public static void main(String args[]) { int a[] = {55, 29, 10, 40, 57, 41, 20, 24, 45}; int val = 10; // value to be searched int n = a.length; int res = linearSearch(a, n, val); System.out.println(); System.out.print("The elements of the array are - "); for (int i = 0; i < n; i++) System.out.print(" " + a[i]); System.out.println(); System.out.println("Element to be searched is - " + val); if (res == -1) System.out.println("Element is not present in the array"); else System.out.println("Element is present at " + res +" position of array"); } } Implementation of Linear Search in Java