SlideShare a Scribd company logo
Week 3c
Basic Searching Algorithms
DATASTRUCTURESANDALGORITHMS
INSTRUCTOR: SulamanAhmadNaz
1
Searching
■ Searching is the process of determining
whether or not a given value exists in a
data structure or a storage media.
■ We will discuss two searching methods on
1D (Linear) data structures:
– Linear search
– Binary search.
2
Linear Search
■ It is also called as Serial or Sequential
Search.
■ The linear (or sequential) search algorithm
on an array is:
– Sequentially scan the array, comparing each
array item with the searched value.
– If a match is found; return the index of the
matched element; otherwise return NULL.
■ Note: linear search can be applied to both
sorted and unsorted arrays.
3
Linear Search
■ Step through array of records, one at a
time.
■ Look for record with matching key.
■ Search stops when
– record with matching key is found
– or when search has examined all records
without success.
4
Linear Search
Linear Search ( Array A, KEY)
Step 1: Set i to L
Step 2: While i <= U do
Step 3: if A[i] = KEY then go to step 7
Step 4: Set i to i + 1
Step 5: End While
Step 6: Print “Element not found” & Exit
Step 7: Print “Element x Found at index i”
Step 8: Exit
5
Linear Search
procedure linear_search (Array, KEY)
For each item in the Array
If item == KEY
return the item's location
End If
End For
End procedure
6
Linear Search
Algorithm LinearSearch (Array A[L..U], KEY)
For i = L to U
If A[i] == KEY
return i
End If
End For
return NULL
7
Working
KEY=33
8
Working
KEY=33
9
Working
KEY=33
10
Working
KEY=33
11
Working
KEY=33
12
Working
KEY=33
13
Working
KEY=33
14
Working
KEY=33
15
Working
KEY=33
16
1 2 3 4 5 6 7 8 9 10
return 7
Analysis of Linear Search
Algorithm LinearSearch (Array A[L..U], KEY)
For i = L to U
If A[i] == KEY
return i
End If
End For
return NULL
17
If n is the size of the array,
S(n) = O(1)
Analysis of Linear Search
Algorithm LinearSearch (Array A[L..U], KEY)
For i = L to U
If A[i] == KEY
return i
End If
End For
return NULL
18
If n is the size of the array,
the loop will run not more
than “n” times.
In each iteration, it performs
some constant time operations.
T(n) = n.c = O(n)
Best Case
KEY=10
19
1 2 3 4 5 6 7 8 9 10
return 1
T(n)BestCase = O(1)
Worst Case - 1
KEY=44
20
1 2 3 4 5 6 7 8 9 10
return 10
T(n)WorstCase = O(n)
Worst Case - 2
KEY=100
21
1 2 3 4 5 6 7 8 9 10
return NULL
T(n)WorstCase = O(n)
Average Case
■ In the Average Case, the value can be somewhere in the
array.
■ Average Case Time Complexity is the average of Time
Complexities of all the possible inputs.
■ Suppose we have an array of n records.
■ If we search for the first record, then it requires 1
comparison; if the second, then 2 comparisons, and so on.
■ The average of all these searches is:
(1+2+3+4+5+6+7+8+9+…+n+n)/(n+1) = n/2+1 = O(n)22
Advantages of Linear Search
■ The linear search is simple.
■ It is easier to understand & implement.
■ It does not require the data to be stored in
any particular order.
23
Disadvantages of Linear Search
■ It has a very poor efficiency as it takes lots
of comparisons to find a particular record
in big files.
■ It fails in very large databases especially in
real-time systems.
24
Scenario 1
■ Suppose you’re searching for a person in
the phone book. His/Her name starts with
K.
■ You could start at the beginning and keep
flipping pages until you get to the Ks.
■ But you’re more likely to start at a page in
the middle, because you know the Ks are
going to be near the middle of the phone
book.
25
Scenario 2
■ Suppose you’re searching for a word in a
dictionary, and it starts with O.
■ Again, you’ll start near the middle.
26
Scenario 3
27
2.7 billion monthly active users
■ Suppose you log on to Facebook. When you do,
Facebook has to verify that you have an account
on the site.
■ So, it needs to search for your username in its
database.
■ Let your username is LARAIB – for instance -
Facebook could start from the As and search for
your name.
■ But it makes more sense for it to begin
somewhere in the middle.
Binary Search
■ All these cases use the same algorithm to solve the problem:
BINARY SEARCH.
28
Binary Search
■ Binary search is a searching algorithm.
■ Its input is a sorted list of elements.
■ If an element you’re looking for is in that list, binary search
returns the position where it’s located.
■ Otherwise, binary search returns NULL.
29
Comparison with Linear Search
■ I’m thinking of a number between 1 and 100.
■ You have to try to guess my number in the fewest tries
possible.
■ With every guess, I’ll tell you if your guess is too low, too
high, or correct.
30
Comparison with Linear Search
■ Suppose you start guessing like
this: 1, 2, 3, 4 ….
■ Here’s how it would go.
31
Comparison with Linear Search
■ Suppose you start guessing like this:
1, 2, 3, 4 ….
■ Here’s how it would go.
32
Comparison with Linear Search
■ This is the sequential search.
■ With each guess, you’re eliminating only one number.
■ If my number was 99, it could take you 99 guesses to get
there!
33
Comparison with Linear Search
■ Here’s a better technique.
■ Start with 50.
34
Comparison with Linear Search
■ You just eliminated half the numbers!
■ Now you know that 1–50 are all too low.
35
Comparison with Linear Search
■ Next guess: 75.
■ Too high, but again you cut down half the remaining
numbers!
■ With binary search, you guess the middle number and
eliminate half the remaining numbers every time.
36
Comparison with Linear Search
■ Next is 63 (halfway between 50 and 75).
37
Comparison with Linear Search
■ This is binary search.
■ Here’s how many numbers you can eliminate every time.
38
Comparison with Linear Search
■ Whatever number I’m thinking of, you can guess in a
maximum of
seven guesses—because you eliminate so many numbers with
every guess!
39
Comparison with Linear Search
■ Suppose you’re looking for a word in
the dictionary. The dictionary has
240,000 words.
■ In the worst case, how many steps do
you think each search will take?
40
Comparison with Linear Search
■ Simple search could take 240,000 steps if the word you’re
looking for is the very last one in the book.
■ With each step of binary search, you cut the number of
words in half until you’re left with only one word.
41
Working
KEY=31
42
Working
KEY=31
43
Working
KEY=31
44
Working
KEY=31
45
Working
KEY=31
46
Working
KEY=31
47
return 5
Binary Search (Iterative Version)
Algorithm BinarySearch (Array A[L..U], KEY)
While L<=U, do
mid = (L+U)/2
If A[mid] == KEY
return mid
ElseIf A[mid] < KEY
L = mid + 1
Else
U = mid - 1
End If
End While
return NULL
48
Binary Search (Recursive Version)
Algorithm BinSearch (Array A, KEY, U, L)
If L>U
return NULL
End If
mid = (L+U)/2
If A[mid] == KEY
return mid
ElseIf A[mid] < KEY
return BinSearch(A, KEY, mid + 1, U)
Else
return BinSearch(A, KEY, L, mid - 1)
End If
49
Binary Search – Side by Side
Algorithm BinarySearch (Array A[L..U], KEY)
While L<=U, do
mid = (L+U)/2
If A[mid] == KEY
return mid
ElseIf A[mid] < KEY
L = mid + 1
Else
U = mid - 1
End If
End While
return NULL
50
Algorithm BinSearch (Array A, KEY, U, L)
If L>U
return NULL
End If
mid = (L+U)/2
If A[mid] == KEY
return mid
ElseIf A[mid] < KEY
return BinSearch(A, KEY, mid + 1,
U)
Else
return BinSearch(A, KEY, L, mid -
1)
Time Complexity: Comparisons
51
Algorithm BinSearch (Array A, KEY, U, L)
If L>U
return NULL
End If
mid = (L+U)/2
If A[mid] == KEY
return mid
ElseIf A[mid] < KEY
return BinSearch(A, KEY, mid + 1,
U)
Else
return BinSearch(A, KEY, L, mid -
1)
C(n) = C( n/2 ) + c
C(1) = c
C(n) = C(n/2) + c
C(1) = c
52
c
c
c
c
C(n)
C(n/2
)
C(n/22
)
C(n/23
)
C(n/2k) =
C(1)
c
For what value of 2k will C(n/2k) become
T(1).
n/2k=1  n=2k  k=log2n
C(n) = c(k+1)
= c(log2n+1)
= O(log2n)
Practice Questions
53
54

