SlideShare a Scribd company logo
By: Er. Anupam Sharma
Assistant Professor in CSE
Searching
Linear search
Linear search is the simplest searching algorithm
which is sometimes known as sequential search.
In this algorithm each element of array is
compared with the targeted element sequentially.
Worst Case Time Complexity -O(N)
Best Case Time Complexity: O(1)
Linear Search Algorithm
Linear Search ( Array A, Value x)
Step 1: Set i to 1
Step 2: if i > n then go to step 7
Step 3: if A[i] = x then go to step 6
Step 4: Set i to i + 1
Step 5: Go to Step 2
Step 6: Print Element x Found at index i and go to step 8
Step 7: Print element not found
Step 8: Exit
Pseudocode for Linear Search
procedure linear_search (list, value)
for each item in the list
if match item == value
return the item's location
end if
end for
end procedure
Linear Search Example
Program for Linear Search in C
#include<stdio.h>
int main()
{
int a[20],i,x,n;
printf("How many elements?");
scanf("%d",&n);
printf("Enter array elements:n");
for(i=0;i<n;++i)
scanf("%d",&a[i]);
printf("nEnter element to search:");
scanf("%d",&x);
for(i=0;i<n;++i)
if(a[i]==x)
break;
if(i<n)
printf("Element found at index %d",i);
else
printf("Element not found");
return 0;
}
Output
How many elements?4
Enter array elements:
6 8 9 1
Enter element to
search:9
Element found at index
2
Binary Search
Binary search algorithm can be applied on a sorted array
to search an element. Search begins with comparing
middle element of array to target element. If both are
equal then position of element is returned. If target
element is less than middle element of array then upper
half of array is discarded and again search continued by
dividing the lower half. If target element is greater than
middle element then lower half is discarded and search is
continued in upper half.
 Worst Case Time Complexity: O(log n)
 Best Case Time Complexity: O(1)
Binary Search Algorithm
 Following are the steps of implementation that we will
be following:
 Start with the middle element:
 If the target value is equal to the middle element of the
array, then return the index of the middle element.
 If not, then compare the middle element with the target
value,
 If the target value is greater than the number in the middle
index, then pick the elements to the right of the middle index,
and start with Step 1.
 If the target value is less than the number in the middle index,
then pick the elements to the left of the middle index, and start
with Step 1.
 When a match is found, return the index of the
element matched.
Pseudo code for Binary Search
Procedure binary_search
A ← sorted array
n ← size of array
x ← value to be searched
Set lowerBound = 1
Set upperBound = n
while x not found
if upperBound < lowerBound
EXIT: x does not exists.
set midPoint = lowerBound + ( upperBound - lowerBound ) / 2
if A[midPoint] < x set lowerBound = midPoint + 1
if A[midPoint] > x set upperBound = midPoint - 1
if A[midPoint] = x EXIT: x found at location midPoint
end while
end procedure
Example
#include <stdio.h>
int main()
{ int c, first, last, middle, n, search, array[100];
printf("Enter number of elementsn");
scanf("%d",&n);
printf("Enter %d integersn", n);
for (c = 0; c < n; c++)
scanf("%d",&array[c]);
printf("Enter value to findn");
scanf("%d", &search);
first = 0; last = n - 1;
middle = (first+last)/2;
while (first <= last) {
if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search) {
printf("%d found at location %d.n", search, middle+1);
break; }
else
last = middle - 1;
middle = (first + last)/2; }
if (first > last)
printf("Not found! %d isn't present in the list.n", search);
return 0; }
Program for Binary Search in
C
Difference between both
Algorithms
Searching

More Related Content

What's hot

Linear search algorithm
Linear search algorithmLinear search algorithm
Linear search algorithm
NeoClassical
 
Sequential & binary, linear search
Sequential & binary, linear searchSequential & binary, linear search
Sequential & binary, linear search
montazur420
 
Searching algorithms
Searching algorithmsSearching algorithms
Searching algorithms
Trupti Agrawal
 
Algorithm & data structures lec4&5
Algorithm & data structures lec4&5Algorithm & data structures lec4&5
Algorithm & data structures lec4&5Abdul Khan
 
Linear and Bianry search
Linear and Bianry searchLinear and Bianry search
Linear and Bianry search
Daffodil International University
 
Binary search algorithm
Binary search algorithmBinary search algorithm
Binary search algorithm
maamir farooq
 
