SlideShare a Scribd company logo
Data Structures (KCS-301)
Searching
Lecture W3.L1
Department of Computer Science and
Engineering
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 = 1 to n)
{
if(a[i] == item)
return i;
}
If(i==n+1)
Print: “Not Found”;
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
•If item is present at the last or item is not present at all.
•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.
Pseudo Code - Iterative
Procedure binary_search
A ← sorted array
n ← size of array
x ← value to be searched
lowerBound = 1
upperBound = n
Do
{
Mid = (lowerBound+upperBound)/2;
if(A[Mid] = = x)
break;
else if (x < A[Mid])
upperBound = Mid – 1;
else
lowerBound = Mid + 1;
}While(lowerBound < = upperBound);
if(lowerBound < = upperBound)
Print: “Element Found at Mid”;
else
Print: “Not Found”;
Pseudo Code - Recursive
Procedure binary_search
A ← sorted array
n ← size of array
x ← value to be searched
lowerBound = 1
upperBound = n
BinarySearch(A[],lowerBound,upperBound,ITEM)
{
if(if(lowerBound > upperBound)
return -1;
Mid = (lowerBound+upperBound)/2;
if(A[Mid] = = x)
return Mid;
else if (x < A[Mid])
BinarySearch(A[], lowerBound, Mid-1, ITEM);
else
BinarySearch(A[], Mid+1, upperBound, ITEM);
}
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(log2
n) and
worst case = O(log2
n).
•Average case is also = O(log2
n).
Indexed Sequential Search
•In this searching method, first of all, an index file is created,
that contains some specific group or division of required
record when the index is obtained, then the partial indexing
takes less time cause it is located in a specified group.
Characteristics
•In Indexed Sequential Search a sorted index is set aside in addition to
the array.
•Each element in the index points to a block of elements in the array
or another expanded index.
•The index is searched 1st then the array and guides the search in the
array.
Explanation by diagram “Indexed Sequential
Search”:

More Related Content

Similar to dsa pdf.pdf

Sorting
SortingSorting
Sorting
Samsil Arefin
 
Searching in Arrays
Searching in ArraysSearching in Arrays
Searching in Arrays
Dhiviya Rose
 
searching techniques.pptx
searching techniques.pptxsearching techniques.pptx
searching techniques.pptx
Dr.Shweta
 
Bsc cs ii dfs u-4 sorting and searching structure
Bsc cs ii dfs u-4 sorting and searching structureBsc cs ii dfs u-4 sorting and searching structure
Bsc cs ii dfs u-4 sorting and searching structure
Rai University
 
Bca ii dfs u-4 sorting and searching structure
Bca ii dfs u-4 sorting and searching structureBca ii dfs u-4 sorting and searching structure
Bca ii dfs u-4 sorting and searching structure
Rai University
 
Mca ii dfs u-5 sorting and searching structure
Mca ii dfs u-5 sorting and searching structureMca ii dfs u-5 sorting and searching structure
Mca ii dfs u-5 sorting and searching structure
Rai University
 
search_sort.ppt
search_sort.pptsearch_sort.ppt
search_sort.ppt
SwatiHans10
 
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
 
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTINGUnit 6 dsa SEARCHING AND SORTING
CS-102 Data Structures HashFunction CS102.pdf
CS-102 Data Structures HashFunction CS102.pdfCS-102 Data Structures HashFunction CS102.pdf
CS-102 Data Structures HashFunction CS102.pdf
ssuser034ce1
 
Array 2
Array 2Array 2
Array 2
Abbott
 
Binary search Algorithm
Binary search AlgorithmBinary search Algorithm
Binary search Algorithm
FazalRehman79
 
sorting-160810203705.pptx
sorting-160810203705.pptxsorting-160810203705.pptx
sorting-160810203705.pptx
AnSHiKa187943
 
Algorithm and Programming (Searching)
Algorithm and Programming (Searching)Algorithm and Programming (Searching)
Algorithm and Programming (Searching)
Adam Mukharil Bachtiar
 
DS Unit 1.pptx
DS Unit 1.pptxDS Unit 1.pptx
DS Unit 1.pptx
chin463670
 
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
 
Lecture_Oct26.pptx
Lecture_Oct26.pptxLecture_Oct26.pptx
Lecture_Oct26.pptx
SylrizcinMarieManzo3
 
searching in data structure.pptx
searching in data structure.pptxsearching in data structure.pptx
searching in data structure.pptx
chouguleamruta24
 

Similar to dsa pdf.pdf (20)

Sorting
SortingSorting
Sorting
 
Searching in Arrays
Searching in ArraysSearching in Arrays
Searching in Arrays
 
searching techniques.pptx
searching techniques.pptxsearching techniques.pptx
searching techniques.pptx
 
Bsc cs ii dfs u-4 sorting and searching structure
Bsc cs ii dfs u-4 sorting and searching structureBsc cs ii dfs u-4 sorting and searching structure
Bsc cs ii dfs u-4 sorting and searching structure
 
Bca ii dfs u-4 sorting and searching structure
Bca ii dfs u-4 sorting and searching structureBca ii dfs u-4 sorting and searching structure
Bca ii dfs u-4 sorting and searching structure
 
Mca ii dfs u-5 sorting and searching structure
Mca ii dfs u-5 sorting and searching structureMca ii dfs u-5 sorting and searching structure
Mca ii dfs u-5 sorting and searching structure
 
search_sort.ppt
search_sort.pptsearch_sort.ppt
search_sort.ppt
 
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
 
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTINGUnit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
 
CS-102 Data Structures HashFunction CS102.pdf
CS-102 Data Structures HashFunction CS102.pdfCS-102 Data Structures HashFunction CS102.pdf
CS-102 Data Structures HashFunction CS102.pdf
 
Array 2
Array 2Array 2
Array 2
 
Binary search Algorithm
Binary search AlgorithmBinary search Algorithm
Binary search Algorithm
 
sorting-160810203705.pptx
sorting-160810203705.pptxsorting-160810203705.pptx
sorting-160810203705.pptx
 
Algorithm and Programming (Searching)
Algorithm and Programming (Searching)Algorithm and Programming (Searching)
Algorithm and Programming (Searching)
 
DS Unit 1.pptx
DS Unit 1.pptxDS Unit 1.pptx
DS Unit 1.pptx
 
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
 
Lecture_Oct26.pptx
Lecture_Oct26.pptxLecture_Oct26.pptx
Lecture_Oct26.pptx
 
searching in data structure.pptx
searching in data structure.pptxsearching in data structure.pptx
searching in data structure.pptx
 

Recently uploaded

Switching Careers Slides - JoyceMSullivan SocMediaFin - 2024Jun11.pdf
Switching Careers Slides - JoyceMSullivan SocMediaFin -  2024Jun11.pdfSwitching Careers Slides - JoyceMSullivan SocMediaFin -  2024Jun11.pdf
Switching Careers Slides - JoyceMSullivan SocMediaFin - 2024Jun11.pdf
SocMediaFin - Joyce Sullivan
 
0624.speakingengagementsandteaching-01.pdf
0624.speakingengagementsandteaching-01.pdf0624.speakingengagementsandteaching-01.pdf
0624.speakingengagementsandteaching-01.pdf
Thomas GIRARD BDes
 
IT Career Hacks Navigate the Tech Jungle with a Roadmap
IT Career Hacks Navigate the Tech Jungle with a RoadmapIT Career Hacks Navigate the Tech Jungle with a Roadmap
IT Career Hacks Navigate the Tech Jungle with a Roadmap
Base Camp
 
A Guide to a Winning Interview June 2024
A Guide to a Winning Interview June 2024A Guide to a Winning Interview June 2024
A Guide to a Winning Interview June 2024
Bruce Bennett
 
thyroid case presentation.pptx Kamala's Lakshaman palatial
thyroid case presentation.pptx Kamala's Lakshaman palatialthyroid case presentation.pptx Kamala's Lakshaman palatial
thyroid case presentation.pptx Kamala's Lakshaman palatial
Aditya Raghav
 
Leadership Ambassador club Adventist module
Leadership Ambassador club Adventist moduleLeadership Ambassador club Adventist module
Leadership Ambassador club Adventist module
kakomaeric00
 
Connect to Grow: The power of building networks
Connect to Grow: The power of building networksConnect to Grow: The power of building networks
Connect to Grow: The power of building networks
Eirini SYKA-LERIOTI
 
Leave-rules.ppt CCS leave rules 1972 for central govt employees
Leave-rules.ppt CCS leave rules 1972 for central govt employeesLeave-rules.ppt CCS leave rules 1972 for central govt employees
Leave-rules.ppt CCS leave rules 1972 for central govt employees
Sreenivas702647
 
Tape Measure Training & Practice Assessments.pdf
Tape Measure Training & Practice Assessments.pdfTape Measure Training & Practice Assessments.pdf
Tape Measure Training & Practice Assessments.pdf
KateRobinson68
 
BUKU PENJAGAAN BUKU PENJAGAAN BUKU PENJAGAAN
BUKU PENJAGAAN BUKU PENJAGAAN BUKU PENJAGAANBUKU PENJAGAAN BUKU PENJAGAAN BUKU PENJAGAAN
BUKU PENJAGAAN BUKU PENJAGAAN BUKU PENJAGAAN
cahgading001
 
All Of My Java Codes With A Sample Output.docx
All Of My Java Codes With A Sample Output.docxAll Of My Java Codes With A Sample Output.docx
All Of My Java Codes With A Sample Output.docx
adhitya5119
 
Learnings from Successful Jobs Searchers
Learnings from Successful Jobs SearchersLearnings from Successful Jobs Searchers
Learnings from Successful Jobs Searchers
Bruce Bennett
 
How to Prepare for Fortinet FCP_FAC_AD-6.5 Certification?
How to Prepare for Fortinet FCP_FAC_AD-6.5 Certification?How to Prepare for Fortinet FCP_FAC_AD-6.5 Certification?
How to Prepare for Fortinet FCP_FAC_AD-6.5 Certification?
NWEXAM
 
一比一原版布拉德福德大学毕业证(bradford毕业证)如何办理
一比一原版布拉德福德大学毕业证(bradford毕业证)如何办理一比一原版布拉德福德大学毕业证(bradford毕业证)如何办理
一比一原版布拉德福德大学毕业证(bradford毕业证)如何办理
taqyea
 
Jill Pizzola's Tenure as Senior Talent Acquisition Partner at THOMSON REUTERS...
Jill Pizzola's Tenure as Senior Talent Acquisition Partner at THOMSON REUTERS...Jill Pizzola's Tenure as Senior Talent Acquisition Partner at THOMSON REUTERS...
Jill Pizzola's Tenure as Senior Talent Acquisition Partner at THOMSON REUTERS...
dsnow9802
 
官方认证美国旧金山州立大学毕业证学位证书案例原版一模一样
官方认证美国旧金山州立大学毕业证学位证书案例原版一模一样官方认证美国旧金山州立大学毕业证学位证书案例原版一模一样
官方认证美国旧金山州立大学毕业证学位证书案例原版一模一样
2zjra9bn
 
Lbs last rank 2023 9988kr47h4744j445.pdf
Lbs last rank 2023 9988kr47h4744j445.pdfLbs last rank 2023 9988kr47h4744j445.pdf
Lbs last rank 2023 9988kr47h4744j445.pdf
ashiquepa3
 
Gabrielle M. A. Sinaga Portfolio, Film Student (2024)
Gabrielle M. A. Sinaga Portfolio, Film Student (2024)Gabrielle M. A. Sinaga Portfolio, Film Student (2024)
Gabrielle M. A. Sinaga Portfolio, Film Student (2024)
GabrielleSinaga
 
在线制作加拿大萨省大学毕业证文凭证书实拍图原版一模一样
在线制作加拿大萨省大学毕业证文凭证书实拍图原版一模一样在线制作加拿大萨省大学毕业证文凭证书实拍图原版一模一样
在线制作加拿大萨省大学毕业证文凭证书实拍图原版一模一样
2zjra9bn
 

Recently uploaded (19)

Switching Careers Slides - JoyceMSullivan SocMediaFin - 2024Jun11.pdf
Switching Careers Slides - JoyceMSullivan SocMediaFin -  2024Jun11.pdfSwitching Careers Slides - JoyceMSullivan SocMediaFin -  2024Jun11.pdf
Switching Careers Slides - JoyceMSullivan SocMediaFin - 2024Jun11.pdf
 
0624.speakingengagementsandteaching-01.pdf
0624.speakingengagementsandteaching-01.pdf0624.speakingengagementsandteaching-01.pdf
0624.speakingengagementsandteaching-01.pdf
 
IT Career Hacks Navigate the Tech Jungle with a Roadmap
IT Career Hacks Navigate the Tech Jungle with a RoadmapIT Career Hacks Navigate the Tech Jungle with a Roadmap
IT Career Hacks Navigate the Tech Jungle with a Roadmap
 
A Guide to a Winning Interview June 2024
A Guide to a Winning Interview June 2024A Guide to a Winning Interview June 2024
A Guide to a Winning Interview June 2024
 
thyroid case presentation.pptx Kamala's Lakshaman palatial
thyroid case presentation.pptx Kamala's Lakshaman palatialthyroid case presentation.pptx Kamala's Lakshaman palatial
thyroid case presentation.pptx Kamala's Lakshaman palatial
 
Leadership Ambassador club Adventist module
Leadership Ambassador club Adventist moduleLeadership Ambassador club Adventist module
Leadership Ambassador club Adventist module
 
Connect to Grow: The power of building networks
Connect to Grow: The power of building networksConnect to Grow: The power of building networks
Connect to Grow: The power of building networks
 
Leave-rules.ppt CCS leave rules 1972 for central govt employees
Leave-rules.ppt CCS leave rules 1972 for central govt employeesLeave-rules.ppt CCS leave rules 1972 for central govt employees
Leave-rules.ppt CCS leave rules 1972 for central govt employees
 
Tape Measure Training & Practice Assessments.pdf
Tape Measure Training & Practice Assessments.pdfTape Measure Training & Practice Assessments.pdf
Tape Measure Training & Practice Assessments.pdf
 
BUKU PENJAGAAN BUKU PENJAGAAN BUKU PENJAGAAN
BUKU PENJAGAAN BUKU PENJAGAAN BUKU PENJAGAANBUKU PENJAGAAN BUKU PENJAGAAN BUKU PENJAGAAN
BUKU PENJAGAAN BUKU PENJAGAAN BUKU PENJAGAAN
 
All Of My Java Codes With A Sample Output.docx
All Of My Java Codes With A Sample Output.docxAll Of My Java Codes With A Sample Output.docx
All Of My Java Codes With A Sample Output.docx
 
Learnings from Successful Jobs Searchers
Learnings from Successful Jobs SearchersLearnings from Successful Jobs Searchers
Learnings from Successful Jobs Searchers
 
How to Prepare for Fortinet FCP_FAC_AD-6.5 Certification?
How to Prepare for Fortinet FCP_FAC_AD-6.5 Certification?How to Prepare for Fortinet FCP_FAC_AD-6.5 Certification?
How to Prepare for Fortinet FCP_FAC_AD-6.5 Certification?
 
一比一原版布拉德福德大学毕业证(bradford毕业证)如何办理
一比一原版布拉德福德大学毕业证(bradford毕业证)如何办理一比一原版布拉德福德大学毕业证(bradford毕业证)如何办理
一比一原版布拉德福德大学毕业证(bradford毕业证)如何办理
 
Jill Pizzola's Tenure as Senior Talent Acquisition Partner at THOMSON REUTERS...
Jill Pizzola's Tenure as Senior Talent Acquisition Partner at THOMSON REUTERS...Jill Pizzola's Tenure as Senior Talent Acquisition Partner at THOMSON REUTERS...
Jill Pizzola's Tenure as Senior Talent Acquisition Partner at THOMSON REUTERS...
 
官方认证美国旧金山州立大学毕业证学位证书案例原版一模一样
官方认证美国旧金山州立大学毕业证学位证书案例原版一模一样官方认证美国旧金山州立大学毕业证学位证书案例原版一模一样
官方认证美国旧金山州立大学毕业证学位证书案例原版一模一样
 
Lbs last rank 2023 9988kr47h4744j445.pdf
Lbs last rank 2023 9988kr47h4744j445.pdfLbs last rank 2023 9988kr47h4744j445.pdf
Lbs last rank 2023 9988kr47h4744j445.pdf
 
Gabrielle M. A. Sinaga Portfolio, Film Student (2024)
Gabrielle M. A. Sinaga Portfolio, Film Student (2024)Gabrielle M. A. Sinaga Portfolio, Film Student (2024)
Gabrielle M. A. Sinaga Portfolio, Film Student (2024)
 
在线制作加拿大萨省大学毕业证文凭证书实拍图原版一模一样
在线制作加拿大萨省大学毕业证文凭证书实拍图原版一模一样在线制作加拿大萨省大学毕业证文凭证书实拍图原版一模一样
在线制作加拿大萨省大学毕业证文凭证书实拍图原版一模一样
 

dsa pdf.pdf

  • 1. Data Structures (KCS-301) Searching Lecture W3.L1 Department of Computer Science and Engineering
  • 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 = 1 to n) { if(a[i] == item) return i; } If(i==n+1) Print: “Not Found”;
  • 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 •If item is present at the last or item is not present at all. •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. Pseudo Code - Iterative Procedure binary_search A ← sorted array n ← size of array x ← value to be searched lowerBound = 1 upperBound = n Do { Mid = (lowerBound+upperBound)/2; if(A[Mid] = = x) break; else if (x < A[Mid]) upperBound = Mid – 1; else lowerBound = Mid + 1; }While(lowerBound < = upperBound); if(lowerBound < = upperBound) Print: “Element Found at Mid”; else Print: “Not Found”;
  • 24. Pseudo Code - Recursive Procedure binary_search A ← sorted array n ← size of array x ← value to be searched lowerBound = 1 upperBound = n BinarySearch(A[],lowerBound,upperBound,ITEM) { if(if(lowerBound > upperBound) return -1; Mid = (lowerBound+upperBound)/2; if(A[Mid] = = x) return Mid; else if (x < A[Mid]) BinarySearch(A[], lowerBound, Mid-1, ITEM); else BinarySearch(A[], Mid+1, upperBound, ITEM); }
  • 25. 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(log2 n) and worst case = O(log2 n). •Average case is also = O(log2 n).
  • 26. Indexed Sequential Search •In this searching method, first of all, an index file is created, that contains some specific group or division of required record when the index is obtained, then the partial indexing takes less time cause it is located in a specified group.
  • 27. Characteristics •In Indexed Sequential Search a sorted index is set aside in addition to the array. •Each element in the index points to a block of elements in the array or another expanded index. •The index is searched 1st then the array and guides the search in the array.
  • 28. Explanation by diagram “Indexed Sequential Search”: