SlideShare a Scribd company logo
1 of 28
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)
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 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.
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.
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.
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 Structures: A
Pseudocode
Approach with C,
Second Edition
22
Binary
search
example



 




 

2
2
lastfirst
mid
endbegin
mid
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).

More Related Content

What's hot

computer notes - Data Structures - 33
computer notes - Data Structures - 33computer notes - Data Structures - 33
computer notes - Data Structures - 33ecomputernotes
 
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 answersbestwriter
 
Quick sort data structures
Quick sort data structuresQuick sort data structures
Quick sort data structureschauhankapil
 
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 listsaiclub_slides
 

What's hot (11)

computer notes - Data Structures - 33
computer notes - Data Structures - 33computer notes - Data Structures - 33
computer notes - Data Structures - 33
 
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
 
Lists methods
Lists methodsLists methods
Lists methods
 
Lists and loops
Lists and loopsLists and loops
Lists and loops
 
Quick sort data structures
Quick sort data structuresQuick sort data structures
Quick sort data structures
 
Python list functions
Python list functionsPython list functions
Python list functions
 
Link list assi
Link list assiLink list assi
Link list assi
 
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....
 
Introduction to lists
Introduction to listsIntroduction to lists
Introduction to lists
 
Dictionaries
DictionariesDictionaries
Dictionaries
 
Linked lists
Linked listsLinked lists
Linked lists
 

Similar to Data structure unit I part B

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
 
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.pptxKrishnanandmishra15
 
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
 
Searching in c language
Searching in c languageSearching in c language
Searching in c languageCHANDAN KUMAR
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdfharamaya university
 
4.1 sequentioal search
4.1 sequentioal search4.1 sequentioal search
4.1 sequentioal searchKrish_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).pptxAftabali702240
 
Searching Sorting
Searching SortingSearching Sorting
Searching Sortingguest2cb109
 
Dsa – data structure and algorithms searching
Dsa – data structure and algorithms   searchingDsa – data structure and algorithms   searching
Dsa – data structure and algorithms searchingsajinis3
 

Similar to Data structure unit I part B (20)

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
 
Analysis of Algorithm - Binary Search.pptx
Analysis of Algorithm - Binary Search.pptxAnalysis of Algorithm - Binary Search.pptx
Analysis of Algorithm - Binary Search.pptx
 
Searching_Sorting.pptx
Searching_Sorting.pptxSearching_Sorting.pptx
Searching_Sorting.pptx
 
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
 
arrays in c
arrays in carrays in c
arrays in c
 
Ch05 Black Jack
Ch05  Black  JackCh05  Black  Jack
Ch05 Black Jack
 
searching
searchingsearching
searching
 
Linear search-and-binary-search
Linear search-and-binary-searchLinear search-and-binary-search
Linear search-and-binary-search
 
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
 
Searching algorithms
Searching algorithmsSearching algorithms
Searching algorithms
 
seaching internal 2 ppt
seaching internal 2 pptseaching internal 2 ppt
seaching internal 2 ppt
 
Searching algorithms
Searching algorithmsSearching algorithms
Searching algorithms
 
Searching in c language
Searching in c languageSearching in c language
Searching in c language
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
 
Searching techniques
Searching techniquesSearching techniques
Searching techniques
 
4.1 sequentioal search
4.1 sequentioal search4.1 sequentioal search
4.1 sequentioal search
 
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
 
Searching Sorting
Searching SortingSearching Sorting
Searching Sorting
 
Dsa – data structure and algorithms searching
Dsa – data structure and algorithms   searchingDsa – data structure and algorithms   searching
Dsa – data structure and algorithms searching
 

More from SSN College of Engineering, Kalavakkam

More from SSN College of Engineering, Kalavakkam (20)

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

Recently uploaded

MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
software engineering Chapter 5 System modeling.pptx
software engineering Chapter 5 System modeling.pptxsoftware engineering Chapter 5 System modeling.pptx
software engineering Chapter 5 System modeling.pptxnada99848
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfPower Karaoke
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 

Recently uploaded (20)

MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
software engineering Chapter 5 System modeling.pptx
software engineering Chapter 5 System modeling.pptxsoftware engineering Chapter 5 System modeling.pptx
software engineering Chapter 5 System modeling.pptx
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdf
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 

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)
  • 5.
  • 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.
  • 7.
  • 8.
  • 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.
  • 14.
  • 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.
  • 16.
  • 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.
  • 18.
  • 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.
  • 21.
  • 22. Data Structures: A Pseudocode Approach with C, Second Edition 22 Binary search example             2 2 lastfirst mid endbegin mid
  • 23.
  • 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).