• Like
CS215 - Lec 8  searching records
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

Published

 

Published in Education , Technology , Business
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
152
On SlideShare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
24
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Structure of Indexes Linear Searching Binary Searching Dr. Hussien M. Sharaf 2
  • 2. Dr. Hussien M. Sharaf 3 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 985 New record n
  • 3. Dr. Hussien M. Sharaf 4 Given the following file, build an index for company names. File contents: ID, Name, Phone, commentn 5, Google, 2133 7710, 5 branchesn 7, ITE, 24413 9900, 1 branchn 8, Microsoft, 2789 0054, 9 branchesn 9, IBM, 24413 9900, 5 branchesn 10, Apple Mac, 2567 9876, 8 branchesn
  • 4. Dr. Hussien M. Sharaf 5 Step:1 write down the offset of each record . Company Name , offset Google , 26 ITE ,60 Microsoft , 90 IBM ,127 Apple Mac , 158
  • 5. Dr. Hussien M. Sharaf 6 Step:2 Reorder the records by company name. Company Name , offset Apple Mac , 158 Google , 26 IBM ,127 ITE ,60 Microsoft , 90
  • 6. Dr. Hussien M. Sharaf 7 int LinearSearch(TargetName) ◦ For each element in Index collection: ◦ { If Index[i].CompanyName==TargetName { Return Index[i].offset } ◦ } ◦ Return -1 //not found
  • 7. Dr. Hussien M. Sharaf 8 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
  • 8. Dr. Hussien M. Sharaf 9 ◦ 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
  • 9. Dr. Hussien M. Sharaf 10 ◦ 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?
  • 10. Implement linear search on IndexesCollection class. Implement Binary search on IndexesCollection class. Dr. Hussien M. Sharaf 11