ps9-slides comparison of searching methods unsorted sorted hashed 6240 11203 03495 2801 21690 1 4731...

13
PS9- Slides Comparison of Searching Methods Unsorted Sorted Hashed 6240 1 1203 0 3495 2801 2 1690 1 4731 3 1731 2 7887 4 1973 3 2143 5 2143 4 7416 8776 6 2777 5 1731 9703 7 2801 6 1973 3522 8 3495 7 5116 9 3522 8 8311 5799 10 3619 9 9027 11 4002 10 8610 1690 12 4615 11 6872 4002 13 4731 12 3619 6114 14 4997 13 7128 4615 15 5116 14 3522 9703 8311 16 5708 15 1203 7416 17 5799 16 6240 8229 18 6021 17 4997 1731 19 6114 18 2777 4615 1203 20 6240 19 6021 21 6872 20 1690 8273 22 7128 21 2143 8610 23 7416 22 5708 2777 24 7887 23 9897 6872 25 8229 24 5116 1973 26 8273 25 8229 7128 27 8311 26 8273 9897 28 8610 27 5799 3619 29 8776 28 5708 30 9027 29 2801 3495 31 9703 30 4997 32 9897 31 9027 32 6114 33 34 8776 35 4731 36 7887 37 38 6021 39 40 41 42 4002

Upload: mary-blair

Post on 30-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: PS9-Slides Comparison of Searching Methods Unsorted Sorted Hashed 6240 11203 03495 2801 21690 1 4731 31731 2 7887 41973 3 2143 52143 47416 8776 62777 51731

PS9-SlidesComparison of Searching Methods

Unsorted Sorted Hashed

6240 1 1203 0 3495 •2801 2 1690 1 • 4731 3 1731 2 • 7887 4 1973 3 • 2143 5 2143 4 7416 •8776 6 2777 5 1731 •9703 7 2801 6 1973 •3522 8 3495 7 • 5116 9 3522 8 8311 •5799 10 3619 9 •9027 11 4002 10 8610 •1690 12 4615 11 6872 •4002 13 4731 12 3619 •6114 14 4997 13 7128 •4615 15 5116 14 3522 9703 •8311 16 5708 15 1203 •7416 17 5799 16 6240 •8229 18 6021 17 4997 •1731 19 6114 18 2777 4615 •1203 20 6240 19 6021 21 6872 20 1690 •8273 22 7128 21 2143 •8610 23 7416 22 5708 •2777 24 7887 23 9897 •6872 25 8229 24 5116 •1973 26 8273 25 8229 •7128 27 8311 26 8273 •9897 28 8610 27 5799 •3619 29 8776 28 •5708 30 9027 29 2801 •3495 31 9703 30 •4997 32 9897 31 9027 •

32 6114 •33 •34 8776 •35 4731 •36 7887 •37 •38 6021 •39 •40 •41 •42 4002 •

Page 2: PS9-Slides Comparison of Searching Methods Unsorted Sorted Hashed 6240 11203 03495 2801 21690 1 4731 31731 2 7887 41973 3 2143 52143 47416 8776 62777 51731

PS9-SlidesModulo 43 Table

0 43 86 129 172 1 44 87 130 173 2 45 88 131 174 3 46 89 132 175 4 47 90 133 176 5 48 91 134 177 6 49 92 135 178 7 50 93 136 179 8 51 94 137 180 9 52 95 138 18110 53 96 139 18211 54 97 140 18312 55 98 141 18413 56 99 142 18514 57 100 143 18615 58 101 144 18716 59 102 145 18817 60 103 146 18918 61 104 147 19019 62 105 148 19120 63 106 149 19221 64 107 150 19322 65 108 151 19423 66 109 152 19524 67 110 153 19625 68 111 154 19726 69 112 155 19827 70 113 156 19928 71 114 157 20029 72 115 158 20130 73 116 159 20231 74 117 160 20332 75 118 161 20433 76 119 162 20534 77 120 163 20635 78 121 164 20736 79 122 165 20837 80 123 166 20938 81 124 167 21039 82 125 168 21140 83 126 169 21241 84 127 170 21342 85 128 171 214

Page 3: PS9-Slides Comparison of Searching Methods Unsorted Sorted Hashed 6240 11203 03495 2801 21690 1 4731 31731 2 7887 41973 3 2143 52143 47416 8776 62777 51731

PS9-Slides A hash Function for Integers.

1. break the long integer up into 3 pieces of 3 digits each - add a leading 0, if necessary, to make the 9 digits.

2. Add the three pieces;

3. Find the remainder of the division of this sum by M (= 67, in this case).

Ex. 012053488-> 012

053

488

------

553 -> 553 % 67 = 17

Page 4: PS9-Slides Comparison of Searching Methods Unsorted Sorted Hashed 6240 11203 03495 2801 21690 1 4731 31731 2 7887 41973 3 2143 52143 47416 8776 62777 51731

PS9-Slides Distribution of h(x):

x

-------------------------------------------------------------------0123456789111111111122222222223333333333444444444455555555556666666 012345678901234567890123456789012345678901234567890123456

Page 5: PS9-Slides Comparison of Searching Methods Unsorted Sorted Hashed 6240 11203 03495 2801 21690 1 4731 31731 2 7887 41973 3 2143 52143 47416 8776 62777 51731

PS9-Slides

Table ADT

void Initialize Table(Table T);bool isNullEntry(TableEntry E);bool isEmpty(Table T, int probe);void InsertTable(Table T, TableEntry newitem);TableEntry RetrieveTable(Table T, KeyType *target)

