SlideShare a Scribd company logo
1 of 9
Searching
Linear Search
Linear Search in Java is a searching algorithm that is used to find the index
of the desired element in an array. It is the simplest and most basic searching
algorithm. In this algorithm, we sequentially visit each element of an array until
the target element is found.
Implementation of linear search
public static void main(String[] args) {
// TODO Auto-generated method stub
int arr[]= {1,3,4,6,8};
int key=4;
for(int i=0;i<arr.length;i++) {
if(arr[i]==key) {
System.out.println(i);
}
}
Binary Search Algorithm
Binary Search in Java is a search algorithm that finds the
position of a target value within a sorted array. Binary
search compares the target value to the middle element of
the array. It works only on a sorted set of elements
Implementation of binary search
public class Binary_Search {
void binarySearch(int arr[], int key) {
int first=0;
int last=arr.length-1;
while(first<=last) {
int mid=(first+last)/2;
if(arr[mid]==key) {
System.out.println("Element present at index: "+mid);
break;
}
else if(arr[mid]<key) {
first=mid+1;
}
else {
last=mid-1;
}
}
}
public static void main(String[] args) {
int arr[]= {12,2,35,62,85,24,98,63};
int key=24;
Arrays.sort(arr);
for(int i=0;i<arr.length;i++) {
System.out.println(arr[i]+ " ");
}
Binary_Search ob=new Binary_Search();
ob.binarySearch(arr,key);
}
}
THANK YOU

More Related Content

Similar to Searching.pptx

Binary search
Binary search Binary search
Binary search
Raghu nath
 
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
 
Java Algorithm Interview Questions & Answers .pdf
Java Algorithm Interview Questions & Answers .pdfJava Algorithm Interview Questions & Answers .pdf
Java Algorithm Interview Questions & Answers .pdf
NiravPanchal50
 
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
 
Java class 5
Java class 5Java class 5
Java class 5
Edureka!
 
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
 
seaching internal 2 ppt
seaching internal 2 pptseaching internal 2 ppt
seaching internal 2 ppt
SubhrasisBiswal1
 

Similar to Searching.pptx (20)

INDEX SORT
INDEX SORTINDEX SORT
INDEX SORT
 
Searching
Searching Searching
Searching
 
Chapter 14
Chapter 14Chapter 14
Chapter 14
 
4.1 sequentioal search
4.1 sequentioal search4.1 sequentioal search
4.1 sequentioal search
 
6_Array.pptx
6_Array.pptx6_Array.pptx
6_Array.pptx
 
Binary search
Binary search Binary search
Binary search
 
Java Programming- 1) Write a recursive method that finds and returns t.docx
Java Programming- 1) Write a recursive method that finds and returns t.docxJava Programming- 1) Write a recursive method that finds and returns t.docx
Java Programming- 1) Write a recursive method that finds and returns t.docx
 
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
 
MODULE 5-Searching and-sorting
MODULE 5-Searching and-sortingMODULE 5-Searching and-sorting
MODULE 5-Searching and-sorting
 
Searching
SearchingSearching
Searching
 
STRINGS IN JAVA
STRINGS IN JAVASTRINGS IN JAVA
STRINGS IN JAVA
 
searching
searchingsearching
searching
 
Java Algorithm Interview Questions & Answers .pdf
Java Algorithm Interview Questions & Answers .pdfJava Algorithm Interview Questions & Answers .pdf
Java Algorithm Interview Questions & Answers .pdf
 
Binary search in ds
Binary search in dsBinary search in ds
Binary search in ds
 
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
 
Java class 5
Java class 5Java class 5
Java class 5
 
Search Algprithms
Search AlgprithmsSearch Algprithms
Search Algprithms
 
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
 
seaching internal 2 ppt
seaching internal 2 pptseaching internal 2 ppt
seaching internal 2 ppt
 
RecursiveBinarySearch.pptx
RecursiveBinarySearch.pptxRecursiveBinarySearch.pptx
RecursiveBinarySearch.pptx
 

More from HemantSharma134028 (7)

b0560b5d-d52d-4bc1-be9c-55aed5fae5e5-151030074028-lva1-app6892.pptx
b0560b5d-d52d-4bc1-be9c-55aed5fae5e5-151030074028-lva1-app6892.pptxb0560b5d-d52d-4bc1-be9c-55aed5fae5e5-151030074028-lva1-app6892.pptx
b0560b5d-d52d-4bc1-be9c-55aed5fae5e5-151030074028-lva1-app6892.pptx
 
Multithreading.pptx
Multithreading.pptxMultithreading.pptx
Multithreading.pptx
 
Introduction_to_Java.ppt
Introduction_to_Java.pptIntroduction_to_Java.ppt
Introduction_to_Java.ppt
 
HHW OF IT PRESENTATION ON INTERNET SECURITY BY AASTHA SHARMA 10TH KEATS^.pptx
HHW OF IT PRESENTATION ON INTERNET SECURITY BY AASTHA SHARMA 10TH KEATS^.pptxHHW OF IT PRESENTATION ON INTERNET SECURITY BY AASTHA SHARMA 10TH KEATS^.pptx
HHW OF IT PRESENTATION ON INTERNET SECURITY BY AASTHA SHARMA 10TH KEATS^.pptx
 
Lec7!JavaThreads.ppt
Lec7!JavaThreads.pptLec7!JavaThreads.ppt
Lec7!JavaThreads.ppt
 
java basics.pptx
java basics.pptxjava basics.pptx
java basics.pptx
 
Heap Sort (project).ppt
Heap Sort (project).pptHeap Sort (project).ppt
Heap Sort (project).ppt
 

Recently uploaded

Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
Epec Engineered Technologies
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
amitlee9823
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
MsecMca
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Recently uploaded (20)

Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equation
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 
22-prompt engineering noted slide shown.pdf
22-prompt engineering noted slide shown.pdf22-prompt engineering noted slide shown.pdf
22-prompt engineering noted slide shown.pdf
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 

Searching.pptx

  • 2.
  • 3. Linear Search Linear Search in Java is a searching algorithm that is used to find the index of the desired element in an array. It is the simplest and most basic searching algorithm. In this algorithm, we sequentially visit each element of an array until the target element is found.
  • 4. Implementation of linear search public static void main(String[] args) { // TODO Auto-generated method stub int arr[]= {1,3,4,6,8}; int key=4; for(int i=0;i<arr.length;i++) { if(arr[i]==key) { System.out.println(i); } }
  • 5.
  • 6. Binary Search Algorithm Binary Search in Java is a search algorithm that finds the position of a target value within a sorted array. Binary search compares the target value to the middle element of the array. It works only on a sorted set of elements
  • 7. Implementation of binary search public class Binary_Search { void binarySearch(int arr[], int key) { int first=0; int last=arr.length-1; while(first<=last) { int mid=(first+last)/2; if(arr[mid]==key) { System.out.println("Element present at index: "+mid); break; } else if(arr[mid]<key) { first=mid+1; } else { last=mid-1; } } } public static void main(String[] args) { int arr[]= {12,2,35,62,85,24,98,63}; int key=24; Arrays.sort(arr); for(int i=0;i<arr.length;i++) { System.out.println(arr[i]+ " "); } Binary_Search ob=new Binary_Search(); ob.binarySearch(arr,key); } }
  • 8.