cs215 - lec 8 searching records
TRANSCRIPT
� Structure of Indexes
� Linear Searching
� Binary Searching
Dr. Hussien M.
Sharaf2
Dr. Hussien M.
Sharaf3
Structure of Indexes
� Indexes must be sorted on ascending or descending order with respect to a (one or more ) field(s).
CompanyName offset
Google 211Record1
\n
\n
IBM 0Record2 \n
ITE 643Record3 \n
Microsoft 462Record4 \n
Apple Mac 985New
record\n
Dr. Hussien M.
Sharaf4
Given the following file, build an index for company names.
File contents:
ID, Name, Phone, comment\n
5, Google, 2133 7710, 5 branches\n
7, ITE, 24413 9900, 1 branch\n
8, Microsoft, 2789 0054, 9 branches\n
9, IBM, 24413 9900, 5 branches\n
10, Apple Mac, 2567 9876, 8 branches\n
Dr. Hussien M.
Sharaf5
Step:1 write down the offset of each record .
Company Name , offset
Google , 26
ITE ,60
Microsoft , 90
IBM ,127
Apple Mac , 158
Dr. Hussien M.
Sharaf6
Step:2 Reorder the records by company name.
Company Name , offset
Apple Mac , 158
Google , 26
IBM ,127
ITE ,60
Microsoft , 90
Dr. Hussien M.
Sharaf7
� int LinearSearch(TargetName)◦ For each element in Index collection:
◦ {� If Index[i].CompanyName==TargetName
� {
� Return Index[i].offset
� }
◦ }
◦ Return -1 //not found
Dr. Hussien M.
Sharaf8
� int BinarySearch(TargetName)
� { int low=0,high=Index.size();
� Midpoint=(high+low)/2;
� while (low<=high)◦ {CurrentRecordIndex=Index[Midpoint];
◦ If (CurrentRecordIndex.CompanyName==TargetN
ame) � Return CurrentRecordIndex.offset
Dr. Hussien M.
Sharaf9
◦ If (CurrentRecordIndex.CompanyName==TargetName) � Return CurrentRecordIndex.offset
◦ If (CurrentRecordIndex.CompanyName<=Targetname)
� Search in the right half: low= Midpoint+1
◦ Else
� Search in the left half: high= Midpoint-1
◦ } //end while
◦ Return -1 //not found
Dr. Hussien M.
Sharaf10
◦ Searching should be on a collection loaded in main memory
◦ Binary search O(logn) is faster than Linear search O(n)
◦ The penalty of using Binary search is that the collection must be sorted.
◦ Is this a problem for an Index?
� Implement linear search on IndexesCollection class.
� Implement Binary search on IndexesCollectionclass.
Dr. Hussien M.
Sharaf11