SlideShare a Scribd company logo
Searching 
algorithm 
Trupti Agrawal 1
Searching Algorithms 
• Necessary components to search a list of data 
– Array containing the list 
– Length of the list 
– Item for which you are searching 
• After search completed 
– If item found, report “success,” return location in array 
– If item not found, report “not found” or “failure” 
Trupti Agrawal 2
Searching Algorithms (Cont’d) 
• Suppose that you want to determine whether 27 is in the list 
• First compare 27 with list[0]; that is, compare 27 with 35 
• Because list[0] ≠ 27, you then compare 27 with list[1] 
• Because list[1] ≠ 27, you compare 27 with the next element in 
the list 
• Because list[2] = 27, the search stops 
• This search is successful! 
FigTruuprtei A g1ra: wAalrray list with seven (07) elements 3
Searching Algorithms (Cont’d) 
Let’s now search for 10 
The search starts at the first element in the list; that is, at 
list[0] 
Proceeding as before, we see that this time the search item, 
which is 10, is compared with every item in the list 
Eventually, no more data is left in the list to compare with 
the search item; this is an unsuccessful search 
Trupti Agrawal 4
Sequential Search Algorithm 
The previous could be further reduced to: 
public static int linSearch(int[] list, int listLength, int key) { 
int loc; 
boolean found = false; 
for(int loc = 0; loc < listLength; loc++) { 
if(list[loc] == key) { 
found = true; 
break; 
} 
} 
if(found) 
return loc; 
else 
return -1; 
} 
Trupti Agrawal 5
Sequential Search Algorithm (Cont’d) 
public static int linSearch(int[] list, int listLength, int key) { 
int loc; 
for(int loc = 0; loc < listLength; loc++) { 
if(list[loc] == key) 
return loc; 
} 
return -1; 
} 
Trupti Agrawal 6
Sequential Search Algorithm (Cont’d) 
• Using a while (or a for) loop, the definition of the method 
seqSearch can also be written without the break statement as: 
public static int linSearch(int[] list, int listLength, int key) { 
int loc = 0; 
boolean found = false; 
while(loc < listLength && !found) { 
if(list[loc] == key) 
found = true; 
else 
loc++ 
} 
if(found) 
return loc; 
else 
return -1; 
} 
Trupti Agrawal 7
Performance of the Sequential Search 
• Suppose that the first element in the array list contains the 
variable key, then we have performed one comparison to find 
the key. 
• Suppose that the second element in the array list contains the 
variable key, then we have performed two comparisons to find 
the key. 
• Carry on the same analysis till the key is contained in the last 
element of the array list. In this case, we have performed N 
comparisons (N is the size of the array list) to find the key. 
• Finally if the key is NOT in the array list, then we would have 
performed N comparisons and the key is NOT found and we 
would return -1. 
Trupti Agrawal 8
Performance of the Sequential Search (Cont’d) 
• Therefore, the best case is: 1 
• And, the worst case is: N 
• The average case is: 
1 + 2 + 3 + …..+ N + N 
N+1 
Best case 
Average Number of 
Comparisons 
Worst case and key found at the end of 
the array list! 
Worst case and key is NOT found! 
= 
Number of possible cases 
Trupti Agrawal 9
Binary Search Algorithm 
Can only be performed on a sorted list !!! 
Uses divide and conquer technique to search list 
Trupti Agrawal 10
Binary Search Algorithm (Cont’d) 
Search item is compared with middle element of list 
If search item < middle element of list, search is restricted 
to first half of the list 
If search item > middle element of list, search second half 
of the list 
If search item = middle element, search is complete 
Trupti Agrawal 11
Binary Search Algorithm (Cont’d) 
• Determine whether 75 is in the list 
Figure 2: Array list with twelve (12) elements 
Figure 3: Search list, list[0] … list[11] 
Trupti Agrawal 12
Binary Search Algorithm (Cont’d) 
Figure 4: Search list, list[6] … list[11] 
Trupti Agrawal 13
Binary Search Algorithm (Cont’d) 
public static int binarySearch(int[] list, int listLength, int key) { 
int first = 0, last = listLength - 1; 
int mid; 
boolean found = false; 
while (first <= last && !found) { 
mid = (first + last) / 2; 
if (list[mid] == key) 
found = true; 
else 
if(list[mid] > key) 
last = mid - 1; 
else 
first = mid + 1; 
} 
if (found) 
return mid; 
else 
return –1; 
} //end binarySearch 
Trupti Agrawal 14
Binary Search Algorithm (Cont’d) 
Figure 5: Sorted list for binary search 
key = 89 
key = 34 
Trupti Agrawal 15
Binary Search Algorithm (Cont’d) 
key = 22 
Figure 6: Sorted list for binary search 
Trupti Agrawal 16
Indexed Search 
• Indexes: Data structures to organize records to 
optimize certain kinds of retrieval operations. 
o Speed up searches for a subset of records, based on values in 
certain (“search key”) fields 
o Updates are much faster than in sorted files. 
Trupti Agrawal 17
Alternatives for Data Entry k* in 
Index 
Data Entry : Records stored in index file 
Given search key value k, provide for efficient retrieval of 
all data entries k* with value k. 
In a data entry k* , alternatives include that we can store: 
 alternative 1: Full data record with key value k, or 
 alternative 2: <k, rid of data record with search key value k>, or 
 alternative 3: <k, list of rids of data records with search key k> 
Choice of above 3 alternative data entries is orthogonal to indexing 
technique used to locate data entries. 
 Example indexing techniques: B+ trees, hash-based structures, etc. 
Trupti Agrawal 18
Alternatives for Data Entries 
Alternative 1: Full data record with key value k 
Index structure is file organization for data records (instead 
of a Heap file or sorted file). 
At most one index on a given collection of data records 
can use Alternative 1. 
Otherwise, data records are duplicated, leading to 
redundant storage and potential inconsistency. 
If data records are very large, this implies size of auxiliary 
information in index is also large. 
Trupti Agrawal 19
Alternatives for Data Entries 
Alternatives 2 (<k, rid>) and 3 (<k, list-of-rids>): 
Data entries typically much smaller than data records. 
Comparison: 
Both better than Alternative 1 with large data records, 
especially if search keys are small. 
Alternative 3 more compact than Alternative 2, 
but leads to variable sized data entries even if search keys 
are of fixed length. 
Trupti Agrawal 20
Index Classification 
Clustered vs. unclustered index : 
If order of data records is the same as, or `close to’, 
order of data entries, then called clustered index. 
Trupti Agrawal 21
Index Clustered vs Unclustered 
Observation 1: 
Alternative 1 implies clustered. True ? 
Observation 2: 
In practice, clustered also implies Alternative 1 (since 
sorted files are rare). 
Observation 3: 
A file can be clustered on at most one search key. 
Observation 4: 
Cost of retrieving data records through index varies 
greatly based on whether index is clustered or not !! 
Trupti Agrawal 22
Index Clustered vs Unclustered 
Observation 1: 
Alternative 1 implies clustered. True ? 
Observation 2: 
In practice, clustered also implies Alternative 1 (since 
sorted files are rare). 
Observation 3: 
A file can be clustered on at most one search key. 
Observation 4: 
Cost of retrieving data records through index varies 
greatly based on whether index is clustered or not !! 
Trupti Agrawal 23
Clustered vs. Unclustered Index 
Index entries 
CLUSTERED direct search for 
UNCLUSTERED 
data entries 
Data entries 
(Index File) 
(Data file) 
Data Records 
Data entries 
Data Records 
Suppose Alternative (2) is used for data entries. 
Trupti Agrawal 24
Clustered vs. Unclustered Index 
Use Alternative (2) for data entries 
Data records are stored in Heap file. 
To build clustered index, first sort the Heap file 
Overflow pages may be needed for inserts. 
Thus, order of data recs is close to (not identical to) 
sort order. 
Index entries 
CLUSTERED direct search for 
UNCLUSTERED 
data entries 
Data entries 
(Index File) 
(Data file) 
Data Records 
Data entries 
Data Records 
Trupti Agrawal 25
Summary of Index Search 
Many alternative file organizations exist, each appropriate in 
some situation. 
If selection queries are frequent, sorting the file or building an 
index is important. 
 Hash-based indexes only good for equality search. 
 Sorted files and tree-based indexes best for range search; also 
good for equality search. 
 Files rarely kept sorted in practice; B+ tree index is better. 
Index is a collection of data entries plus a way to quickly find 
entries with given key values. 
Trupti Agrawal 26
Summary of Index Search 
 Data entries can be : 
 actual data records, 
 <key, rid> pairs, or 
 <key, rid-list> pairs. 
 Can have several indexes on a given file of data 
records, each with a different search key. 
 Indexes can be classified as clustered vs. unclustered, 
 Differences have important consequences for 
utility/performance of query processing 
Trupti Agrawal 27
THANK YOU….. !!! 
Trupti Agrawal 28

More Related Content

What's hot

Lecture 3 data structures & algorithms - sorting techniques - http://techiem...
Lecture 3  data structures & algorithms - sorting techniques - http://techiem...Lecture 3  data structures & algorithms - sorting techniques - http://techiem...
Lecture 3 data structures & algorithms - sorting techniques - http://techiem...
Dharmendra Prasad
 
Sorting Techniques
Sorting TechniquesSorting Techniques
Sorting Techniques
Rafay Farooq
 
Binary search
Binary search Binary search
Binary search
Raghu nath
 
Lect11 Sorting
Lect11 SortingLect11 Sorting
Lect11 Sorting
ryokollll
 

What's hot (20)

Different types of Shoring Algorithms with Animation
Different types of Shoring Algorithms with AnimationDifferent types of Shoring Algorithms with Animation
Different types of Shoring Algorithms with Animation
 
Lecture 3 data structures & algorithms - sorting techniques - http://techiem...
Lecture 3  data structures & algorithms - sorting techniques - http://techiem...Lecture 3  data structures & algorithms - sorting techniques - http://techiem...
Lecture 3 data structures & algorithms - sorting techniques - http://techiem...
 
Sorting algorithm
Sorting algorithmSorting algorithm
Sorting algorithm
 
Sorting Techniques
Sorting TechniquesSorting Techniques
Sorting Techniques
 
Hub 102 - Lesson 5 - Algorithm: Sorting & Searching
Hub 102 - Lesson 5 - Algorithm: Sorting & SearchingHub 102 - Lesson 5 - Algorithm: Sorting & Searching
Hub 102 - Lesson 5 - Algorithm: Sorting & Searching
 
Data Structures- Part4 basic sorting algorithms
Data Structures- Part4 basic sorting algorithmsData Structures- Part4 basic sorting algorithms
Data Structures- Part4 basic sorting algorithms
 
Best,worst,average case .17581556 045
Best,worst,average case .17581556 045Best,worst,average case .17581556 045
Best,worst,average case .17581556 045
 
Sorting Algorithm
Sorting AlgorithmSorting Algorithm
Sorting Algorithm
 
Sorting
SortingSorting
Sorting
 
Binary search
Binary search Binary search
Binary search
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Lect11 Sorting
Lect11 SortingLect11 Sorting
Lect11 Sorting
 
Algorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching AlgorithmsAlgorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching Algorithms
 
stacks and queues for public
stacks and queues for publicstacks and queues for public
stacks and queues for public
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sorting
 
Quicksort Algorithm..simply defined through animations..!!
Quicksort Algorithm..simply defined through animations..!!Quicksort Algorithm..simply defined through animations..!!
Quicksort Algorithm..simply defined through animations..!!
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Insertion sort
Insertion sort Insertion sort
Insertion sort
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 

Viewers also liked

Presentation2
Presentation2Presentation2
Presentation2
ikelean
 
Publisher research
Publisher researchPublisher research
Publisher research
laura-illing
 
паспорт 9 б оош №15
паспорт 9   б оош №15паспорт 9   б оош №15
паспорт 9 б оош №15
olga0101
 

Viewers also liked (15)

Year of 13
Year of 13Year of 13
Year of 13
 
Presentation2
Presentation2Presentation2
Presentation2
 
Поговорим о различных версиях .NET
Поговорим о различных версиях .NETПоговорим о различных версиях .NET
Поговорим о различных версиях .NET
 
The Netherlands investment climate - Main tax features
The Netherlands investment climate - Main tax featuresThe Netherlands investment climate - Main tax features
The Netherlands investment climate - Main tax features
 
クラウド時代のインターネット接続
クラウド時代のインターネット接続クラウド時代のインターネット接続
クラウド時代のインターネット接続
 
Hd presentation
Hd presentationHd presentation
Hd presentation
 
Analysing magazines
Analysing magazinesAnalysing magazines
Analysing magazines
 
Roberts Zīle - Termiņuzturēšanās atļaujas: ekonomiskie, politiskie un sociāl...
Roberts Zīle -  Termiņuzturēšanās atļaujas: ekonomiskie, politiskie un sociāl...Roberts Zīle -  Termiņuzturēšanās atļaujas: ekonomiskie, politiskie un sociāl...
Roberts Zīle - Termiņuzturēšanās atļaujas: ekonomiskie, politiskie un sociāl...
 
Emergent Process Design
Emergent Process DesignEmergent Process Design
Emergent Process Design
 
Publisher research
Publisher researchPublisher research
Publisher research
 
Taller técnicas efectivas de cobranza
Taller técnicas efectivas de cobranzaTaller técnicas efectivas de cobranza
Taller técnicas efectivas de cobranza
 
Sept/Oct Lunenburg County SPCA Newsletter
Sept/Oct Lunenburg County SPCA NewsletterSept/Oct Lunenburg County SPCA Newsletter
Sept/Oct Lunenburg County SPCA Newsletter
 
Photograph
PhotographPhotograph
Photograph
 
паспорт 9 б оош №15
паспорт 9   б оош №15паспорт 9   б оош №15
паспорт 9 б оош №15
 
Jan-Feb 2015 Lunenburg County SPCA Newsletter
Jan-Feb 2015 Lunenburg County SPCA NewsletterJan-Feb 2015 Lunenburg County SPCA Newsletter
Jan-Feb 2015 Lunenburg County SPCA Newsletter
 

Similar to Searching algorithms

Similar to Searching algorithms (20)

Searching algorithms
Searching algorithmsSearching algorithms
Searching algorithms
 
Data Structures 8
Data Structures 8Data Structures 8
Data Structures 8
 
Chapter 14
Chapter 14Chapter 14
Chapter 14
 
Data structure unit I part B
Data structure unit I part BData structure unit I part B
Data structure unit I part B
 
IRJET- A Survey on Different Searching Algorithms
IRJET- A Survey on Different Searching AlgorithmsIRJET- A Survey on Different Searching Algorithms
IRJET- A Survey on Different Searching Algorithms
 
Algorithm and Programming (Searching)
Algorithm and Programming (Searching)Algorithm and Programming (Searching)
Algorithm and Programming (Searching)
 
Lecture3b searching
Lecture3b searchingLecture3b searching
Lecture3b searching
 
9780324782011_PPT_ch09.ppt
9780324782011_PPT_ch09.ppt9780324782011_PPT_ch09.ppt
9780324782011_PPT_ch09.ppt
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
 
Unit08 dbms
Unit08 dbmsUnit08 dbms
Unit08 dbms
 
Analysis of Algorithm - Binary Search.pptx
Analysis of Algorithm - Binary Search.pptxAnalysis of Algorithm - Binary Search.pptx
Analysis of Algorithm - Binary Search.pptx
 
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTINGUnit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
 
lect 2-DS ALGO(online).pdf
lect 2-DS  ALGO(online).pdflect 2-DS  ALGO(online).pdf
lect 2-DS ALGO(online).pdf
 
Chap10
Chap10Chap10
Chap10
 
Chapter 4: basic search algorithms data structure
Chapter 4: basic search algorithms data structureChapter 4: basic search algorithms data structure
Chapter 4: basic search algorithms data structure
 
Topic11 sortingandsearching
Topic11 sortingandsearchingTopic11 sortingandsearching
Topic11 sortingandsearching
 
arrays in c
arrays in carrays in c
arrays in c
 
Searching
SearchingSearching
Searching
 
Algorithms.pptx
Algorithms.pptxAlgorithms.pptx
Algorithms.pptx
 
Unit viii searching and hashing
Unit   viii searching and hashing Unit   viii searching and hashing
Unit viii searching and hashing
 

Recently uploaded

Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Po-Chuan Chen
 
Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training Report
Avinash Rai
 

Recently uploaded (20)

Application of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matricesApplication of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matrices
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptBasic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
 
B.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdfB.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdf
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx
 
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
 
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptxslides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
 
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
Operations Management - Book1.p  - Dr. Abdulfatah A. SalemOperations Management - Book1.p  - Dr. Abdulfatah A. Salem
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
 
Salient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxSalient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptx
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training Report
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
 

Searching algorithms

  • 2. Searching Algorithms • Necessary components to search a list of data – Array containing the list – Length of the list – Item for which you are searching • After search completed – If item found, report “success,” return location in array – If item not found, report “not found” or “failure” Trupti Agrawal 2
  • 3. Searching Algorithms (Cont’d) • Suppose that you want to determine whether 27 is in the list • First compare 27 with list[0]; that is, compare 27 with 35 • Because list[0] ≠ 27, you then compare 27 with list[1] • Because list[1] ≠ 27, you compare 27 with the next element in the list • Because list[2] = 27, the search stops • This search is successful! FigTruuprtei A g1ra: wAalrray list with seven (07) elements 3
  • 4. Searching Algorithms (Cont’d) Let’s now search for 10 The search starts at the first element in the list; that is, at list[0] Proceeding as before, we see that this time the search item, which is 10, is compared with every item in the list Eventually, no more data is left in the list to compare with the search item; this is an unsuccessful search Trupti Agrawal 4
  • 5. Sequential Search Algorithm The previous could be further reduced to: public static int linSearch(int[] list, int listLength, int key) { int loc; boolean found = false; for(int loc = 0; loc < listLength; loc++) { if(list[loc] == key) { found = true; break; } } if(found) return loc; else return -1; } Trupti Agrawal 5
  • 6. Sequential Search Algorithm (Cont’d) public static int linSearch(int[] list, int listLength, int key) { int loc; for(int loc = 0; loc < listLength; loc++) { if(list[loc] == key) return loc; } return -1; } Trupti Agrawal 6
  • 7. Sequential Search Algorithm (Cont’d) • Using a while (or a for) loop, the definition of the method seqSearch can also be written without the break statement as: public static int linSearch(int[] list, int listLength, int key) { int loc = 0; boolean found = false; while(loc < listLength && !found) { if(list[loc] == key) found = true; else loc++ } if(found) return loc; else return -1; } Trupti Agrawal 7
  • 8. Performance of the Sequential Search • Suppose that the first element in the array list contains the variable key, then we have performed one comparison to find the key. • Suppose that the second element in the array list contains the variable key, then we have performed two comparisons to find the key. • Carry on the same analysis till the key is contained in the last element of the array list. In this case, we have performed N comparisons (N is the size of the array list) to find the key. • Finally if the key is NOT in the array list, then we would have performed N comparisons and the key is NOT found and we would return -1. Trupti Agrawal 8
  • 9. Performance of the Sequential Search (Cont’d) • Therefore, the best case is: 1 • And, the worst case is: N • The average case is: 1 + 2 + 3 + …..+ N + N N+1 Best case Average Number of Comparisons Worst case and key found at the end of the array list! Worst case and key is NOT found! = Number of possible cases Trupti Agrawal 9
  • 10. Binary Search Algorithm Can only be performed on a sorted list !!! Uses divide and conquer technique to search list Trupti Agrawal 10
  • 11. Binary Search Algorithm (Cont’d) Search item is compared with middle element of list If search item < middle element of list, search is restricted to first half of the list If search item > middle element of list, search second half of the list If search item = middle element, search is complete Trupti Agrawal 11
  • 12. Binary Search Algorithm (Cont’d) • Determine whether 75 is in the list Figure 2: Array list with twelve (12) elements Figure 3: Search list, list[0] … list[11] Trupti Agrawal 12
  • 13. Binary Search Algorithm (Cont’d) Figure 4: Search list, list[6] … list[11] Trupti Agrawal 13
  • 14. Binary Search Algorithm (Cont’d) public static int binarySearch(int[] list, int listLength, int key) { int first = 0, last = listLength - 1; int mid; boolean found = false; while (first <= last && !found) { mid = (first + last) / 2; if (list[mid] == key) found = true; else if(list[mid] > key) last = mid - 1; else first = mid + 1; } if (found) return mid; else return –1; } //end binarySearch Trupti Agrawal 14
  • 15. Binary Search Algorithm (Cont’d) Figure 5: Sorted list for binary search key = 89 key = 34 Trupti Agrawal 15
  • 16. Binary Search Algorithm (Cont’d) key = 22 Figure 6: Sorted list for binary search Trupti Agrawal 16
  • 17. Indexed Search • Indexes: Data structures to organize records to optimize certain kinds of retrieval operations. o Speed up searches for a subset of records, based on values in certain (“search key”) fields o Updates are much faster than in sorted files. Trupti Agrawal 17
  • 18. Alternatives for Data Entry k* in Index Data Entry : Records stored in index file Given search key value k, provide for efficient retrieval of all data entries k* with value k. In a data entry k* , alternatives include that we can store:  alternative 1: Full data record with key value k, or  alternative 2: <k, rid of data record with search key value k>, or  alternative 3: <k, list of rids of data records with search key k> Choice of above 3 alternative data entries is orthogonal to indexing technique used to locate data entries.  Example indexing techniques: B+ trees, hash-based structures, etc. Trupti Agrawal 18
  • 19. Alternatives for Data Entries Alternative 1: Full data record with key value k Index structure is file organization for data records (instead of a Heap file or sorted file). At most one index on a given collection of data records can use Alternative 1. Otherwise, data records are duplicated, leading to redundant storage and potential inconsistency. If data records are very large, this implies size of auxiliary information in index is also large. Trupti Agrawal 19
  • 20. Alternatives for Data Entries Alternatives 2 (<k, rid>) and 3 (<k, list-of-rids>): Data entries typically much smaller than data records. Comparison: Both better than Alternative 1 with large data records, especially if search keys are small. Alternative 3 more compact than Alternative 2, but leads to variable sized data entries even if search keys are of fixed length. Trupti Agrawal 20
  • 21. Index Classification Clustered vs. unclustered index : If order of data records is the same as, or `close to’, order of data entries, then called clustered index. Trupti Agrawal 21
  • 22. Index Clustered vs Unclustered Observation 1: Alternative 1 implies clustered. True ? Observation 2: In practice, clustered also implies Alternative 1 (since sorted files are rare). Observation 3: A file can be clustered on at most one search key. Observation 4: Cost of retrieving data records through index varies greatly based on whether index is clustered or not !! Trupti Agrawal 22
  • 23. Index Clustered vs Unclustered Observation 1: Alternative 1 implies clustered. True ? Observation 2: In practice, clustered also implies Alternative 1 (since sorted files are rare). Observation 3: A file can be clustered on at most one search key. Observation 4: Cost of retrieving data records through index varies greatly based on whether index is clustered or not !! Trupti Agrawal 23
  • 24. Clustered vs. Unclustered Index Index entries CLUSTERED direct search for UNCLUSTERED data entries Data entries (Index File) (Data file) Data Records Data entries Data Records Suppose Alternative (2) is used for data entries. Trupti Agrawal 24
  • 25. Clustered vs. Unclustered Index Use Alternative (2) for data entries Data records are stored in Heap file. To build clustered index, first sort the Heap file Overflow pages may be needed for inserts. Thus, order of data recs is close to (not identical to) sort order. Index entries CLUSTERED direct search for UNCLUSTERED data entries Data entries (Index File) (Data file) Data Records Data entries Data Records Trupti Agrawal 25
  • 26. Summary of Index Search Many alternative file organizations exist, each appropriate in some situation. If selection queries are frequent, sorting the file or building an index is important.  Hash-based indexes only good for equality search.  Sorted files and tree-based indexes best for range search; also good for equality search.  Files rarely kept sorted in practice; B+ tree index is better. Index is a collection of data entries plus a way to quickly find entries with given key values. Trupti Agrawal 26
  • 27. Summary of Index Search  Data entries can be :  actual data records,  <key, rid> pairs, or  <key, rid-list> pairs.  Can have several indexes on a given file of data records, each with a different search key.  Indexes can be classified as clustered vs. unclustered,  Differences have important consequences for utility/performance of query processing Trupti Agrawal 27
  • 28. THANK YOU….. !!! Trupti Agrawal 28