SlideShare a Scribd company logo
Data Structure
 Unit-I Part B
Searching
 It is a method to find a element or data.
Searching Types
Linear Search Binary Search
List search
 The algorithm used to search a list depends to a large extent on the structure of
the list.
 The two basic searches for arrays are the sequential or linear search and binary
search.
 Sequential or Linear Search
 It can be used to locate an item in any array.
 Binary Search
 It requires on ordered list.
Sequential or Linear Search:
 Search in sequential order.
 It is used whether the list is not ordered.
 Example:
 It is used only for small lists or lists that aren’t searched often.
 We start searching for the target at the beginning of the list and continue until we
find the target.
 Termination Condition
 Target is found (Success)
 List of elements is exhausted or Target is not found (failure)
Data structure unit I part B
Algorithm
 Step 1: The list we are searching
 Step 2: An index to the last element in the list.
 Step 3: The target
 Step 4: The address where the found element’s index location is to be stored.
 Step 5: To tell calling algorithm whether data were located
 Step 6: We return a Boolean - true for we found it or false for we didn’t found it.
 Note: As an alternative to the index to the last element, the size of the list may be
passed.
Data structure unit I part B
Data structure unit I part B
Data Structures: A
Pseudocode
Approach with C,
Second Edition
9
Successful
search of an
unordered list
Data Structures: A
Pseudocode
Approach with C,
Second Edition
10
Unsuccessful
search in
unordered list
Data Structures: A
Pseudocode
Approach with C,
Second Edition
11
Sequential
search
algorithm
Variations on Sequential Search
 Three useful variations on the sequential search algorithm:
 The sentinel search
 The probability search
 The ordered list search
Sentinel Search
 Loop tests two conditions:
 End of list
 Target not found.
Knuth states that, “When the inner loop of a program tests two or more conditions, an
attempt should be made to reduce it to just one condition”
 The only way we can ensure that a target is actually in the list is to put it there our self.
 A target is put in the list by adding an extra element (sentinel entry) at the end of the array and
placing the target in the sentinel.
 We can then optimize the loop and determine after the loop completes whether we found actual
data or the sentinel.
 Disadvantage:
 The rest of the processing must be careful to never look at the sentinel element at the end of the
list.
Data structure unit I part B
Probability Search
 The array is ordered with the most probable search elements at the beginning of
the array and the least probable search elements at the end.
 It is especially useful when relatively few elements are the targets for most of the
searches.
 To ensure that the probability ordering is correct over time, in each search we
exchange the located element with the element immediately before it in the array.
Data structure unit I part B
Ordered List Search
 Although we generally recommend a binary search when searching a list ordered on the key
(target), if the list is small it may be more efficient to use a sequential search.
 When searching an ordered list sequentially, however, it is not necessary to search to the end of
the list to determine that the target is not in the list.
 We can stop when the target becomes less than or equal to the current element we are testing.
 In addition, we can incorporate the sentinel concept by bypassing the search loop when the
target is greater than the last item.
 In other words, when the target is less than or equal to the last element, the last element
becomes a sentinel, allowing us to eliminate the test for the end of the list.
 Although it can be used with array implementations, the ordered list search is more commonly
used when searching linked list implementations.
Data structure unit I part B
Binary Search
 The sequential search algorithm is very slow.
 If we have an array of 1000 elements, we must make 1000 comparisons in the worst case.
 If the array is not sorted, the sequential search is the only solution.
 It the array is sorted, we can use a more efficient algorithm called binary search.
 The binary search starts by testing the data in the element at the middle of the array.
 To determine if the target is in the first or the second half of the list.
 mid = (begin + end) / 2
 If it is in the first half, we do not need to check the second half. If it is in the second half, we
do not need to test the first half.
 To find the middle of the list, we need three variables:
 Step 1: One to identify the beginning of the list
 Step 2: One to identify the middle of the list
 Step 3: One to identify the end of the list.
 We analyze two cases here:
 The target is in the list
 The target is not in the list.
Data structure unit I part B
Data Structures: A
Pseudocode
Approach with C,
Second Edition
22
Binary
search
example



 




 

2
2
lastfirst
mid
endbegin
mid
Data structure unit I part B
Data Structures: A
Pseudocode
Approach with C,
Second Edition
24
Figure 2-5
Unsuccessful
binary
search
example
Data Structures: A
Pseudocode
Approach with C,
Second Edition
25
Data Structures: A
Pseudocode
Approach with C,
Second Edition
26
(continued)
Data Structures: A Pseudocode Approach with C, Second Edition 27
Analyzing search algorithms
The efficiency of the
sequential search is
O(n).
The efficiency of the
binary search is
O(log n).
List size Iterations
binary Sequential
(Average)
Sequential
(worst
case)
16 4 8 16
50 6 25 50
256 8 128 256
1000 10 500 1000
10000 14 5000 10000
100000 17 50000 100000
1000000 20 500000 1000000
 a) For Sequential Search The basic loop for the sequential search is shown below.
 The efficiency of the sequential search is O(n).
 The search efficiency for the sentinel search is basically the same as for the sequential search.
 Although the sentinel search saves a few instructions in the loop, its design is identical.
 Therefore, it is also an O(n).
 b) The binary search locates an item by repeatedly dividing the list in half.
 Its loop is:
 This loop obviously divides, and it is therefore a logarithmic loop.
 The efficiency is the binary search is O(log n).
Ad

More Related Content

What's hot (11)

computer notes - Data Structures - 33
computer notes - Data Structures - 33computer notes - Data Structures - 33
computer notes - Data Structures - 33
ecomputernotes
 
Psy 870 module 3 problem set answers
Psy 870  module 3 problem set answersPsy 870  module 3 problem set answers
Psy 870 module 3 problem set answers
bestwriter
 
Lists methods
Lists methodsLists methods
Lists methods
aiclub_slides
 
Lists and loops
Lists and loopsLists and loops
Lists and loops
aiclub_slides
 
Quick sort data structures
Quick sort data structuresQuick sort data structures
Quick sort data structures
chauhankapil
 
Python list functions
Python list functionsPython list functions
Python list functions
Mokhtar Ebrahim
 
Link list assi
Link list assiLink list assi
Link list assi
PATILPANKAJ106130
 
In order to receive the points, you must provide me a very detailed solution....
In order to receive the points, you must provide me a very detailed solution....In order to receive the points, you must provide me a very detailed solution....
In order to receive the points, you must provide me a very detailed solution....
hwbloom460000
 
Introduction to lists
Introduction to listsIntroduction to lists
Introduction to lists
aiclub_slides
 
Dictionaries
DictionariesDictionaries
Dictionaries
aiclub_slides
 
Linked lists
Linked listsLinked lists
Linked lists
Hafsa Naeem
 
computer notes - Data Structures - 33
computer notes - Data Structures - 33computer notes - Data Structures - 33
computer notes - Data Structures - 33
ecomputernotes
 
Psy 870 module 3 problem set answers
Psy 870  module 3 problem set answersPsy 870  module 3 problem set answers
Psy 870 module 3 problem set answers
bestwriter
 
Quick sort data structures
Quick sort data structuresQuick sort data structures
Quick sort data structures
chauhankapil
 
In order to receive the points, you must provide me a very detailed solution....
In order to receive the points, you must provide me a very detailed solution....In order to receive the points, you must provide me a very detailed solution....
In order to receive the points, you must provide me a very detailed solution....
hwbloom460000
 
Introduction to lists
Introduction to listsIntroduction to lists
Introduction to lists
aiclub_slides
 

Similar to Data structure unit I part B (20)

Data Structures_ Sorting & Searching
Data Structures_ Sorting & SearchingData Structures_ Sorting & Searching
Data Structures_ Sorting & Searching
ThenmozhiK5
 
DS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptxDS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptx
prakashvs7
 
Analysis of Algorithm - Binary Search.pptx
Analysis of Algorithm - Binary Search.pptxAnalysis of Algorithm - Binary Search.pptx
Analysis of Algorithm - Binary Search.pptx
Maulana Abul Kalam Azad University of Technology
 
Searching_Sorting.pptx
Searching_Sorting.pptxSearching_Sorting.pptx
Searching_Sorting.pptx
21BD1A058RSahithi
 
Presentation on Searching and Sorting in C language.pptx
Presentation on Searching and Sorting in C language.pptxPresentation on Searching and Sorting in C language.pptx
Presentation on Searching and Sorting in C language.pptx
Krishnanandmishra15
 
arrays in c
arrays in carrays in c
arrays in c
vidhi mehta
 
Ch05 Black Jack
Ch05  Black  JackCh05  Black  Jack
Ch05 Black Jack
leminhvuong
 
Data structure Unit - II Searching and Sorting.pptx
Data structure Unit - II Searching and Sorting.pptxData structure Unit - II Searching and Sorting.pptx
Data structure Unit - II Searching and Sorting.pptx
gavanisanjana
 
searching
searchingsearching
searching
A. S. M. Shafi
 
Linear search-and-binary-search
Linear search-and-binary-searchLinear search-and-binary-search
Linear search-and-binary-search
International Islamic University
 
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
03446940736
 
Searching algorithms
Searching algorithmsSearching algorithms
Searching algorithms
Trupti Agrawal
 
seaching internal 2 ppt
seaching internal 2 pptseaching internal 2 ppt
seaching internal 2 ppt
SubhrasisBiswal1
 
Searching algorithms
Searching algorithmsSearching algorithms
Searching algorithms
Trupti Agrawal
 
Searching in c language
Searching in c languageSearching in c language
Searching in c language
CHANDAN KUMAR
 
Searching Algorithm, Linear search algorithm and their time complexity
Searching Algorithm, Linear search algorithm and their time complexitySearching Algorithm, Linear search algorithm and their time complexity
Searching Algorithm, Linear search algorithm and their time complexity
hamzamunawarkhan
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
haramaya university
 
Searching techniques
Searching techniquesSearching techniques
Searching techniques
Archana Burujwale
 
4.1 sequentioal search
4.1 sequentioal search4.1 sequentioal search
4.1 sequentioal search
Krish_ver2
 
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
 
Data Structures_ Sorting & Searching
Data Structures_ Sorting & SearchingData Structures_ Sorting & Searching
Data Structures_ Sorting & Searching
ThenmozhiK5
 
DS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptxDS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptx
prakashvs7
 
Presentation on Searching and Sorting in C language.pptx
Presentation on Searching and Sorting in C language.pptxPresentation on Searching and Sorting in C language.pptx
Presentation on Searching and Sorting in C language.pptx
Krishnanandmishra15
 
Data structure Unit - II Searching and Sorting.pptx
Data structure Unit - II Searching and Sorting.pptxData structure Unit - II Searching and Sorting.pptx
Data structure Unit - II Searching and Sorting.pptx
gavanisanjana
 
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
03446940736
 
Searching in c language
Searching in c languageSearching in c language
Searching in c language
CHANDAN KUMAR
 
Searching Algorithm, Linear search algorithm and their time complexity
Searching Algorithm, Linear search algorithm and their time complexitySearching Algorithm, Linear search algorithm and their time complexity
Searching Algorithm, Linear search algorithm and their time complexity
hamzamunawarkhan
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
haramaya university
 
4.1 sequentioal search
4.1 sequentioal search4.1 sequentioal search
4.1 sequentioal search
Krish_ver2
 
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
 
Ad

More from SSN College of Engineering, Kalavakkam (20)

ECG
ECG ECG
ECG
SSN College of Engineering, Kalavakkam
 
Localization, Classification, and Evaluation.pdf
Localization, Classification, and Evaluation.pdfLocalization, Classification, and Evaluation.pdf
Localization, Classification, and Evaluation.pdf
SSN College of Engineering, Kalavakkam
 
ADBMS 3a
ADBMS   3aADBMS   3a
ADBMS 3a
SSN College of Engineering, Kalavakkam
 
Exercise 5
Exercise   5Exercise   5
Exercise 5
SSN College of Engineering, Kalavakkam
 
ADBMS Unit-II c
ADBMS Unit-II cADBMS Unit-II c
ADBMS Unit-II c
SSN College of Engineering, Kalavakkam
 
ADBMS Unit-II b
ADBMS Unit-II bADBMS Unit-II b
ADBMS Unit-II b
SSN College of Engineering, Kalavakkam
 
Database Management System - 2a
Database Management System - 2aDatabase Management System - 2a
Database Management System - 2a
SSN College of Engineering, Kalavakkam
 
Database Management System
Database Management SystemDatabase Management System
Database Management System
SSN College of Engineering, Kalavakkam
 
Unit III - Inventory Problems
Unit III - Inventory ProblemsUnit III - Inventory Problems
Unit III - Inventory Problems
SSN College of Engineering, Kalavakkam
 
Unit II B - Game Theory
Unit II B - Game TheoryUnit II B - Game Theory
Unit II B - Game Theory
SSN College of Engineering, Kalavakkam
 
Unit II A - Game Theory
Unit II A - Game TheoryUnit II A - Game Theory
Unit II A - Game Theory
SSN College of Engineering, Kalavakkam
 
Unit V - Queuing Theory
Unit V - Queuing TheoryUnit V - Queuing Theory
Unit V - Queuing Theory
SSN College of Engineering, Kalavakkam
 
Unit IV-Project Management
Unit IV-Project ManagementUnit IV-Project Management
Unit IV-Project Management
SSN College of Engineering, Kalavakkam
 
Unit I-B
Unit I-BUnit I-B
Unit I-B
SSN College of Engineering, Kalavakkam
 
Unit I-A
Unit I-AUnit I-A
Unit I-A
SSN College of Engineering, Kalavakkam
 
Web technology Unit-II Part-C
Web technology Unit-II Part-CWeb technology Unit-II Part-C
Web technology Unit-II Part-C
SSN College of Engineering, Kalavakkam
 
Data structure Unit-I Part-C
Data structure Unit-I Part-CData structure Unit-I Part-C
Data structure Unit-I Part-C
SSN College of Engineering, Kalavakkam
 
Web technology Unit-II Part A
Web technology Unit-II Part AWeb technology Unit-II Part A
Web technology Unit-II Part A
SSN College of Engineering, Kalavakkam
 
Data structure Unit-I Part A
Data structure Unit-I Part AData structure Unit-I Part A
Data structure Unit-I Part A
SSN College of Engineering, Kalavakkam
 
Web technology Unit-I Part E
Web technology Unit-I   Part EWeb technology Unit-I   Part E
Web technology Unit-I Part E
SSN College of Engineering, Kalavakkam
 
Ad

Recently uploaded (20)

Mobile Application Developer Dubai | Custom App Solutions by Ajath
Mobile Application Developer Dubai | Custom App Solutions by AjathMobile Application Developer Dubai | Custom App Solutions by Ajath
Mobile Application Developer Dubai | Custom App Solutions by Ajath
Ajath Infotech Technologies LLC
 
wAIred_LearnWithOutAI_JCON_14052025.pptx
wAIred_LearnWithOutAI_JCON_14052025.pptxwAIred_LearnWithOutAI_JCON_14052025.pptx
wAIred_LearnWithOutAI_JCON_14052025.pptx
SimonedeGijt
 
Solar-wind hybrid engery a system sustainable power
Solar-wind  hybrid engery a system sustainable powerSolar-wind  hybrid engery a system sustainable power
Solar-wind hybrid engery a system sustainable power
bhoomigowda12345
 
Digital Twins Software Service in Belfast
Digital Twins Software Service in BelfastDigital Twins Software Service in Belfast
Digital Twins Software Service in Belfast
julia smits
 
Sequence Diagrams With Pictures (1).pptx
Sequence Diagrams With Pictures (1).pptxSequence Diagrams With Pictures (1).pptx
Sequence Diagrams With Pictures (1).pptx
aashrithakondapalli8
 
[gbgcpp] Let's get comfortable with concepts
[gbgcpp] Let's get comfortable with concepts[gbgcpp] Let's get comfortable with concepts
[gbgcpp] Let's get comfortable with concepts
Dimitrios Platis
 
Orion Context Broker introduction 20250509
Orion Context Broker introduction 20250509Orion Context Broker introduction 20250509
Orion Context Broker introduction 20250509
Fermin Galan
 
The-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptx
The-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptxThe-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptx
The-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptx
james brownuae
 
Robotic Process Automation (RPA) Software Development Services.pptx
Robotic Process Automation (RPA) Software Development Services.pptxRobotic Process Automation (RPA) Software Development Services.pptx
Robotic Process Automation (RPA) Software Development Services.pptx
julia smits
 
Passive House Canada Conference 2025 Presentation [Final]_v4.ppt
Passive House Canada Conference 2025 Presentation [Final]_v4.pptPassive House Canada Conference 2025 Presentation [Final]_v4.ppt
Passive House Canada Conference 2025 Presentation [Final]_v4.ppt
IES VE
 
sequencediagrams.pptx software Engineering
sequencediagrams.pptx software Engineeringsequencediagrams.pptx software Engineering
sequencediagrams.pptx software Engineering
aashrithakondapalli8
 
Wilcom Embroidery Studio Crack 2025 For Windows
Wilcom Embroidery Studio Crack 2025 For WindowsWilcom Embroidery Studio Crack 2025 For Windows
Wilcom Embroidery Studio Crack 2025 For Windows
Google
 
Gojek Clone App for Multi-Service Business
Gojek Clone App for Multi-Service BusinessGojek Clone App for Multi-Service Business
Gojek Clone App for Multi-Service Business
XongoLab Technologies LLP
 
What Do Candidates Really Think About AI-Powered Recruitment Tools?
What Do Candidates Really Think About AI-Powered Recruitment Tools?What Do Candidates Really Think About AI-Powered Recruitment Tools?
What Do Candidates Really Think About AI-Powered Recruitment Tools?
HireME
 
Adobe InDesign Crack FREE Download 2025 link
Adobe InDesign Crack FREE Download 2025 linkAdobe InDesign Crack FREE Download 2025 link
Adobe InDesign Crack FREE Download 2025 link
mahmadzubair09
 
How to Troubleshoot 9 Types of OutOfMemoryError
How to Troubleshoot 9 Types of OutOfMemoryErrorHow to Troubleshoot 9 Types of OutOfMemoryError
How to Troubleshoot 9 Types of OutOfMemoryError
Tier1 app
 
From Vibe Coding to Vibe Testing - Complete PowerPoint Presentation
From Vibe Coding to Vibe Testing - Complete PowerPoint PresentationFrom Vibe Coding to Vibe Testing - Complete PowerPoint Presentation
From Vibe Coding to Vibe Testing - Complete PowerPoint Presentation
Shay Ginsbourg
 
Troubleshooting JVM Outages – 3 Fortune 500 case studies
Troubleshooting JVM Outages – 3 Fortune 500 case studiesTroubleshooting JVM Outages – 3 Fortune 500 case studies
Troubleshooting JVM Outages – 3 Fortune 500 case studies
Tier1 app
 
AEM User Group DACH - 2025 Inaugural Meeting
AEM User Group DACH - 2025 Inaugural MeetingAEM User Group DACH - 2025 Inaugural Meeting
AEM User Group DACH - 2025 Inaugural Meeting
jennaf3
 
Time Estimation: Expert Tips & Proven Project Techniques
Time Estimation: Expert Tips & Proven Project TechniquesTime Estimation: Expert Tips & Proven Project Techniques
Time Estimation: Expert Tips & Proven Project Techniques
Livetecs LLC
 
Mobile Application Developer Dubai | Custom App Solutions by Ajath
Mobile Application Developer Dubai | Custom App Solutions by AjathMobile Application Developer Dubai | Custom App Solutions by Ajath
Mobile Application Developer Dubai | Custom App Solutions by Ajath
Ajath Infotech Technologies LLC
 
wAIred_LearnWithOutAI_JCON_14052025.pptx
wAIred_LearnWithOutAI_JCON_14052025.pptxwAIred_LearnWithOutAI_JCON_14052025.pptx
wAIred_LearnWithOutAI_JCON_14052025.pptx
SimonedeGijt
 
Solar-wind hybrid engery a system sustainable power
Solar-wind  hybrid engery a system sustainable powerSolar-wind  hybrid engery a system sustainable power
Solar-wind hybrid engery a system sustainable power
bhoomigowda12345
 
Digital Twins Software Service in Belfast
Digital Twins Software Service in BelfastDigital Twins Software Service in Belfast
Digital Twins Software Service in Belfast
julia smits
 
Sequence Diagrams With Pictures (1).pptx
Sequence Diagrams With Pictures (1).pptxSequence Diagrams With Pictures (1).pptx
Sequence Diagrams With Pictures (1).pptx
aashrithakondapalli8
 
