+ All Categories
Home > Education > CS215 - Lec 8 searching records

CS215 - Lec 8 searching records

Date post: 29-Jun-2015
Category:
Upload: arab-open-university-and-cairo-university
View: 185 times
Download: 2 times
Share this document with a friend
Popular Tags:
12
Transcript
Page 1: CS215 - Lec 8  searching records
Page 2: CS215 - Lec 8  searching records

� Structure of Indexes

� Linear Searching

� Binary Searching

Dr. Hussien M.

Sharaf2

Page 3: CS215 - Lec 8  searching records

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

Page 4: CS215 - Lec 8  searching records

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

Page 5: CS215 - Lec 8  searching records

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

Page 6: CS215 - Lec 8  searching records

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

Page 7: CS215 - Lec 8  searching records

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

Page 8: CS215 - Lec 8  searching records

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

Page 9: CS215 - Lec 8  searching records

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

Page 10: CS215 - Lec 8  searching records

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?

Page 11: CS215 - Lec 8  searching records

� Implement linear search on IndexesCollection class.

� Implement Binary search on IndexesCollectionclass.

Dr. Hussien M.

Sharaf11

Page 12: CS215 - Lec 8  searching records

Recommended