SlideShare a Scribd company logo
Prepared by Nisha Soms
Reference:
(i) Kruse and Ryba Ch 7.1-7.3 and 9.6
(ii) https://cs-eb.bu.edu/fac/gkollios/cs113/Slides/Searching.ppt
Problem: Search
 We are given a list of records.
 Each record has an associated key.
 Give efficient algorithm for searching for a record
containing a particular key.
 Efficiency is quantified in terms of average time
analysis (number of comparisons) to retrieve an item.
Search
[ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 700 ]
Number 506643548
Number 233667136
Number 281942902
Number 155778322
Number 580625685
Number 701466868
…
Number 580625685
Each record in list has an associated key.
In this example, the keys are ID numbers.
Given a particular key, how can we
efficiently retrieve the record from the list?
Serial 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.
Pseudocode for Serial Search
// Search for a desired item in the n array elements
// starting at a[first].
// Returns pointer to desired record if found.
// Otherwise, return NULL
…
for(i = first; i < n; ++i )
if(a[first+i] is desired item)
return &a[first+i];
// if we drop through loop, then desired item was not found
return NULL;
Serial Search Analysis
 What are the worst and average case running times for
serial search?
 We must determine the O-notation for the number of
operations required in search.
 Number of operations depends on n, the number of
entries in the list.
Worst Case Time for Serial Search
 For an array of n elements, the worst case time
for serial search requires n array accesses:
O(n).
 Consider cases where we must loop over all n
records:
 desired record appears in the last position of the
array
 desired record does not appear in the array at all
Average Case for Serial Search
Assumptions:
1. All keys are equally likely in a search
2. We always search for a key that is in the array
Example:
 We have an array of 10 records.
 If search for the first record, then it requires 1
array access; if the second, then 2 array accesses.
etc.
The average of all these searches is:
(1+2+3+4+5+6+7+8+9+10)/10 = 5.5
Average Case Time for Serial Search
Generalize for array size n.
Expression for average-case running time:
(1+2+…+n)/n = n(n+1)/2n = (n+1)/2
Therefore, average case time complexity for serial
search is O(n).
Binary Search
 Perhaps we can do better than O(n) in the average
case?
 Assume that we are give an array of records that is
sorted. For instance:
 an array of records with integer keys sorted from
smallest to largest (e.g., ID numbers), or
 an array of records with string keys sorted in
alphabetical order (e.g., names).
Binary Search Pseudocode
…
if(size == 0)
found = false;
else {
middle = index of approximate midpoint of array segment;
if(target == a[middle])
target has been found!
else if(target < a[middle])
search for target in area before midpoint;
else
search for target in area after midpoint;
}
…
Binary Search
[ 0 ] [ 1 ]
Example: sorted array of integer keys. Target=7.
3 6 7 11 32 33 53
[ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ]
Binary Search
[ 0 ] [ 1 ]
Example: sorted array of integer keys. Target=7.
3 6 7 11 32 33 53
[ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ]
Find approximate midpoint
Binary Search
[ 0 ] [ 1 ]
Example: sorted array of integer keys. Target=7.
3 6 7 11 32 33 53
[ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ]
Is 7 = midpoint key? NO.
Binary Search
[ 0 ] [ 1 ]
Example: sorted array of integer keys. Target=7.
3 6 7 11 32 33 53
[ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ]
Is 7 < midpoint key? YES.
Binary Search
[ 0 ] [ 1 ]
Example: sorted array of integer keys. Target=7.
3 6 7 11 32 33 53
[ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ]
Search for the target in the area before midpoint.
Binary Search
[ 0 ] [ 1 ]
Example: sorted array of integer keys. Target=7.
3 6 7 11 32 33 53
[ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ]
Find approximate midpoint
Binary Search
[ 0 ] [ 1 ]
Example: sorted array of integer keys. Target=7.
3 6 7 11 32 33 53
[ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ]
Target = key of midpoint? NO.
Binary Search
[ 0 ] [ 1 ]
Example: sorted array of integer keys. Target=7.
3 6 7 11 32 33 53
[ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ]
Target < key of midpoint? NO.
Binary Search
[ 0 ] [ 1 ]
Example: sorted array of integer keys. Target=7.
3 6 7 11 32 33 53
[ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ]
Target > key of midpoint? YES.
Binary Search
[ 0 ] [ 1 ]
Example: sorted array of integer keys. Target=7.
3 6 7 11 32 33 53
[ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ]
Search for the target in the area after midpoint.
Binary Search
[ 0 ] [ 1 ]
Example: sorted array of integer keys. Target=7.
3 6 7 11 32 33 53
[ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ]
Find approximate midpoint.
Is target = midpoint key? YES.
Binary Search Implementation
void search(const int a[ ], size_t first, size_t size, int target, bool& found, size_t& location)
{
size_t middle;
if(size == 0) found = false;
else {
middle = first + size/2;
if(target == a[middle]){
location = middle;
found = true;
}
else if (target < a[middle])
// target is less than middle, so search subarray before middle
search(a, first, size/2, target, found, location);
else
// target is greater than middle, so search subarray after middle
search(a, middle+1, (size-1)/2, target, found, location);
}
}
Relation to Binary Search Tree
Corresponding complete binary search tree
3 6 7 11 32 33 53
3
6
7
11
32
33
53
Array of previous example:
Search for target = 7
Start at root:
Find midpoint:
3 6 7 11 32 33 53
3
6
7
11
32
33
53
Search left subarray:
Search for target = 7
Search left subtree:
3 6 7 11 32 33 53
3
6
7
11
32
33
53
Find approximate midpoint of subarray:
Search for target = 7
Visit root of subtree:
3 6 7 11 32 33 53
3
6
7
11
32
33
53
Search right subarray:
Search for target = 7
Search right subtree:
3 6 7 11 32 33 53
3
6
7
11
32
33
53
Binary Search: Analysis
 Worst case complexity?
 What is the maximum depth of recursive calls in
binary search as function of n?
 Each level in the recursion, we split the array in half
(divide by two).
 Therefore maximum recursion depth is floor(log2n)
and worst case = O(log2n).
 Average case is also = O(log2n).
Can we do better than O(log2n)?
 Average and worst case of serial search = O(n)
 Average and worst case of binary search =
O(log2n)
 Can we do better than this?
YES. Use a hash table!
Linear and Binary search

More Related Content

What's hot (20)

PPT
Sorting Techniques
Rafay Farooq
 
PPTX
Binary search
AparnaKumari31
 
PPTX
Binary Search Tree in Data Structure
Dharita Chokshi
 
PPT
Hashing PPT
Saurabh Kumar
 
PPTX
Sorting Algorithms
Pranay Neema
 
PPT
Bucket sort
Hossain Md Shakhawat
 
PPT
Data Structure and Algorithms Binary Search Tree
ManishPrajapati78
 
PDF
Searching and Sorting Techniques in Data Structure
Balwant Gorad
 
PPTX
Quick sort-Data Structure
Jeanie Arnoco
 
PPTX
Presentation on the topic selection sort
District Administration
 
PPTX
Binary search in data structure
Meherul1234
 
PPTX
Searching Techniques and Analysis
AkashBorse2
 
PDF
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
Sowmya Jyothi
 
PDF
Algorithms Lecture 4: Sorting Algorithms I
Mohamed Loey
 
PPT
Lecture 1 data structures and algorithms
Aakash deep Singhal
 
PDF
Binary search algorithm
maamir farooq
 
PPTX
Searching
Ashim Lamichhane
 
PPTX
Insertion sort algorithm power point presentation
University of Science and Technology Chitttagong
 
PDF
Expression trees
Salman Vadsarya
 
PDF
Java Linked List Tutorial | Edureka
Edureka!
 
Sorting Techniques
Rafay Farooq
 
Binary search
AparnaKumari31
 
Binary Search Tree in Data Structure
Dharita Chokshi
 
Hashing PPT
Saurabh Kumar
 
Sorting Algorithms
Pranay Neema
 
Data Structure and Algorithms Binary Search Tree
ManishPrajapati78
 
Searching and Sorting Techniques in Data Structure
Balwant Gorad
 
Quick sort-Data Structure
Jeanie Arnoco
 
Presentation on the topic selection sort
District Administration
 
Binary search in data structure
Meherul1234
 
Searching Techniques and Analysis
AkashBorse2
 
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
Sowmya Jyothi
 
Algorithms Lecture 4: Sorting Algorithms I
Mohamed Loey
 
Lecture 1 data structures and algorithms
Aakash deep Singhal
 
Binary search algorithm
maamir farooq
 
Searching
Ashim Lamichhane
 
Insertion sort algorithm power point presentation
University of Science and Technology Chitttagong
 
Expression trees
Salman Vadsarya
 
Java Linked List Tutorial | Edureka
Edureka!
 

Similar to Linear and Binary search (20)

PDF
dsa pdf.pdf
DewashishDwivedi
 
PPT
Searching algorithm
MG Thushara Pradeesh
 
PPT
Searching.ppt
p83629918
 
PPT
Searching Algorithms with Binary Search and Hashing Concept with Time and Spa...
mrhabib10
 
PPT
Searching.ppt
rasheed747195
 
PPT
Searching inear Search And Binary Agorithms
PuneetVashistha1
 
PPSX
Searching in Arrays
Dhiviya Rose
 
PDF
Ocw chp6 2searchbinary
Prashant Rai
 
PPT
Search techniques and Hashing
Thirunavukarasu Mani
 
PPTX
Unit 8 searching and hashing
Dabbal Singh Mahara
 
PPTX
Bsc cs ii dfs u-4 sorting and searching structure
Rai University
 
PPTX
Bca ii dfs u-4 sorting and searching structure
Rai University
 
PPTX
Mca ii dfs u-5 sorting and searching structure
Rai University
 
PPTX
Chapter 2. data structure and algorithm
SolomonEndalu
 
PPSX
Algorithm and Programming (Searching)
Adam Mukharil Bachtiar
 
PPT
Chapter 4: basic search algorithms data structure
Mahmoud Alfarra
 
PPTX
Binary search2
tahanihassan
 
PPTX
Chapter #3 (Searchinmmmg ALgorithm).pptx
hekmatyarzahir44
 
PPT
ds 2-Arrays and its types and operations
kavita20193
 
PPT
ds 2Arrays.ppt
AlliVinay1
 
dsa pdf.pdf
DewashishDwivedi
 
Searching algorithm
MG Thushara Pradeesh
 
Searching.ppt
p83629918
 
Searching Algorithms with Binary Search and Hashing Concept with Time and Spa...
mrhabib10
 
Searching.ppt
rasheed747195
 
Searching inear Search And Binary Agorithms
PuneetVashistha1
 
Searching in Arrays
Dhiviya Rose
 
Ocw chp6 2searchbinary
Prashant Rai
 
Search techniques and Hashing
Thirunavukarasu Mani
 
Unit 8 searching and hashing
Dabbal Singh Mahara
 
Bsc cs ii dfs u-4 sorting and searching structure
Rai University
 
Bca ii dfs u-4 sorting and searching structure
Rai University
 
Mca ii dfs u-5 sorting and searching structure
Rai University
 
Chapter 2. data structure and algorithm
SolomonEndalu
 
Algorithm and Programming (Searching)
Adam Mukharil Bachtiar
 
Chapter 4: basic search algorithms data structure
Mahmoud Alfarra
 
Binary search2
tahanihassan
 
Chapter #3 (Searchinmmmg ALgorithm).pptx
hekmatyarzahir44
 
ds 2-Arrays and its types and operations
kavita20193
 
ds 2Arrays.ppt
AlliVinay1
 
Ad

More from Nisha Soms (6)

PPTX
Asymptotic analysis
Nisha Soms
 
PPTX
Algorithm analysis
Nisha Soms
 
PPTX
Notion of an algorithm
Nisha Soms
 
PPTX
Divide and conquer strategy
Nisha Soms
 
PPTX
Merge sort
Nisha Soms
 
PPTX
Depth First Search and Breadth First Search
Nisha Soms
 
Asymptotic analysis
Nisha Soms
 
Algorithm analysis
Nisha Soms
 
Notion of an algorithm
Nisha Soms
 
Divide and conquer strategy
Nisha Soms
 
Merge sort
Nisha Soms
 
Depth First Search and Breadth First Search
Nisha Soms
 
Ad

Recently uploaded (20)

PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
PDF
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
PDF
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
PPTX
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
PDF
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PPTX
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
PPSX
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PPTX
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PDF
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PDF
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
PDF
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PPTX
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
PDF
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 

Linear and Binary search

  • 1. Prepared by Nisha Soms Reference: (i) Kruse and Ryba Ch 7.1-7.3 and 9.6 (ii) https://cs-eb.bu.edu/fac/gkollios/cs113/Slides/Searching.ppt
  • 2. Problem: Search  We are given a list of records.  Each record has an associated key.  Give efficient algorithm for searching for a record containing a particular key.  Efficiency is quantified in terms of average time analysis (number of comparisons) to retrieve an item.
  • 3. Search [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 700 ] Number 506643548 Number 233667136 Number 281942902 Number 155778322 Number 580625685 Number 701466868 … Number 580625685 Each record in list has an associated key. In this example, the keys are ID numbers. Given a particular key, how can we efficiently retrieve the record from the list?
  • 4. Serial 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.
  • 5. Pseudocode for Serial Search // Search for a desired item in the n array elements // starting at a[first]. // Returns pointer to desired record if found. // Otherwise, return NULL … for(i = first; i < n; ++i ) if(a[first+i] is desired item) return &a[first+i]; // if we drop through loop, then desired item was not found return NULL;
  • 6. Serial Search Analysis  What are the worst and average case running times for serial search?  We must determine the O-notation for the number of operations required in search.  Number of operations depends on n, the number of entries in the list.
  • 7. Worst Case Time for Serial Search  For an array of n elements, the worst case time for serial search requires n array accesses: O(n).  Consider cases where we must loop over all n records:  desired record appears in the last position of the array  desired record does not appear in the array at all
  • 8. Average Case for Serial Search Assumptions: 1. All keys are equally likely in a search 2. We always search for a key that is in the array Example:  We have an array of 10 records.  If search for the first record, then it requires 1 array access; if the second, then 2 array accesses. etc. The average of all these searches is: (1+2+3+4+5+6+7+8+9+10)/10 = 5.5
  • 9. Average Case Time for Serial Search Generalize for array size n. Expression for average-case running time: (1+2+…+n)/n = n(n+1)/2n = (n+1)/2 Therefore, average case time complexity for serial search is O(n).
  • 10. Binary Search  Perhaps we can do better than O(n) in the average case?  Assume that we are give an array of records that is sorted. For instance:  an array of records with integer keys sorted from smallest to largest (e.g., ID numbers), or  an array of records with string keys sorted in alphabetical order (e.g., names).
  • 11. Binary Search Pseudocode … if(size == 0) found = false; else { middle = index of approximate midpoint of array segment; if(target == a[middle]) target has been found! else if(target < a[middle]) search for target in area before midpoint; else search for target in area after midpoint; } …
  • 12. Binary Search [ 0 ] [ 1 ] Example: sorted array of integer keys. Target=7. 3 6 7 11 32 33 53 [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ]
  • 13. Binary Search [ 0 ] [ 1 ] Example: sorted array of integer keys. Target=7. 3 6 7 11 32 33 53 [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] Find approximate midpoint
  • 14. Binary Search [ 0 ] [ 1 ] Example: sorted array of integer keys. Target=7. 3 6 7 11 32 33 53 [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] Is 7 = midpoint key? NO.
  • 15. Binary Search [ 0 ] [ 1 ] Example: sorted array of integer keys. Target=7. 3 6 7 11 32 33 53 [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] Is 7 < midpoint key? YES.
  • 16. Binary Search [ 0 ] [ 1 ] Example: sorted array of integer keys. Target=7. 3 6 7 11 32 33 53 [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] Search for the target in the area before midpoint.
  • 17. Binary Search [ 0 ] [ 1 ] Example: sorted array of integer keys. Target=7. 3 6 7 11 32 33 53 [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] Find approximate midpoint
  • 18. Binary Search [ 0 ] [ 1 ] Example: sorted array of integer keys. Target=7. 3 6 7 11 32 33 53 [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] Target = key of midpoint? NO.
  • 19. Binary Search [ 0 ] [ 1 ] Example: sorted array of integer keys. Target=7. 3 6 7 11 32 33 53 [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] Target < key of midpoint? NO.
  • 20. Binary Search [ 0 ] [ 1 ] Example: sorted array of integer keys. Target=7. 3 6 7 11 32 33 53 [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] Target > key of midpoint? YES.
  • 21. Binary Search [ 0 ] [ 1 ] Example: sorted array of integer keys. Target=7. 3 6 7 11 32 33 53 [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] Search for the target in the area after midpoint.
  • 22. Binary Search [ 0 ] [ 1 ] Example: sorted array of integer keys. Target=7. 3 6 7 11 32 33 53 [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] Find approximate midpoint. Is target = midpoint key? YES.
  • 23. Binary Search Implementation void search(const int a[ ], size_t first, size_t size, int target, bool& found, size_t& location) { size_t middle; if(size == 0) found = false; else { middle = first + size/2; if(target == a[middle]){ location = middle; found = true; } else if (target < a[middle]) // target is less than middle, so search subarray before middle search(a, first, size/2, target, found, location); else // target is greater than middle, so search subarray after middle search(a, middle+1, (size-1)/2, target, found, location); } }
  • 24. Relation to Binary Search Tree Corresponding complete binary search tree 3 6 7 11 32 33 53 3 6 7 11 32 33 53 Array of previous example:
  • 25. Search for target = 7 Start at root: Find midpoint: 3 6 7 11 32 33 53 3 6 7 11 32 33 53
  • 26. Search left subarray: Search for target = 7 Search left subtree: 3 6 7 11 32 33 53 3 6 7 11 32 33 53
  • 27. Find approximate midpoint of subarray: Search for target = 7 Visit root of subtree: 3 6 7 11 32 33 53 3 6 7 11 32 33 53
  • 28. Search right subarray: Search for target = 7 Search right subtree: 3 6 7 11 32 33 53 3 6 7 11 32 33 53
  • 29. Binary Search: Analysis  Worst case complexity?  What is the maximum depth of recursive calls in binary search as function of n?  Each level in the recursion, we split the array in half (divide by two).  Therefore maximum recursion depth is floor(log2n) and worst case = O(log2n).  Average case is also = O(log2n).
  • 30. Can we do better than O(log2n)?  Average and worst case of serial search = O(n)  Average and worst case of binary search = O(log2n)  Can we do better than this? YES. Use a hash table!