[gbgcpp] Let's get comfortable with concepts
[gbgcpp] Let's get comfortable with concepts[gbgcpp] Let's get comfortable with concepts
[gbgcpp] Let's get comfortable with concepts
Dimitrios Platis
 
Orion Context Broker introduction 20250509
Orion Context Broker introduction 20250509Orion Context Broker introduction 20250509
Orion Context Broker introduction 20250509
Fermin Galan
 
The-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptx
The-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptxThe-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptx
The-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptx
james brownuae
 
Robotic Process Automation (RPA) Software Development Services.pptx
Robotic Process Automation (RPA) Software Development Services.pptxRobotic Process Automation (RPA) Software Development Services.pptx
Robotic Process Automation (RPA) Software Development Services.pptx
julia smits
 
Passive House Canada Conference 2025 Presentation [Final]_v4.ppt
Passive House Canada Conference 2025 Presentation [Final]_v4.pptPassive House Canada Conference 2025 Presentation [Final]_v4.ppt
Passive House Canada Conference 2025 Presentation [Final]_v4.ppt
IES VE
 
sequencediagrams.pptx software Engineering
sequencediagrams.pptx software Engineeringsequencediagrams.pptx software Engineering
sequencediagrams.pptx software Engineering
aashrithakondapalli8
 
Wilcom Embroidery Studio Crack 2025 For Windows
Wilcom Embroidery Studio Crack 2025 For WindowsWilcom Embroidery Studio Crack 2025 For Windows
Wilcom Embroidery Studio Crack 2025 For Windows
Google
 
What Do Candidates Really Think About AI-Powered Recruitment Tools?
What Do Candidates Really Think About AI-Powered Recruitment Tools?What Do Candidates Really Think About AI-Powered Recruitment Tools?
What Do Candidates Really Think About AI-Powered Recruitment Tools?
HireME
 
Adobe InDesign Crack FREE Download 2025 link
Adobe InDesign Crack FREE Download 2025 linkAdobe InDesign Crack FREE Download 2025 link
Adobe InDesign Crack FREE Download 2025 link
mahmadzubair09
 
How to Troubleshoot 9 Types of OutOfMemoryError
How to Troubleshoot 9 Types of OutOfMemoryErrorHow to Troubleshoot 9 Types of OutOfMemoryError
How to Troubleshoot 9 Types of OutOfMemoryError
Tier1 app
 
From Vibe Coding to Vibe Testing - Complete PowerPoint Presentation
From Vibe Coding to Vibe Testing - Complete PowerPoint PresentationFrom Vibe Coding to Vibe Testing - Complete PowerPoint Presentation
From Vibe Coding to Vibe Testing - Complete PowerPoint Presentation
Shay Ginsbourg
 
Troubleshooting JVM Outages – 3 Fortune 500 case studies
Troubleshooting JVM Outages – 3 Fortune 500 case studiesTroubleshooting JVM Outages – 3 Fortune 500 case studies
Troubleshooting JVM Outages – 3 Fortune 500 case studies
Tier1 app
 
AEM User Group DACH - 2025 Inaugural Meeting
AEM User Group DACH - 2025 Inaugural MeetingAEM User Group DACH - 2025 Inaugural Meeting
AEM User Group DACH - 2025 Inaugural Meeting
jennaf3
 
Time Estimation: Expert Tips & Proven Project Techniques
Time Estimation: Expert Tips & Proven Project TechniquesTime Estimation: Expert Tips & Proven Project Techniques
Time Estimation: Expert Tips & Proven Project Techniques
Livetecs LLC
 

Data structure unit I part B

  • 2. Searching  It is a method to find a element or data. Searching Types Linear Search Binary Search
  • 3. List search  The algorithm used to search a list depends to a large extent on the structure of the list.  The two basic searches for arrays are the sequential or linear search and binary search.  Sequential or Linear Search  It can be used to locate an item in any array.  Binary Search  It requires on ordered list.
  • 4. Sequential or Linear Search:  Search in sequential order.  It is used whether the list is not ordered.  Example:  It is used only for small lists or lists that aren’t searched often.  We start searching for the target at the beginning of the list and continue until we find the target.  Termination Condition  Target is found (Success)  List of elements is exhausted or Target is not found (failure)
  • 6. Algorithm  Step 1: The list we are searching  Step 2: An index to the last element in the list.  Step 3: The target  Step 4: The address where the found element’s index location is to be stored.  Step 5: To tell calling algorithm whether data were located  Step 6: We return a Boolean - true for we found it or false for we didn’t found it.  Note: As an alternative to the index to the last element, the size of the list may be passed.
  • 9. Data Structures: A Pseudocode Approach with C, Second Edition 9 Successful search of an unordered list
  • 10. Data Structures: A Pseudocode Approach with C, Second Edition 10 Unsuccessful search in unordered list
  • 11. Data Structures: A Pseudocode Approach with C, Second Edition 11 Sequential search algorithm
  • 12. Variations on Sequential Search  Three useful variations on the sequential search algorithm:  The sentinel search  The probability search  The ordered list search
  • 13. Sentinel Search  Loop tests two conditions:  End of list  Target not found. Knuth states that, “When the inner loop of a program tests two or more conditions, an attempt should be made to reduce it to just one condition”  The only way we can ensure that a target is actually in the list is to put it there our self.  A target is put in the list by adding an extra element (sentinel entry) at the end of the array and placing the target in the sentinel.  We can then optimize the loop and determine after the loop completes whether we found actual data or the sentinel.  Disadvantage:  The rest of the processing must be careful to never look at the sentinel element at the end of the list.
  • 15. Probability Search  The array is ordered with the most probable search elements at the beginning of the array and the least probable search elements at the end.  It is especially useful when relatively few elements are the targets for most of the searches.  To ensure that the probability ordering is correct over time, in each search we exchange the located element with the element immediately before it in the array.
  • 17. Ordered List Search  Although we generally recommend a binary search when searching a list ordered on the key (target), if the list is small it may be more efficient to use a sequential search.  When searching an ordered list sequentially, however, it is not necessary to search to the end of the list to determine that the target is not in the list.  We can stop when the target becomes less than or equal to the current element we are testing.  In addition, we can incorporate the sentinel concept by bypassing the search loop when the target is greater than the last item.  In other words, when the target is less than or equal to the last element, the last element becomes a sentinel, allowing us to eliminate the test for the end of the list.  Although it can be used with array implementations, the ordered list search is more commonly used when searching linked list implementations.
  • 19. Binary Search  The sequential search algorithm is very slow.  If we have an array of 1000 elements, we must make 1000 comparisons in the worst case.  If the array is not sorted, the sequential search is the only solution.  It the array is sorted, we can use a more efficient algorithm called binary search.  The binary search starts by testing the data in the element at the middle of the array.  To determine if the target is in the first or the second half of the list.  mid = (begin + end) / 2  If it is in the first half, we do not need to check the second half. If it is in the second half, we do not need to test the first half.
  • 20.  To find the middle of the list, we need three variables:  Step 1: One to identify the beginning of the list  Step 2: One to identify the middle of the list  Step 3: One to identify the end of the list.  We analyze two cases here:  The target is in the list  The target is not in the list.
  • 22. Data Structures: A Pseudocode Approach with C, Second Edition 22 Binary search example             2 2 lastfirst mid endbegin mid
  • 24. Data Structures: A Pseudocode Approach with C, Second Edition 24 Figure 2-5 Unsuccessful binary search example
  • 25. Data Structures: A Pseudocode Approach with C, Second Edition 25
  • 26. Data Structures: A Pseudocode Approach with C, Second Edition 26 (continued)
  • 27. Data Structures: A Pseudocode Approach with C, Second Edition 27 Analyzing search algorithms The efficiency of the sequential search is O(n). The efficiency of the binary search is O(log n). List size Iterations binary Sequential (Average) Sequential (worst case) 16 4 8 16 50 6 25 50 256 8 128 256 1000 10 500 1000 10000 14 5000 10000 100000 17 50000 100000 1000000 20 500000 1000000
  • 28.  a) For Sequential Search The basic loop for the sequential search is shown below.  The efficiency of the sequential search is O(n).  The search efficiency for the sentinel search is basically the same as for the sequential search.  Although the sentinel search saves a few instructions in the loop, its design is identical.  Therefore, it is also an O(n).  b) The binary search locates an item by repeatedly dividing the list in half.  Its loop is:  This loop obviously divides, and it is therefore a logarithmic loop.  The efficiency is the binary search is O(log n).
  翻译: