SlideShare a Scribd company logo
1 of 12
Searching methods
• is an operation or a technique that helps finds the place of a
given element or value in the list.
• are designed to check for an element or retrieve an element
from any data structure where it is stored.
• is said to be successful or unsuccessful depending upon
whether the element that is being searched is found or not.
• standard techniques
• Sequential Search
• Interval Search
Linear Search
• simplest method for searching
• defined as a sequential search algorithm
• 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
Linear Search
• Given an array arr[] of N elements, the task is to write a
function to search a given element x in arr[].
• Iterate from 0 to N-1 and compare the value of every index with
x if they match return index.
Steps for Linear Search
• Start from the leftmost element of arr[] and one by one compare
x with each element of arr[]
• If x matches with an element, return the index.
• If x doesn’t match with any of the elements, return -1.
Algorithm for Linear Search
• algorithm Seqnl_Search(list, item)
• Pre: list != ;
• Post: return the index of the item if found, otherwise: 1
• index <- fi
• while index < list.Cnt and list[index] != item //cnt: counter variable
• index <- index + 1
• end while
• if index < list.Cnt and list[index] = item
• return index
• end if
• return: 1
Linear Search implementation in C
Binary search
• is a very fast and efficient searching technique.
• requires the list to be in sorted order.
Binary search
• execution needs to compare the element to be searched with the
present element at the center of the list.
• If it matches, then the search is successful otherwise the list is
divided into two halves:
• one from the 0th element to the middle element which is the
center element (first half)
• another from the center element to the last element (which is the
2nd half) where all values are greater than the center element.
• proceeds from either of the two halves depending upon whether the
target element is greater or smaller than the central element.
• If the element is smaller than the central element, then searching is
done in the first half, otherwise searching is done in the second half.
Algorithm for Binary Search
• algorithm Binary_Search(list, item)
• Set L to 0 and R to n: 1
• if L > R, then Binary_Search terminates as unsuccessful
• else
• Set m (the position in the mid element) to the floor of (L + R) / 2
• if Am < T, set L to m + 1 and go to step 3
• if Am > T, set R to m: 1 and go to step 3
• Now, Am = T,
• the search is done; return (m)
Binary Search implementation in C
Linear Search and Binary Search
Linear Search and Binary Search

More Related Content

Similar to unit II_2_i.pptx

DSA Lec 5+6(Search+Sort) (1).pdf
DSA Lec 5+6(Search+Sort) (1).pdfDSA Lec 5+6(Search+Sort) (1).pdf
DSA Lec 5+6(Search+Sort) (1).pdfMustafaJutt4
 
linear search and binary search
linear search and binary searchlinear search and binary search
linear search and binary searchZia Ush Shamszaman
 
data_structure_Chapter two_computer.pptx
data_structure_Chapter two_computer.pptxdata_structure_Chapter two_computer.pptx
data_structure_Chapter two_computer.pptxMohammed472103
 
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
 
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 Algorithm03446940736
 
Dsa – data structure and algorithms searching
Dsa – data structure and algorithms   searchingDsa – data structure and algorithms   searching
Dsa – data structure and algorithms searchingsajinis3
 
programming in C
programming in Cprogramming in C
programming in CADITHYAM19
 
Data Structure & Algorithms - Operations
Data Structure & Algorithms - OperationsData Structure & Algorithms - Operations
Data Structure & Algorithms - Operationsbabuk110
 
1.4 Sorting.pptx
1.4 Sorting.pptx1.4 Sorting.pptx
1.4 Sorting.pptxSujan527908
 
Binary search algorithm.pptx
Binary search  algorithm.pptxBinary search  algorithm.pptx
Binary search algorithm.pptxbhuvansaachi18
 
DS PPT - ( 1 )SORTING lgoritham techniques with bast example
DS PPT - ( 1 )SORTING lgoritham techniques with bast exampleDS PPT - ( 1 )SORTING lgoritham techniques with bast example
DS PPT - ( 1 )SORTING lgoritham techniques with bast exampleVivek487417
 
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).pptxAyanMandal44
 
Searching Sorting
Searching SortingSearching Sorting
Searching Sortingguest2cb109
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdfharamaya university
 
Data structure and algorithms
Data structure and algorithmsData structure and algorithms
Data structure and algorithmstechnologygyan
 

Similar to unit II_2_i.pptx (20)

DSA Lec 5+6(Search+Sort) (1).pdf
DSA Lec 5+6(Search+Sort) (1).pdfDSA Lec 5+6(Search+Sort) (1).pdf
DSA Lec 5+6(Search+Sort) (1).pdf
 
Searching
SearchingSearching
Searching
 
seaching internal 2 ppt
seaching internal 2 pptseaching internal 2 ppt
seaching internal 2 ppt
 
linear search and binary search
linear search and binary searchlinear search and binary search
linear search and binary search
 
data_structure_Chapter two_computer.pptx
data_structure_Chapter two_computer.pptxdata_structure_Chapter two_computer.pptx
data_structure_Chapter two_computer.pptx
 
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
 
Searching_Sorting.pptx
Searching_Sorting.pptxSearching_Sorting.pptx
Searching_Sorting.pptx
 
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
 
cs702 ppt.ppt
cs702 ppt.pptcs702 ppt.ppt
cs702 ppt.ppt
 
Dsa – data structure and algorithms searching
Dsa – data structure and algorithms   searchingDsa – data structure and algorithms   searching
Dsa – data structure and algorithms searching
 
programming in C
programming in Cprogramming in C
programming in C
 
Data Structure & Algorithms - Operations
Data Structure & Algorithms - OperationsData Structure & Algorithms - Operations
Data Structure & Algorithms - Operations
 