More Related Content

Similar to Data Structures Algorithms - Week 3c - Basic Searching Algorithms.pptx

Algorithm & data structures lec4&5
Algorithm & data structures lec4&5Algorithm & data structures lec4&5
Algorithm & data structures lec4&5
Abdul Khan
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
haramaya university
 
(Data Structure) Chapter11 searching & sorting
(Data Structure) Chapter11 searching & sorting(Data Structure) Chapter11 searching & sorting
(Data Structure) Chapter11 searching & sorting
Fadhil Ismail
 
Searching
SearchingSearching
Searching
Ashim Lamichhane
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
Drishti Bhalla
 
Binary.pptx
Binary.pptxBinary.pptx
Binary.pptx
hasantahamid15
 
Unit viii searching and hashing
Unit   viii searching and hashing Unit   viii searching and hashing
Unit viii searching and hashing
Tribhuvan University
 
Rahat &amp; juhith
Rahat &amp; juhithRahat &amp; juhith
Rahat &amp; juhith
Rj Juhith
 
Searching Algorithms for students of CS and IT using C++
Searching Algorithms for students of CS and IT using C++Searching Algorithms for students of CS and IT using C++
Searching Algorithms for students of CS and IT using C++
shahidameer8
 
Daa final
Daa finalDaa final
Daa final
Gagan019
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
Mohamed Essam
 
Sorting and hashing concepts
Sorting and hashing conceptsSorting and hashing concepts
Sorting and hashing concepts
LJ Projects
 
Sorting and hashing concepts
Sorting and hashing conceptsSorting and hashing concepts
Sorting and hashing concepts
LJ Projects
 
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTINGUnit 6 dsa SEARCHING AND SORTING
Unit 7 sorting
Unit   7 sortingUnit   7 sorting
Unit 7 sorting
Dabbal Singh Mahara
 
SEARCHING
SEARCHINGSEARCHING
SEARCHING
SWATHIR72
 
Cs1311lecture23wdl
Cs1311lecture23wdlCs1311lecture23wdl
Cs1311lecture23wdl
Muhammad Wasif
 
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
 
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
Umesh Kumar
 
Unit vii sorting
Unit   vii sorting Unit   vii sorting
Unit vii sorting
Tribhuvan University
 

Similar to Data Structures Algorithms - Week 3c - Basic Searching Algorithms.pptx (20)

Algorithm & data structures lec4&5
Algorithm & data structures lec4&5Algorithm & data structures lec4&5
Algorithm & data structures lec4&5
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
 
(Data Structure) Chapter11 searching & sorting
(Data Structure) Chapter11 searching & sorting(Data Structure) Chapter11 searching & sorting
(Data Structure) Chapter11 searching & sorting
 
Searching
SearchingSearching
Searching
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
 
Binary.pptx
Binary.pptxBinary.pptx
Binary.pptx
 
Unit viii searching and hashing
Unit   viii searching and hashing Unit   viii searching and hashing
Unit viii searching and hashing
 
Rahat &amp; juhith
Rahat &amp; juhithRahat &amp; juhith
Rahat &amp; juhith
 
Searching Algorithms for students of CS and IT using C++
Searching Algorithms for students of CS and IT using C++Searching Algorithms for students of CS and IT using C++
Searching Algorithms for students of CS and IT using C++
 
Daa final
Daa finalDaa final
Daa final
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
 
Sorting and hashing concepts
Sorting and hashing conceptsSorting and hashing concepts
Sorting and hashing concepts
 
Sorting and hashing concepts
Sorting and hashing conceptsSorting and hashing concepts
Sorting and hashing concepts
 
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTINGUnit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
 
Unit 7 sorting
Unit   7 sortingUnit   7 sorting
Unit 7 sorting
 
SEARCHING
SEARCHINGSEARCHING
SEARCHING
 
Cs1311lecture23wdl
Cs1311lecture23wdlCs1311lecture23wdl
Cs1311lecture23wdl
 
Analysis of Algorithm - Binary Search.pptx
Analysis of Algorithm - Binary Search.pptxAnalysis of Algorithm - Binary Search.pptx
Analysis of Algorithm - Binary Search.pptx
 
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
 
Unit vii sorting
Unit   vii sorting Unit   vii sorting
Unit vii sorting
 

Recently uploaded

RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
Himanshu Rai
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Fajar Baskoro
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
Celine George
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
siemaillard
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
paigestewart1632
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 
How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience
Wahiba Chair Training & Consulting
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
TechSoup
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Diana Rendina
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
iammrhaywood
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
GeorgeMilliken2
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
HajraNaeem15
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
Nguyen Thanh Tu Collection
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
Celine George
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
National Information Standards Organization (NISO)
 

Recently uploaded (20)

RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 
How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
 

Data Structures Algorithms - Week 3c - Basic Searching Algorithms.pptx

  • 1. Week 3c Basic Searching Algorithms DATASTRUCTURESANDALGORITHMS INSTRUCTOR: SulamanAhmadNaz 1
  • 2. Searching ■ Searching is the process of determining whether or not a given value exists in a data structure or a storage media. ■ We will discuss two searching methods on 1D (Linear) data structures: – Linear search – Binary search. 2
  • 3. Linear Search ■ It is also called as Serial or Sequential Search. ■ The linear (or sequential) search algorithm on an array is: – Sequentially scan the array, comparing each array item with the searched value. – If a match is found; return the index of the matched element; otherwise return NULL. ■ Note: linear search can be applied to both sorted and unsorted arrays. 3
  • 4. Linear Search ■ Step through array of records, one at a time. ■ Look for record with matching key. ■ Search stops when – record with matching key is found – or when search has examined all records without success. 4
  • 5. Linear Search Linear Search ( Array A, KEY) Step 1: Set i to L Step 2: While i <= U do Step 3: if A[i] = KEY then go to step 7 Step 4: Set i to i + 1 Step 5: End While Step 6: Print “Element not found” & Exit Step 7: Print “Element x Found at index i” Step 8: Exit 5
  • 6. Linear Search procedure linear_search (Array, KEY) For each item in the Array If item == KEY return the item's location End If End For End procedure 6
  • 7. Linear Search Algorithm LinearSearch (Array A[L..U], KEY) For i = L to U If A[i] == KEY return i End If End For return NULL 7
  • 16. Working KEY=33 16 1 2 3 4 5 6 7 8 9 10 return 7
  • 17. Analysis of Linear Search Algorithm LinearSearch (Array A[L..U], KEY) For i = L to U If A[i] == KEY return i End If End For return NULL 17 If n is the size of the array, S(n) = O(1)
  • 18. Analysis of Linear Search Algorithm LinearSearch (Array A[L..U], KEY) For i = L to U If A[i] == KEY return i End If End For return NULL 18 If n is the size of the array, the loop will run not more than “n” times. In each iteration, it performs some constant time operations. T(n) = n.c = O(n)
  • 19. Best Case KEY=10 19 1 2 3 4 5 6 7 8 9 10 return 1 T(n)BestCase = O(1)
  • 20. Worst Case - 1 KEY=44 20 1 2 3 4 5 6 7 8 9 10 return 10 T(n)WorstCase = O(n)
  • 21. Worst Case - 2 KEY=100 21 1 2 3 4 5 6 7 8 9 10 return NULL T(n)WorstCase = O(n)
  • 22. Average Case ■ In the Average Case, the value can be somewhere in the array. ■ Average Case Time Complexity is the average of Time Complexities of all the possible inputs. ■ Suppose we have an array of n records. ■ If we search for the first record, then it requires 1 comparison; if the second, then 2 comparisons, and so on. ■ The average of all these searches is: (1+2+3+4+5+6+7+8+9+…+n+n)/(n+1) = n/2+1 = O(n)22
  • 23. Advantages of Linear Search ■ The linear search is simple. ■ It is easier to understand & implement. ■ It does not require the data to be stored in any particular order. 23
  • 24. Disadvantages of Linear Search ■ It has a very poor efficiency as it takes lots of comparisons to find a particular record in big files. ■ It fails in very large databases especially in real-time systems. 24
  • 25. Scenario 1 ■ Suppose you’re searching for a person in the phone book. His/Her name starts with K. ■ You could start at the beginning and keep flipping pages until you get to the Ks. ■ But you’re more likely to start at a page in the middle, because you know the Ks are going to be near the middle of the phone book. 25
  • 26. Scenario 2 ■ Suppose you’re searching for a word in a dictionary, and it starts with O. ■ Again, you’ll start near the middle. 26
  • 27. Scenario 3 27 2.7 billion monthly active users ■ Suppose you log on to Facebook. When you do, Facebook has to verify that you have an account on the site. ■ So, it needs to search for your username in its database. ■ Let your username is LARAIB – for instance - Facebook could start from the As and search for your name. ■ But it makes more sense for it to begin somewhere in the middle.
  • 28. Binary Search ■ All these cases use the same algorithm to solve the problem: BINARY SEARCH. 28
  • 29. Binary Search ■ Binary search is a searching algorithm. ■ Its input is a sorted list of elements. ■ If an element you’re looking for is in that list, binary search returns the position where it’s located. ■ Otherwise, binary search returns NULL. 29
  • 30. Comparison with Linear Search ■ I’m thinking of a number between 1 and 100. ■ You have to try to guess my number in the fewest tries possible. ■ With every guess, I’ll tell you if your guess is too low, too high, or correct. 30
  • 31. Comparison with Linear Search ■ Suppose you start guessing like this: 1, 2, 3, 4 …. ■ Here’s how it would go. 31
  • 32. Comparison with Linear Search ■ Suppose you start guessing like this: 1, 2, 3, 4 …. ■ Here’s how it would go. 32
  • 33. Comparison with Linear Search ■ This is the sequential search. ■ With each guess, you’re eliminating only one number. ■ If my number was 99, it could take you 99 guesses to get there! 33
  • 34. Comparison with Linear Search ■ Here’s a better technique. ■ Start with 50. 34
  • 35. Comparison with Linear Search ■ You just eliminated half the numbers! ■ Now you know that 1–50 are all too low. 35
  • 36. Comparison with Linear Search ■ Next guess: 75. ■ Too high, but again you cut down half the remaining numbers! ■ With binary search, you guess the middle number and eliminate half the remaining numbers every time. 36
  • 37. Comparison with Linear Search ■ Next is 63 (halfway between 50 and 75). 37
  • 38. Comparison with Linear Search ■ This is binary search. ■ Here’s how many numbers you can eliminate every time. 38
  • 39. Comparison with Linear Search ■ Whatever number I’m thinking of, you can guess in a maximum of seven guesses—because you eliminate so many numbers with every guess! 39
  • 40. Comparison with Linear Search ■ Suppose you’re looking for a word in the dictionary. The dictionary has 240,000 words. ■ In the worst case, how many steps do you think each search will take? 40
  • 41. Comparison with Linear Search ■ Simple search could take 240,000 steps if the word you’re looking for is the very last one in the book. ■ With each step of binary search, you cut the number of words in half until you’re left with only one word. 41
  • 48. Binary Search (Iterative Version) Algorithm BinarySearch (Array A[L..U], KEY) While L<=U, do mid = (L+U)/2 If A[mid] == KEY return mid ElseIf A[mid] < KEY L = mid + 1 Else U = mid - 1 End If End While return NULL 48
  • 49. Binary Search (Recursive Version) Algorithm BinSearch (Array A, KEY, U, L) If L>U return NULL End If mid = (L+U)/2 If A[mid] == KEY return mid ElseIf A[mid] < KEY return BinSearch(A, KEY, mid + 1, U) Else return BinSearch(A, KEY, L, mid - 1) End If 49
  • 50. Binary Search – Side by Side Algorithm BinarySearch (Array A[L..U], KEY) While L<=U, do mid = (L+U)/2 If A[mid] == KEY return mid ElseIf A[mid] < KEY L = mid + 1 Else U = mid - 1 End If End While return NULL 50 Algorithm BinSearch (Array A, KEY, U, L) If L>U return NULL End If mid = (L+U)/2 If A[mid] == KEY return mid ElseIf A[mid] < KEY return BinSearch(A, KEY, mid + 1, U) Else return BinSearch(A, KEY, L, mid - 1)
  • 51. Time Complexity: Comparisons 51 Algorithm BinSearch (Array A, KEY, U, L) If L>U return NULL End If mid = (L+U)/2 If A[mid] == KEY return mid ElseIf A[mid] < KEY return BinSearch(A, KEY, mid + 1, U) Else return BinSearch(A, KEY, L, mid - 1) C(n) = C( n/2 ) + c C(1) = c C(n) = C(n/2) + c C(1) = c
  • 52. 52 c c c c C(n) C(n/2 ) C(n/22 ) C(n/23 ) C(n/2k) = C(1) c For what value of 2k will C(n/2k) become T(1). n/2k=1  n=2k  k=log2n C(n) = c(k+1) = c(log2n+1) = O(log2n)
  • 54. 54