Linear Search Data Structure
Linear Search Data StructureLinear Search Data Structure
Linear Search Data Structure
Talha Shaikh
 
linear search and binary search
linear search and binary searchlinear search and binary search
linear search and binary search
Zia Ush Shamszaman
 
Searching linear &amp; binary search
Searching linear &amp; binary searchSearching linear &amp; binary search
Searching linear &amp; binary search
nikunjandy
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
Mohammed Hussein
 
Binary search
Binary searchBinary search
Binary search
Gaurav Solanki
 
Algorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching AlgorithmsAlgorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching Algorithms
Mohamed Loey
 
Searching algorithm
Searching algorithmSearching algorithm
Searching algorithm
MG Thushara Pradeesh
 
Searching in c language
Searching in c languageSearching in c language
Searching in c language
CHANDAN KUMAR
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
CHANDAN KUMAR
 
Searching techniques
Searching techniquesSearching techniques
Searching techniques
Archana Burujwale
 
Linear search in ds
Linear search in dsLinear search in ds
Linear search in ds
chauhankapil
 
366 it elective 4 (analysis of algoritm)
366 it elective 4 (analysis of algoritm)366 it elective 4 (analysis of algoritm)
366 it elective 4 (analysis of algoritm)
Neil Soliven
 

What's hot (20)

Linear search algorithm
Linear search algorithmLinear search algorithm
Linear search algorithm
 
Sequential & binary, linear search
Sequential & binary, linear searchSequential & binary, linear search
Sequential & binary, linear search
 
Searching algorithms
Searching algorithmsSearching algorithms
Searching algorithms
 
Algorithm & data structures lec4&5
Algorithm & data structures lec4&5Algorithm & data structures lec4&5
Algorithm & data structures lec4&5
 
Linear and Bianry search
Linear and Bianry searchLinear and Bianry search
Linear and Bianry search
 
Binary search algorithm
Binary search algorithmBinary search algorithm
Binary search algorithm
 
Linear Search Data Structure
Linear Search Data StructureLinear Search Data Structure
Linear Search Data Structure
 
linear search and binary search
linear search and binary searchlinear search and binary search
linear search and binary search
 
Chapter 14
Chapter 14Chapter 14
Chapter 14
 
Searching linear &amp; binary search
Searching linear &amp; binary searchSearching linear &amp; binary search
Searching linear &amp; binary search
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Binary search
Binary searchBinary search
Binary search
 
Algorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching AlgorithmsAlgorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching Algorithms
 
Searching algorithm
Searching algorithmSearching algorithm
Searching algorithm
 
L10 sorting-searching
L10 sorting-searchingL10 sorting-searching
L10 sorting-searching
 
Searching in c language
Searching in c languageSearching in c language
Searching in c language
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Searching techniques
Searching techniquesSearching techniques
Searching techniques
 
Linear search in ds
Linear search in dsLinear search in ds
Linear search in ds
 
366 it elective 4 (analysis of algoritm)
366 it elective 4 (analysis of algoritm)366 it elective 4 (analysis of algoritm)
366 it elective 4 (analysis of algoritm)
 

Similar to Searching

Searching and Sorting Algorithms in Data Structures
Searching and Sorting Algorithms  in Data StructuresSearching and Sorting Algorithms  in Data Structures
Searching and Sorting Algorithms in Data Structures
poongothai11
 
Searching in DSA that follow a dsa searching.pptx
Searching in DSA that follow a dsa searching.pptxSearching in DSA that follow a dsa searching.pptx
Searching in DSA that follow a dsa searching.pptx
StephenRobert15
 
Searching_Sorting.pptx
Searching_Sorting.pptxSearching_Sorting.pptx
Searching_Sorting.pptx
21BD1A058RSahithi
 
Linear and Binary search .pptx
Linear and Binary search .pptxLinear and Binary search .pptx
Linear and Binary search .pptx
p83629918
 
Linear and binary search
Linear and binary searchLinear and binary search
Linear and binary search
JeoJoyA
 
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
 
Lecture_Oct26.pptx
Lecture_Oct26.pptxLecture_Oct26.pptx
Lecture_Oct26.pptx
SylrizcinMarieManzo3
 
Unit iv(dsc++)
Unit iv(dsc++)Unit iv(dsc++)
Unit iv(dsc++)
Durga Devi
 
MODULE 5-Searching and-sorting
MODULE 5-Searching and-sortingMODULE 5-Searching and-sorting
MODULE 5-Searching and-sorting
nikshaikh786
 
PPT.pptx Searching and Sorting Techniques
PPT.pptx Searching and Sorting TechniquesPPT.pptx Searching and Sorting Techniques
PPT.pptx Searching and Sorting Techniques
Vaibhav Parjane
 
In the binary search, if the array being searched has 32 elements in.pdf
In the binary search, if the array being searched has 32 elements in.pdfIn the binary search, if the array being searched has 32 elements in.pdf
In the binary search, if the array being searched has 32 elements in.pdf
arpitaeron555
 
Searching, Sorting and Complexity analysis in DS.pdf
Searching, Sorting and Complexity analysis in DS.pdfSearching, Sorting and Complexity analysis in DS.pdf
Searching, Sorting and Complexity analysis in DS.pdf
AkshatMehrotra14
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
haramaya university
 
Searching Sorting
Searching SortingSearching Sorting
Searching Sortingguest2cb109
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3
infanciaj
 
searching in data structure.pptx
searching in data structure.pptxsearching in data structure.pptx
searching in data structure.pptx
chouguleamruta24
 
21-algorithms.ppt
21-algorithms.ppt21-algorithms.ppt
21-algorithms (1).ppt
21-algorithms (1).ppt21-algorithms (1).ppt
21-algorithms (1).ppt
DaniloMislosAlbay
 
21CS32 DS Module 1 PPT.pptx
21CS32 DS Module 1 PPT.pptx21CS32 DS Module 1 PPT.pptx
21CS32 DS Module 1 PPT.pptx
reddy19841
 

Similar to Searching (20)

Searching and Sorting Algorithms in Data Structures
Searching and Sorting Algorithms  in Data StructuresSearching and Sorting Algorithms  in Data Structures
Searching and Sorting Algorithms in Data Structures
 
Searching in DSA that follow a dsa searching.pptx
Searching in DSA that follow a dsa searching.pptxSearching in DSA that follow a dsa searching.pptx
Searching in DSA that follow a dsa searching.pptx
 
Searching_Sorting.pptx
Searching_Sorting.pptxSearching_Sorting.pptx
Searching_Sorting.pptx
 
Linear and Binary search .pptx
Linear and Binary search .pptxLinear and Binary search .pptx
Linear and Binary search .pptx
 
Linear and binary search
Linear and binary searchLinear and binary search
Linear and binary search
 
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
 
Lecture_Oct26.pptx
Lecture_Oct26.pptxLecture_Oct26.pptx
Lecture_Oct26.pptx
 
Unit iv(dsc++)
Unit iv(dsc++)Unit iv(dsc++)
Unit iv(dsc++)
 
MODULE 5-Searching and-sorting
MODULE 5-Searching and-sortingMODULE 5-Searching and-sorting
MODULE 5-Searching and-sorting
 
PPT.pptx Searching and Sorting Techniques
PPT.pptx Searching and Sorting TechniquesPPT.pptx Searching and Sorting Techniques
PPT.pptx Searching and Sorting Techniques
 
In the binary search, if the array being searched has 32 elements in.pdf
In the binary search, if the array being searched has 32 elements in.pdfIn the binary search, if the array being searched has 32 elements in.pdf
In the binary search, if the array being searched has 32 elements in.pdf
 
Searching, Sorting and Complexity analysis in DS.pdf
Searching, Sorting and Complexity analysis in DS.pdfSearching, Sorting and Complexity analysis in DS.pdf
Searching, Sorting and Complexity analysis in DS.pdf
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
 
Searching Sorting
Searching SortingSearching Sorting
Searching Sorting
 
9 Arrays
9 Arrays9 Arrays
9 Arrays
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3
 
searching in data structure.pptx
searching in data structure.pptxsearching in data structure.pptx
searching in data structure.pptx
 
21-algorithms.ppt
21-algorithms.ppt21-algorithms.ppt
21-algorithms.ppt
 
21-algorithms (1).ppt
21-algorithms (1).ppt21-algorithms (1).ppt
21-algorithms (1).ppt
 
21CS32 DS Module 1 PPT.pptx
21CS32 DS Module 1 PPT.pptx21CS32 DS Module 1 PPT.pptx
21CS32 DS Module 1 PPT.pptx
 

More from CGC Technical campus,Mohali

Gender Issues CS.pptx
Gender Issues CS.pptxGender Issues CS.pptx
Gender Issues CS.pptx
CGC Technical campus,Mohali
 
Intellectual Property Rights.pptx
Intellectual Property Rights.pptxIntellectual Property Rights.pptx
Intellectual Property Rights.pptx
CGC Technical campus,Mohali
 
Cyber Safety ppt.pptx
Cyber Safety ppt.pptxCyber Safety ppt.pptx
Cyber Safety ppt.pptx
CGC Technical campus,Mohali
 
Python Modules .pptx
Python Modules .pptxPython Modules .pptx
Python Modules .pptx
CGC Technical campus,Mohali
 
Dynamic allocation
Dynamic allocationDynamic allocation
Dynamic allocation
CGC Technical campus,Mohali
 
Arrays in c
Arrays in cArrays in c
Control statments in c
Control statments in cControl statments in c
Control statments in c
CGC Technical campus,Mohali
 
Operators in c by anupam
Operators in c by anupamOperators in c by anupam
Operators in c by anupam
CGC Technical campus,Mohali
 
Datatypes in c
Datatypes in cDatatypes in c
Fundamentals of-computer
Fundamentals of-computerFundamentals of-computer
Fundamentals of-computer
CGC Technical campus,Mohali
 
File handling-c
File handling-cFile handling-c
Structure in C language
Structure in C languageStructure in C language
Structure in C language
CGC Technical campus,Mohali
 
Function in c program
Function in c programFunction in c program
Function in c program
CGC Technical campus,Mohali
 
Function in c
Function in cFunction in c
string in C
string in Cstring in C
C arrays
C arraysC arrays
Data processing and Report writing in Research(Section E)
Data processing and Report writing in Research(Section E)Data processing and Report writing in Research(Section E)
Data processing and Report writing in Research(Section E)
CGC Technical campus,Mohali
 
data analysis and report wring in research (Section d)
data analysis and report wring  in research (Section d)data analysis and report wring  in research (Section d)
data analysis and report wring in research (Section d)
CGC Technical campus,Mohali
 
Section C(Analytical and descriptive surveys... )
Section C(Analytical and descriptive surveys... )Section C(Analytical and descriptive surveys... )
Section C(Analytical and descriptive surveys... )
CGC Technical campus,Mohali
 

More from CGC Technical campus,Mohali (20)

Gender Issues CS.pptx
Gender Issues CS.pptxGender Issues CS.pptx
Gender Issues CS.pptx
 
Intellectual Property Rights.pptx
Intellectual Property Rights.pptxIntellectual Property Rights.pptx
Intellectual Property Rights.pptx
 
Cyber Safety ppt.pptx
Cyber Safety ppt.pptxCyber Safety ppt.pptx
Cyber Safety ppt.pptx
 
Python Modules .pptx
Python Modules .pptxPython Modules .pptx
Python Modules .pptx
 
Dynamic allocation
Dynamic allocationDynamic allocation
Dynamic allocation
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
Control statments in c
Control statments in cControl statments in c
Control statments in c
 
Operators in c by anupam
Operators in c by anupamOperators in c by anupam
Operators in c by anupam
 
Datatypes in c
Datatypes in cDatatypes in c
Datatypes in c
 
Fundamentals of-computer
Fundamentals of-computerFundamentals of-computer
Fundamentals of-computer
 
File handling-c
File handling-cFile handling-c
File handling-c
 
Structure in C language
Structure in C languageStructure in C language
Structure in C language
 
Intro
IntroIntro
Intro
 
Function in c program
Function in c programFunction in c program
Function in c program
 
Function in c
Function in cFunction in c
Function in c
 
string in C
string in Cstring in C
string in C
 
C arrays
C arraysC arrays
C arrays
 
Data processing and Report writing in Research(Section E)
Data processing and Report writing in Research(Section E)Data processing and Report writing in Research(Section E)
Data processing and Report writing in Research(Section E)
 
data analysis and report wring in research (Section d)
data analysis and report wring  in research (Section d)data analysis and report wring  in research (Section d)
data analysis and report wring in research (Section d)
 
Section C(Analytical and descriptive surveys... )
Section C(Analytical and descriptive surveys... )Section C(Analytical and descriptive surveys... )
Section C(Analytical and descriptive surveys... )
 

Recently uploaded

Forklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella PartsForklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella Parts
Intella Parts
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
Jayaprasanna4
 
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang,  ICLR 2024, MLILAB, KAIST AI.pdfJ.Yang,  ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
seandesed
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Teleport Manpower Consultant
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Dr.Costas Sachpazis
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
ssuser9bd3ba
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
SamSarthak3
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
Divya Somashekar
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
TeeVichai
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
R&R Consult
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
AJAYKUMARPUND1
 
ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdf
AhmedHussein950959
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
obonagu
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Sreedhar Chowdam
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
ankuprajapati0525
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
karthi keyan
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
fxintegritypublishin
 

Recently uploaded (20)

Forklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella PartsForklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella Parts
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
 
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang,  ICLR 2024, MLILAB, KAIST AI.pdfJ.Yang,  ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
 
ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdf
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
 

Searching

  • 1. By: Er. Anupam Sharma Assistant Professor in CSE Searching
  • 2. Linear search Linear search is the simplest searching algorithm which is sometimes known as sequential search. In this algorithm each element of array is compared with the targeted element sequentially. Worst Case Time Complexity -O(N) Best Case Time Complexity: O(1)
  • 3. Linear Search Algorithm Linear Search ( Array A, Value x) Step 1: Set i to 1 Step 2: if i > n then go to step 7 Step 3: if A[i] = x then go to step 6 Step 4: Set i to i + 1 Step 5: Go to Step 2 Step 6: Print Element x Found at index i and go to step 8 Step 7: Print element not found Step 8: Exit
  • 4. Pseudocode for Linear Search procedure linear_search (list, value) for each item in the list if match item == value return the item's location end if end for end procedure
  • 6. Program for Linear Search in C #include<stdio.h> int main() { int a[20],i,x,n; printf("How many elements?"); scanf("%d",&n); printf("Enter array elements:n"); for(i=0;i<n;++i) scanf("%d",&a[i]); printf("nEnter element to search:"); scanf("%d",&x); for(i=0;i<n;++i) if(a[i]==x) break; if(i<n) printf("Element found at index %d",i); else printf("Element not found"); return 0; } Output How many elements?4 Enter array elements: 6 8 9 1 Enter element to search:9 Element found at index 2
  • 7. Binary Search Binary search algorithm can be applied on a sorted array to search an element. Search begins with comparing middle element of array to target element. If both are equal then position of element is returned. If target element is less than middle element of array then upper half of array is discarded and again search continued by dividing the lower half. If target element is greater than middle element then lower half is discarded and search is continued in upper half.  Worst Case Time Complexity: O(log n)  Best Case Time Complexity: O(1)
  • 8. Binary Search Algorithm  Following are the steps of implementation that we will be following:  Start with the middle element:  If the target value is equal to the middle element of the array, then return the index of the middle element.  If not, then compare the middle element with the target value,  If the target value is greater than the number in the middle index, then pick the elements to the right of the middle index, and start with Step 1.  If the target value is less than the number in the middle index, then pick the elements to the left of the middle index, and start with Step 1.  When a match is found, return the index of the element matched.
  • 9. Pseudo code for Binary Search Procedure binary_search A ← sorted array n ← size of array x ← value to be searched Set lowerBound = 1 Set upperBound = n while x not found if upperBound < lowerBound EXIT: x does not exists. set midPoint = lowerBound + ( upperBound - lowerBound ) / 2 if A[midPoint] < x set lowerBound = midPoint + 1 if A[midPoint] > x set upperBound = midPoint - 1 if A[midPoint] = x EXIT: x found at location midPoint end while end procedure
  • 11. #include <stdio.h> int main() { int c, first, last, middle, n, search, array[100]; printf("Enter number of elementsn"); scanf("%d",&n); printf("Enter %d integersn", n); for (c = 0; c < n; c++) scanf("%d",&array[c]); printf("Enter value to findn"); scanf("%d", &search); first = 0; last = n - 1; middle = (first+last)/2; while (first <= last) { if (array[middle] < search) first = middle + 1; else if (array[middle] == search) { printf("%d found at location %d.n", search, middle+1); break; } else last = middle - 1; middle = (first + last)/2; } if (first > last) printf("Not found! %d isn't present in the list.n", search); return 0; } Program for Binary Search in C