1.4 Sorting.pptx
1.4 Sorting.pptx1.4 Sorting.pptx
1.4 Sorting.pptx
 
Binary search algorithm.pptx
Binary search  algorithm.pptxBinary search  algorithm.pptx
Binary search algorithm.pptx
 
DS PPT - ( 1 )SORTING lgoritham techniques with bast example
DS PPT - ( 1 )SORTING lgoritham techniques with bast exampleDS PPT - ( 1 )SORTING lgoritham techniques with bast example
DS PPT - ( 1 )SORTING lgoritham techniques with bast example
 
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
 
Searching Sorting
Searching SortingSearching Sorting
Searching Sorting
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
 
Data structure and algorithms
Data structure and algorithmsData structure and algorithms
Data structure and algorithms
 

More from HODElex

Unit 4_Network Layer_Part II.pptx
Unit 4_Network Layer_Part II.pptxUnit 4_Network Layer_Part II.pptx
Unit 4_Network Layer_Part II.pptxHODElex
 
Unit VI - Graphs.ppt
Unit VI - Graphs.pptUnit VI - Graphs.ppt
Unit VI - Graphs.pptHODElex
 
IEEE and Lower Level LAN Protocols.ppt
IEEE and Lower Level LAN Protocols.pptIEEE and Lower Level LAN Protocols.ppt
IEEE and Lower Level LAN Protocols.pptHODElex
 
I & E Tutorials.pdf
I & E Tutorials.pdfI & E Tutorials.pdf
I & E Tutorials.pdfHODElex
 
wireless lan presentation.ppt
wireless lan presentation.pptwireless lan presentation.ppt
wireless lan presentation.pptHODElex
 
Unit II_Searching and Sorting Algorithms.ppt
Unit II_Searching and Sorting Algorithms.pptUnit II_Searching and Sorting Algorithms.ppt
Unit II_Searching and Sorting Algorithms.pptHODElex
 
Unit 3_Network Layer_Part II.pptx
Unit 3_Network Layer_Part II.pptxUnit 3_Network Layer_Part II.pptx
Unit 3_Network Layer_Part II.pptxHODElex
 

More from HODElex (7)

Unit 4_Network Layer_Part II.pptx
Unit 4_Network Layer_Part II.pptxUnit 4_Network Layer_Part II.pptx
Unit 4_Network Layer_Part II.pptx
 
Unit VI - Graphs.ppt
Unit VI - Graphs.pptUnit VI - Graphs.ppt
Unit VI - Graphs.ppt
 
IEEE and Lower Level LAN Protocols.ppt
IEEE and Lower Level LAN Protocols.pptIEEE and Lower Level LAN Protocols.ppt
IEEE and Lower Level LAN Protocols.ppt
 
I & E Tutorials.pdf
I & E Tutorials.pdfI & E Tutorials.pdf
I & E Tutorials.pdf
 
wireless lan presentation.ppt
wireless lan presentation.pptwireless lan presentation.ppt
wireless lan presentation.ppt
 
Unit II_Searching and Sorting Algorithms.ppt
Unit II_Searching and Sorting Algorithms.pptUnit II_Searching and Sorting Algorithms.ppt
Unit II_Searching and Sorting Algorithms.ppt
 
Unit 3_Network Layer_Part II.pptx
Unit 3_Network Layer_Part II.pptxUnit 3_Network Layer_Part II.pptx
Unit 3_Network Layer_Part II.pptx
 

Recently uploaded

Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...RajaP95
 

Recently uploaded (20)

Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
 

unit II_2_i.pptx

  • 1. Searching methods • is an operation or a technique that helps finds the place of a given element or value in the list. • are designed to check for an element or retrieve an element from any data structure where it is stored. • is said to be successful or unsuccessful depending upon whether the element that is being searched is found or not. • standard techniques • Sequential Search • Interval Search
  • 2. Linear Search • simplest method for searching • defined as a sequential search algorithm • 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
  • 3. Linear Search • Given an array arr[] of N elements, the task is to write a function to search a given element x in arr[]. • Iterate from 0 to N-1 and compare the value of every index with x if they match return index.
  • 4. Steps for Linear Search • Start from the leftmost element of arr[] and one by one compare x with each element of arr[] • If x matches with an element, return the index. • If x doesn’t match with any of the elements, return -1.
  • 5. Algorithm for Linear Search • algorithm Seqnl_Search(list, item) • Pre: list != ; • Post: return the index of the item if found, otherwise: 1 • index <- fi • while index < list.Cnt and list[index] != item //cnt: counter variable • index <- index + 1 • end while • if index < list.Cnt and list[index] = item • return index • end if • return: 1
  • 7. Binary search • is a very fast and efficient searching technique. • requires the list to be in sorted order.
  • 8. Binary search • execution needs to compare the element to be searched with the present element at the center of the list. • If it matches, then the search is successful otherwise the list is divided into two halves: • one from the 0th element to the middle element which is the center element (first half) • another from the center element to the last element (which is the 2nd half) where all values are greater than the center element. • proceeds from either of the two halves depending upon whether the target element is greater or smaller than the central element. • If the element is smaller than the central element, then searching is done in the first half, otherwise searching is done in the second half.
  • 9. Algorithm for Binary Search • algorithm Binary_Search(list, item) • Set L to 0 and R to n: 1 • if L > R, then Binary_Search terminates as unsuccessful • else • Set m (the position in the mid element) to the floor of (L + R) / 2 • if Am < T, set L to m + 1 and go to step 3 • if Am > T, set R to m: 1 and go to step 3 • Now, Am = T, • the search is done; return (m)
  • 11. Linear Search and Binary Search
  • 12. Linear Search and Binary Search