Page 6: PS9-Slides Comparison of Searching Methods Unsorted Sorted Hashed 6240 11203 03495 2801 21690 1 4731 31731 2 7887 41973 3 2143 52143 47416 8776 62777 51731

PS9-Slides#define M 67

typedef char KeyType;

typedef struct{

char Name[25];

int Number;

/* there could be additional information here */

} President;

typedef struct{

KeyType Key[7];

President Info;

} TableEntry;

typedef TableEntry Table[M]

0

1

2

3

4

5

6

:

:

:

i

:

:

:

:

M-1

TableEntry

Table T

Key or

Target

h(Key)

Hash Table Definition

Page 7: PS9-Slides Comparison of Searching Methods Unsorted Sorted Hashed 6240 11203 03495 2801 21690 1 4731 31731 2 7887 41973 3 2143 52143 47416 8776 62777 51731

PS9-Slides

George Washington

John Adams

Thomas Jefferson

James Madison

James Monroe

John Quincy Adams

Andrew Jackson

Martin Van_Buren

William H. Harrison

John Tyler

:

0

1

2

:

i

:

M-1

h(Key)

Use HashTable to

retrieve information

MakeHashTable(T)

"PresName.dat"

Table T

Table Entry

(Key and

President)

target

President Hash Table

Page 8: PS9-Slides Comparison of Searching Methods Unsorted Sorted Hashed 6240 11203 03495 2801 21690 1 4731 31731 2 7887 41973 3 2143 52143 47416 8776 62777 51731

PS9-Slides Appendix D: President Names File (PresName.dat)

George WashingtonJohn AdamsThomas JeffersonJames MadisonJames MonroeJohn Quincy AdamsAndrew JacksonMartin Van_BurenWilliam H. HarrisonJohn TylerJames Knox PolkZachary TaylorMillard FillmoreFranklin PierceJames BuchananAbraham LincolnAndrew JohnsonUlysses Simpson GrantRutherford B. HayesJames Abram GarfieldChester Alan ArthurGrover ClevelandBenjamin HarrisonWilliam McKinleyTheodore RooseveltWilliam Howard TaftWoodrow WilsonWarren G. HardingCalvin CoolidgeHerbert Clark HooverFranklin D. RooseveltHarry S. TrumanDwight D. EisenhowerJohn F. KennedyLyndon Baines JohnsonRichard Milhous NixonGerald R. FordJimmy CarterRonald ReaganGeorge BushBill Clinton

Page 9: PS9-Slides Comparison of Searching Methods Unsorted Sorted Hashed 6240 11203 03495 2801 21690 1 4731 31731 2 7887 41973 3 2143 52143 47416 8776 62777 51731

PS9-Slides

int h(char *c){

int i;

unsigned s=0;

for(i=0; i < 6; i++){

s += (i+1)*c[i];

}

return s%67;

}

washgx

w - 119 * 1 = 119

a - 97 * 2 = 194

s - 115 * 3 = 345

h - 104 * 4 = 416

g - 103 * 5 = 515

x - 120 * 6 = 720

2309 % 67 = 31

2309

Hash Function for President Keys

Page 10: PS9-Slides Comparison of Searching Methods Unsorted Sorted Hashed 6240 11203 03495 2801 21690 1 4731 31731 2 7887 41973 3 2143 52143 47416 8776 62777 51731

PS9-Slides Experimental Values for Hash Function Distribution

Problem 1. Distribution of h(x) for Integers in IntKeys.dat

x

-------------------------------------------------------------------0123456789111111111122222222223333333333444444444455555555556666666 012345678901234567890123456789012345678901234567890123456

Problem 2. Distribution of h(x) for String Keys Developed from PresName.dat

x

-------------------------------------------------------------------0123456789111111111122222222223333333333444444444455555555556666666 012345678901234567890123456789012345678901234567890123456

Page 11: PS9-Slides Comparison of Searching Methods Unsorted Sorted Hashed 6240 11203 03495 2801 21690 1 4731 31731 2 7887 41973 3 2143 52143 47416 8776 62777 51731

PS9-Slides Experimental Values for Hash Function Distribution

Problem 3. Distribution of of Table Entries for President Hash Tables.

Table Entry Locations: Linear Probing.

x x

-------------------------------------------------------------------0123456789111111111122222222223333333333444444444455555555556666666 012345678901234567890123456789012345678901234567890123456

Table Entry Locations: Double Hashing.

x x

-------------------------------------------------------------------0123456789111111111122222222223333333333444444444455555555556666666 012345678901234567890123456789012345678901234567890123456

Page 12: PS9-Slides Comparison of Searching Methods Unsorted Sorted Hashed 6240 11203 03495 2801 21690 1 4731 31731 2 7887 41973 3 2143 52143 47416 8776 62777 51731

PS9-Slides

Using Defines to Control Program Output

#define _STUDY

: :

#ifdef _STUDY printf(" Insert %s:\n",E.key);#endif // _STUDY

:

Page 13: PS9-Slides Comparison of Searching Methods Unsorted Sorted Hashed 6240 11203 03495 2801 21690 1 4731 31731 2 7887 41973 3 2143 52143 47416 8776 62777 51731

PS9-Slides

Using Defines to Select Functions (Macros)

#define EQ(a,b) (!strcmp((a),(b)))#define EQ(a,b) ((a) == (b))

:

if( EQ(E.Key,EmptyKey)) return true;else return false;

: