際際滷

際際滷Share a Scribd company logo
Chapter 6Chapter 6
searChingsearChing
01/31/18 BY MS. SHAISTA QADIR 1
PRESENTED BY
Shaista Qadir
Lecturer king khalid university
COntentsCOntents
 searChing
 seQUentiaL searCh
 exampLe
 aLgOrithm
 prOgram LOgiC
 BinarY searCh
 exampLe
 aLgOrithm
 prOgram LOgiC
01/31/18 BY MS. SHAISTA QADIR 2
searChingsearChing
01/31/18 BY MS. SHAISTA QADIR 3
 Computer systems are often used to store large amounts of
data.
 Sometimes data must be retrieved from storage based on a
searching criteria.
 Efficient storage of data to facilitate fast searching.
seQUentiaL searChseQUentiaL searCh
01/31/18 BY MS. SHAISTA QADIR 4
 The sequential search (also called the Linear Search) is the
simplest search algorithm.
 It is also the least efficient.
 It simply examines each element sequentially, starting with
the first element, until it finds the
 key element or it reaches the end of the array.
 Application: To search a person in the train
SEQUENTIAL SEARCH ExAmpLESEQUENTIAL SEARCH ExAmpLE
01/31/18 BY MS. SHAISTA QADIR 5
 Example:
 Consider an array with the following numbers:
SEQUENTIAL SEARCHSEQUENTIAL SEARCH
ALGORITHmALGORITHm
01/31/18 BY MS. SHAISTA QADIR 6
 ALGORITHM:
(Postcondition: either the index i is returned where si = x,
or 1 is returned.)
1. Repeat steps 23, for i = 0 to n  1.
2. If si = x, return i .
3. Return 1.
SEQUENTIAL SEARCH pROGRAmSEQUENTIAL SEARCH pROGRAm
LOGICLOGIC
01/31/18 BY MS. SHAISTA QADIR 7
 PROGRAM LOGIC:
public int seqsearch(int [ ]arr, int x)
{
for(int i=0; i< arr.length; i++)
if(arr [ i ] == x)
return i;
return -1;
}
 Time Complexity of Sequential search algorithm is: O(n)3.
BINARY SEARCHBINARY SEARCH
01/31/18 BY MS. SHAISTA QADIR 8
 The binary search is the standard algorithm for searching
through a sorted sequence.
 It is much more efficient than the sequential search.
 It repeatedly divides the sequence in two, each time
restricting the search to the half that would contain the
element
 Application: Binary search is used to search a word in a
dictionary.
BINARY SEARCH ExAmplEBINARY SEARCH ExAmplE
01/31/18 BY MS. SHAISTA QADIR 9
 Example: Condition : The array must be sorted.
 Consider an array with the following numbers:
 Divide-and-Conquer Strategy
 Search for the number 16.
 Calculate: mid=(First + last)/2
BINARY SEARCH ExAmplEBINARY SEARCH ExAmplE
01/31/18 BY MS. SHAISTA QADIR 10
 Example: Condition : The array must be sorted.
 Return the position of 16 which is 3
BINARY SEARCH AlGORITHmBINARY SEARCH AlGORITHm
01/31/18 BY MS. SHAISTA QADIR 11
 ALGORITHM:
 (Precondition:1. s={s0, s1, . . ., sn1} is a sorted sequence of
n values of the same type as x.either the index i is returned
where si = x, or 1 is returned.)
1. Let ss be a subsequence of the sequence s, initially set
equal to s.
2. If the subsequence ss is empty, return 1.
3. Let si be the middle element of ss.
4. If si = x, return its index i .
5. If si < x, repeat steps 26on the subsequence that lies
above si.
6. Repeat st
BINARY SEARCH pROGRAmBINARY SEARCH pROGRAm
lOGIClOGIC
01/31/18 BY MS. SHAISTA QADIR 12
 PROGRAM LOGIC:
public static int binarysearch(int[ ] a, int x) {
int lo = 0; int hi = a.length;
while (lo < hi) {
int i = (lo + hi) / 2;
if (a[i] == x)
return i;
else if (a[i] < x)
lo = i+1;
else hi = I; }
return -1; }
 Time Complexity of binary search algorithm is: O(logn)
01/31/18 BY MS. SHAISTA QADIR 13
THANK YOUTHANK YOU
Ad

Recommended

Secondary Index Search in InnoDB
Secondary Index Search in InnoDB
MIJIN AN
4 heapsort pq
4 heapsort pq
hasan Mohammad
Java - Collections
Java - Collections
Amith jayasekara
Deadlock Prevention in Operating System
Deadlock Prevention in Operating System
Zeeshan Iqbal
Big Data & Hadoop Data Analysis
Big Data & Hadoop Data Analysis
Koushik Mondal
A Survey of Sequential Rule Mining Techniques
A Survey of Sequential Rule Mining Techniques
ijsrd.com
Ds chapter 2
Ds chapter 2
Prof .Pragati Khade
Heap Sort in Design and Analysis of algorithms
Heap Sort in Design and Analysis of algorithms
samairaakram
A novel algorithm for mining closed sequential patterns
A novel algorithm for mining closed sequential patterns
IJDKP
Fast Sequential Rule Mining
Fast Sequential Rule Mining
ijsrd.com
Linear search-and-binary-search
Linear search-and-binary-search
International Islamic University
IRJET- A Survey on Different Searching Algorithms
IRJET- A Survey on Different Searching Algorithms
IRJET Journal
Graph based Approach and Clustering of Patterns (GACP) for Sequential Pattern...
Graph based Approach and Clustering of Patterns (GACP) for Sequential Pattern...
AshishDPatel1
Mining Top-k Closed Sequential Patterns in Sequential Databases
Mining Top-k Closed Sequential Patterns in Sequential Databases
IOSR Journals
Mining closed sequential patterns in large sequence databases
Mining closed sequential patterns in large sequence databases
IJDMS
Aslam
Aslam
Muhammad Aslam
A NEW EXTRACTION OPTIMIZATION APPROACH TO FREQUENT 2 ITEMSETS
A NEW EXTRACTION OPTIMIZATION APPROACH TO FREQUENT 2 ITEMSETS
ijcsa
A NEW EXTRACTION OPTIMIZATION APPROACH TO FREQUENT 2 ITEMSETS
A NEW EXTRACTION OPTIMIZATION APPROACH TO FREQUENT 2 ITEMSETS
ijcsa
A New Extraction Optimization Approach to Frequent 2 Item sets
A New Extraction Optimization Approach to Frequent 2 Item sets
ijcsa
Review Over Sequential Rule Mining
Review Over Sequential Rule Mining
ijsrd.com
C# Arrays Presentation - Computer Science and Engineering Department
C# Arrays Presentation - Computer Science and Engineering Department
BhuvaneswaranB1
Algorithm 8th lecture linear & binary search(2).pptx
Algorithm 8th lecture linear & binary search(2).pptx
Aftabali702240
Sequential Pattern Mining Methods: A Snap Shot
Sequential Pattern Mining Methods: A Snap Shot
IOSR Journals
Clustering and Visualisation using R programming
Clustering and Visualisation using R programming
Nixon Mendez
Chapter three data structure and algorithms qaybta quee
Chapter three data structure and algorithms qaybta quee
habdi203062
Sequential Pattern Tree Mining
Sequential Pattern Tree Mining
IOSR Journals
Algorithm - Mergesort & Quicksort
Algorithm - Mergesort & Quicksort
Varendra University Rajshahi-bangladesh
A survey paper on sequence pattern mining with incremental
A survey paper on sequence pattern mining with incremental
Alexander Decker
Chapter - 2 introduction to Computer Organization.pdf
Chapter - 2 introduction to Computer Organization.pdf
Shaista Qadir
Chapter - 1 Introduction to Computer Science.pdf
Chapter - 1 Introduction to Computer Science.pdf
Shaista Qadir

More Related Content

Similar to Searching (20)

A novel algorithm for mining closed sequential patterns
A novel algorithm for mining closed sequential patterns
IJDKP
Fast Sequential Rule Mining
Fast Sequential Rule Mining
ijsrd.com
Linear search-and-binary-search
Linear search-and-binary-search
International Islamic University
IRJET- A Survey on Different Searching Algorithms
IRJET- A Survey on Different Searching Algorithms
IRJET Journal
Graph based Approach and Clustering of Patterns (GACP) for Sequential Pattern...
Graph based Approach and Clustering of Patterns (GACP) for Sequential Pattern...
AshishDPatel1
Mining Top-k Closed Sequential Patterns in Sequential Databases
Mining Top-k Closed Sequential Patterns in Sequential Databases
IOSR Journals
Mining closed sequential patterns in large sequence databases
Mining closed sequential patterns in large sequence databases
IJDMS
Aslam
Aslam
Muhammad Aslam
A NEW EXTRACTION OPTIMIZATION APPROACH TO FREQUENT 2 ITEMSETS
A NEW EXTRACTION OPTIMIZATION APPROACH TO FREQUENT 2 ITEMSETS
ijcsa
A NEW EXTRACTION OPTIMIZATION APPROACH TO FREQUENT 2 ITEMSETS
A NEW EXTRACTION OPTIMIZATION APPROACH TO FREQUENT 2 ITEMSETS
ijcsa
A New Extraction Optimization Approach to Frequent 2 Item sets
A New Extraction Optimization Approach to Frequent 2 Item sets
ijcsa
Review Over Sequential Rule Mining
Review Over Sequential Rule Mining
ijsrd.com
C# Arrays Presentation - Computer Science and Engineering Department
C# Arrays Presentation - Computer Science and Engineering Department
BhuvaneswaranB1
Algorithm 8th lecture linear & binary search(2).pptx
Algorithm 8th lecture linear & binary search(2).pptx
Aftabali702240
Sequential Pattern Mining Methods: A Snap Shot
Sequential Pattern Mining Methods: A Snap Shot
IOSR Journals
Clustering and Visualisation using R programming
Clustering and Visualisation using R programming
Nixon Mendez
Chapter three data structure and algorithms qaybta quee
Chapter three data structure and algorithms qaybta quee
habdi203062
Sequential Pattern Tree Mining
Sequential Pattern Tree Mining
IOSR Journals
Algorithm - Mergesort & Quicksort
Algorithm - Mergesort & Quicksort
Varendra University Rajshahi-bangladesh
A survey paper on sequence pattern mining with incremental
A survey paper on sequence pattern mining with incremental
Alexander Decker
A novel algorithm for mining closed sequential patterns
A novel algorithm for mining closed sequential patterns
IJDKP
Fast Sequential Rule Mining
Fast Sequential Rule Mining
ijsrd.com
IRJET- A Survey on Different Searching Algorithms
IRJET- A Survey on Different Searching Algorithms
IRJET Journal
Graph based Approach and Clustering of Patterns (GACP) for Sequential Pattern...
Graph based Approach and Clustering of Patterns (GACP) for Sequential Pattern...
AshishDPatel1
Mining Top-k Closed Sequential Patterns in Sequential Databases
Mining Top-k Closed Sequential Patterns in Sequential Databases
IOSR Journals
Mining closed sequential patterns in large sequence databases
Mining closed sequential patterns in large sequence databases
IJDMS
A NEW EXTRACTION OPTIMIZATION APPROACH TO FREQUENT 2 ITEMSETS
A NEW EXTRACTION OPTIMIZATION APPROACH TO FREQUENT 2 ITEMSETS
ijcsa
A NEW EXTRACTION OPTIMIZATION APPROACH TO FREQUENT 2 ITEMSETS
A NEW EXTRACTION OPTIMIZATION APPROACH TO FREQUENT 2 ITEMSETS
ijcsa
A New Extraction Optimization Approach to Frequent 2 Item sets
A New Extraction Optimization Approach to Frequent 2 Item sets
ijcsa
Review Over Sequential Rule Mining
Review Over Sequential Rule Mining
ijsrd.com
C# Arrays Presentation - Computer Science and Engineering Department
C# Arrays Presentation - Computer Science and Engineering Department
BhuvaneswaranB1
Algorithm 8th lecture linear & binary search(2).pptx
Algorithm 8th lecture linear & binary search(2).pptx
Aftabali702240
Sequential Pattern Mining Methods: A Snap Shot
Sequential Pattern Mining Methods: A Snap Shot
IOSR Journals
Clustering and Visualisation using R programming
Clustering and Visualisation using R programming
Nixon Mendez
Chapter three data structure and algorithms qaybta quee
Chapter three data structure and algorithms qaybta quee
habdi203062
Sequential Pattern Tree Mining
Sequential Pattern Tree Mining
IOSR Journals
A survey paper on sequence pattern mining with incremental
A survey paper on sequence pattern mining with incremental
Alexander Decker

More from Shaista Qadir (8)

Chapter - 2 introduction to Computer Organization.pdf
Chapter - 2 introduction to Computer Organization.pdf
Shaista Qadir
Chapter - 1 Introduction to Computer Science.pdf
Chapter - 1 Introduction to Computer Science.pdf
Shaista Qadir
Lecture 4
Lecture 4
Shaista Qadir
Lecture 1
Lecture 1
Shaista Qadir
Sorting
Sorting
Shaista Qadir
Queue
Queue
Shaista Qadir
linked list
linked list
Shaista Qadir
Complexity Analysis
Complexity Analysis
Shaista Qadir
Chapter - 2 introduction to Computer Organization.pdf
Chapter - 2 introduction to Computer Organization.pdf
Shaista Qadir
Chapter - 1 Introduction to Computer Science.pdf
Chapter - 1 Introduction to Computer Science.pdf
Shaista Qadir
Complexity Analysis
Complexity Analysis
Shaista Qadir
Ad

Recently uploaded (20)

Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
caoyixuan2019
2025_06_18 - OpenMetadata Community Meeting.pdf
2025_06_18 - OpenMetadata Community Meeting.pdf
OpenMetadata
Smarter Aviation Data Management: Lessons from Swedavia Airports and Sweco
Smarter Aviation Data Management: Lessons from Swedavia Airports and Sweco
Safe Software
You are not excused! How to avoid security blind spots on the way to production
You are not excused! How to avoid security blind spots on the way to production
Michele Leroux Bustamante
cnc-processing-centers-centateq-p-110-en.pdf
cnc-processing-centers-centateq-p-110-en.pdf
AmirStern2
Wenn alles versagt - IBM Tape sch端tzt, was z辰hlt! Und besonders mit dem neust...
Wenn alles versagt - IBM Tape sch端tzt, was z辰hlt! Und besonders mit dem neust...
Josef Weingand
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Alliance
Cyber Defense Matrix Workshop - RSA Conference
Cyber Defense Matrix Workshop - RSA Conference
Priyanka Aash
Coordinated Disclosure for ML - What's Different and What's the Same.pdf
Coordinated Disclosure for ML - What's Different and What's the Same.pdf
Priyanka Aash
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
biswajitbanerjee38
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
Fwdays
Securing AI - There Is No Try, Only Do!.pdf
Securing AI - There Is No Try, Only Do!.pdf
Priyanka Aash
Security Tips for Enterprise Azure Solutions
Security Tips for Enterprise Azure Solutions
Michele Leroux Bustamante
Securing Account Lifecycles in the Age of Deepfakes.pptx
Securing Account Lifecycles in the Age of Deepfakes.pptx
FIDO Alliance
Improving Data Integrity: Synchronization between EAM and ArcGIS Utility Netw...
Improving Data Integrity: Synchronization between EAM and ArcGIS Utility Netw...
Safe Software
The Future of Technology: 2025-2125 by Saikat Basu.pdf
The Future of Technology: 2025-2125 by Saikat Basu.pdf
Saikat Basu
PyCon SG 25 - Firecracker Made Easy with Python.pdf
PyCon SG 25 - Firecracker Made Easy with Python.pdf
Muhammad Yuga Nugraha
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Alliance
Lessons Learned from Developing Secure AI Workflows.pdf
Lessons Learned from Developing Secure AI Workflows.pdf
Priyanka Aash
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
caoyixuan2019
2025_06_18 - OpenMetadata Community Meeting.pdf
2025_06_18 - OpenMetadata Community Meeting.pdf
OpenMetadata
Smarter Aviation Data Management: Lessons from Swedavia Airports and Sweco
Smarter Aviation Data Management: Lessons from Swedavia Airports and Sweco
Safe Software
You are not excused! How to avoid security blind spots on the way to production
You are not excused! How to avoid security blind spots on the way to production
Michele Leroux Bustamante
cnc-processing-centers-centateq-p-110-en.pdf
cnc-processing-centers-centateq-p-110-en.pdf
AmirStern2
Wenn alles versagt - IBM Tape sch端tzt, was z辰hlt! Und besonders mit dem neust...
Wenn alles versagt - IBM Tape sch端tzt, was z辰hlt! Und besonders mit dem neust...
Josef Weingand
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Alliance
Cyber Defense Matrix Workshop - RSA Conference
Cyber Defense Matrix Workshop - RSA Conference
Priyanka Aash
Coordinated Disclosure for ML - What's Different and What's the Same.pdf
Coordinated Disclosure for ML - What's Different and What's the Same.pdf
Priyanka Aash
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
biswajitbanerjee38
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
Fwdays
Securing AI - There Is No Try, Only Do!.pdf
Securing AI - There Is No Try, Only Do!.pdf
Priyanka Aash
Security Tips for Enterprise Azure Solutions
Security Tips for Enterprise Azure Solutions
Michele Leroux Bustamante
Securing Account Lifecycles in the Age of Deepfakes.pptx
Securing Account Lifecycles in the Age of Deepfakes.pptx
FIDO Alliance
Improving Data Integrity: Synchronization between EAM and ArcGIS Utility Netw...
Improving Data Integrity: Synchronization between EAM and ArcGIS Utility Netw...
Safe Software
The Future of Technology: 2025-2125 by Saikat Basu.pdf
The Future of Technology: 2025-2125 by Saikat Basu.pdf
Saikat Basu
PyCon SG 25 - Firecracker Made Easy with Python.pdf
PyCon SG 25 - Firecracker Made Easy with Python.pdf
Muhammad Yuga Nugraha
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Alliance
Lessons Learned from Developing Secure AI Workflows.pdf
Lessons Learned from Developing Secure AI Workflows.pdf
Priyanka Aash
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance
Ad

Searching

  • 1. Chapter 6Chapter 6 searChingsearChing 01/31/18 BY MS. SHAISTA QADIR 1 PRESENTED BY Shaista Qadir Lecturer king khalid university
  • 2. COntentsCOntents searChing seQUentiaL searCh exampLe aLgOrithm prOgram LOgiC BinarY searCh exampLe aLgOrithm prOgram LOgiC 01/31/18 BY MS. SHAISTA QADIR 2
  • 3. searChingsearChing 01/31/18 BY MS. SHAISTA QADIR 3 Computer systems are often used to store large amounts of data. Sometimes data must be retrieved from storage based on a searching criteria. Efficient storage of data to facilitate fast searching.
  • 4. seQUentiaL searChseQUentiaL searCh 01/31/18 BY MS. SHAISTA QADIR 4 The sequential search (also called the Linear Search) is the simplest search algorithm. It is also the least efficient. It simply examines each element sequentially, starting with the first element, until it finds the key element or it reaches the end of the array. Application: To search a person in the train
  • 5. SEQUENTIAL SEARCH ExAmpLESEQUENTIAL SEARCH ExAmpLE 01/31/18 BY MS. SHAISTA QADIR 5 Example: Consider an array with the following numbers:
  • 6. SEQUENTIAL SEARCHSEQUENTIAL SEARCH ALGORITHmALGORITHm 01/31/18 BY MS. SHAISTA QADIR 6 ALGORITHM: (Postcondition: either the index i is returned where si = x, or 1 is returned.) 1. Repeat steps 23, for i = 0 to n 1. 2. If si = x, return i . 3. Return 1.
  • 7. SEQUENTIAL SEARCH pROGRAmSEQUENTIAL SEARCH pROGRAm LOGICLOGIC 01/31/18 BY MS. SHAISTA QADIR 7 PROGRAM LOGIC: public int seqsearch(int [ ]arr, int x) { for(int i=0; i< arr.length; i++) if(arr [ i ] == x) return i; return -1; } Time Complexity of Sequential search algorithm is: O(n)3.
  • 8. BINARY SEARCHBINARY SEARCH 01/31/18 BY MS. SHAISTA QADIR 8 The binary search is the standard algorithm for searching through a sorted sequence. It is much more efficient than the sequential search. It repeatedly divides the sequence in two, each time restricting the search to the half that would contain the element Application: Binary search is used to search a word in a dictionary.
  • 9. BINARY SEARCH ExAmplEBINARY SEARCH ExAmplE 01/31/18 BY MS. SHAISTA QADIR 9 Example: Condition : The array must be sorted. Consider an array with the following numbers: Divide-and-Conquer Strategy Search for the number 16. Calculate: mid=(First + last)/2
  • 10. BINARY SEARCH ExAmplEBINARY SEARCH ExAmplE 01/31/18 BY MS. SHAISTA QADIR 10 Example: Condition : The array must be sorted. Return the position of 16 which is 3
  • 11. BINARY SEARCH AlGORITHmBINARY SEARCH AlGORITHm 01/31/18 BY MS. SHAISTA QADIR 11 ALGORITHM: (Precondition:1. s={s0, s1, . . ., sn1} is a sorted sequence of n values of the same type as x.either the index i is returned where si = x, or 1 is returned.) 1. Let ss be a subsequence of the sequence s, initially set equal to s. 2. If the subsequence ss is empty, return 1. 3. Let si be the middle element of ss. 4. If si = x, return its index i . 5. If si < x, repeat steps 26on the subsequence that lies above si. 6. Repeat st
  • 12. BINARY SEARCH pROGRAmBINARY SEARCH pROGRAm lOGIClOGIC 01/31/18 BY MS. SHAISTA QADIR 12 PROGRAM LOGIC: public static int binarysearch(int[ ] a, int x) { int lo = 0; int hi = a.length; while (lo < hi) { int i = (lo + hi) / 2; if (a[i] == x) return i; else if (a[i] < x) lo = i+1; else hi = I; } return -1; } Time Complexity of binary search algorithm is: O(logn)
  • 13. 01/31/18 BY MS. SHAISTA QADIR 13 THANK YOUTHANK YOU