1 tables & file © dave bockus. 2 binary search recursive int bsearch(tabletype t, keytype key,...
TRANSCRIPT
![Page 1: 1 Tables & File © Dave Bockus. 2 Binary Search Recursive int Bsearch(TableType T, KeyType key, int lt, int rt) { int mid; mid = (lt+rt)/2; if (lt>rt)](https://reader036.vdocuments.net/reader036/viewer/2022062312/5519b1f055034660578b461d/html5/thumbnails/1.jpg)
1
Tables & File
© Dave Bockus
![Page 2: 1 Tables & File © Dave Bockus. 2 Binary Search Recursive int Bsearch(TableType T, KeyType key, int lt, int rt) { int mid; mid = (lt+rt)/2; if (lt>rt)](https://reader036.vdocuments.net/reader036/viewer/2022062312/5519b1f055034660578b461d/html5/thumbnails/2.jpg)
2
Binary SearchRecursive
int Bsearch(TableType T, KeyType key, int lt, int rt) {
int mid;
mid = (lt+rt)/2;
if (lt>rt) //Search unsuccessful
return 0;
else if (T[mid]==key) //Search successful
return mid;
else if (T[mid]>key) //Search item < mid
return Bsearch(T,key,lt,mid-1);
else //Search item > mid
return Bsearch(T,key,mid+1,rt);
}
![Page 3: 1 Tables & File © Dave Bockus. 2 Binary Search Recursive int Bsearch(TableType T, KeyType key, int lt, int rt) { int mid; mid = (lt+rt)/2; if (lt>rt)](https://reader036.vdocuments.net/reader036/viewer/2022062312/5519b1f055034660578b461d/html5/thumbnails/3.jpg)
3
public static int binarySearch( Comparable [ ] a, Comparable x ) { int low = 0, high = a.length - 1; int mid; while( low <= high ){ mid = ( low + high ) / 2; if( a[ mid ].compareTo( x ) < 0 ) low = mid + 1; // Search lower partition else if( a[ mid ].compareTo( x ) > 0 ) high = mid - 1; // Search upper partition else return mid; // Found } return NOT_FOUND; // NOT_FOUND is defined as -1}
Binary SearchIterative
![Page 4: 1 Tables & File © Dave Bockus. 2 Binary Search Recursive int Bsearch(TableType T, KeyType key, int lt, int rt) { int mid; mid = (lt+rt)/2; if (lt>rt)](https://reader036.vdocuments.net/reader036/viewer/2022062312/5519b1f055034660578b461d/html5/thumbnails/4.jpg)
4
51
Binary Search Example
1 4 6 7 8 9 10 13 16 20
1 2 3 4 5 6 7 8 9 10
lt = rt = mid = 1 10
2 lt = rt = mid = 1 2
3 lt = rt = mid =
(Array[5] = 8) 7 so… continue(Array[2] = 4) 7 so… continue
4
43 3
(Array[3] = 6) 7 so ... continue
4 lt = rt = mid = 4
(Array[4] = 7) = 7 Found!!!!
4 4
![Page 5: 1 Tables & File © Dave Bockus. 2 Binary Search Recursive int Bsearch(TableType T, KeyType key, int lt, int rt) { int mid; mid = (lt+rt)/2; if (lt>rt)](https://reader036.vdocuments.net/reader036/viewer/2022062312/5519b1f055034660578b461d/html5/thumbnails/5.jpg)
5
Hashing - Chaining
88 75
64
28 41
38
62
3
49
93 54
![Page 6: 1 Tables & File © Dave Bockus. 2 Binary Search Recursive int Bsearch(TableType T, KeyType key, int lt, int rt) { int mid; mid = (lt+rt)/2; if (lt>rt)](https://reader036.vdocuments.net/reader036/viewer/2022062312/5519b1f055034660578b461d/html5/thumbnails/6.jpg)
6
Hashing - Open Addressing
88
75
64
28
41
38
62
3
49
93
54
![Page 7: 1 Tables & File © Dave Bockus. 2 Binary Search Recursive int Bsearch(TableType T, KeyType key, int lt, int rt) { int mid; mid = (lt+rt)/2; if (lt>rt)](https://reader036.vdocuments.net/reader036/viewer/2022062312/5519b1f055034660578b461d/html5/thumbnails/7.jpg)
7
Hashing - Double Hashing
88
75
64
28
41
38
62
3
49
93
54
![Page 8: 1 Tables & File © Dave Bockus. 2 Binary Search Recursive int Bsearch(TableType T, KeyType key, int lt, int rt) { int mid; mid = (lt+rt)/2; if (lt>rt)](https://reader036.vdocuments.net/reader036/viewer/2022062312/5519b1f055034660578b461d/html5/thumbnails/8.jpg)
8
Hashing - Internal Chaining
88 -1
75 -1
3
64 -1
28 -1
41 -1
0
38 -1
7
62 -1
4
3 -1
49 -1
5
93 -1
8
54 -1
9
![Page 9: 1 Tables & File © Dave Bockus. 2 Binary Search Recursive int Bsearch(TableType T, KeyType key, int lt, int rt) { int mid; mid = (lt+rt)/2; if (lt>rt)](https://reader036.vdocuments.net/reader036/viewer/2022062312/5519b1f055034660578b461d/html5/thumbnails/9.jpg)
9
Buckets
Hard Disks consist of platters of magnetic media
![Page 10: 1 Tables & File © Dave Bockus. 2 Binary Search Recursive int Bsearch(TableType T, KeyType key, int lt, int rt) { int mid; mid = (lt+rt)/2; if (lt>rt)](https://reader036.vdocuments.net/reader036/viewer/2022062312/5519b1f055034660578b461d/html5/thumbnails/10.jpg)
10
Buckets - Cont..
Tracks are written to the surface of the disk
![Page 11: 1 Tables & File © Dave Bockus. 2 Binary Search Recursive int Bsearch(TableType T, KeyType key, int lt, int rt) { int mid; mid = (lt+rt)/2; if (lt>rt)](https://reader036.vdocuments.net/reader036/viewer/2022062312/5519b1f055034660578b461d/html5/thumbnails/11.jpg)
11
Buckets - Cont..
Tracks are broken into sectors
![Page 12: 1 Tables & File © Dave Bockus. 2 Binary Search Recursive int Bsearch(TableType T, KeyType key, int lt, int rt) { int mid; mid = (lt+rt)/2; if (lt>rt)](https://reader036.vdocuments.net/reader036/viewer/2022062312/5519b1f055034660578b461d/html5/thumbnails/12.jpg)
12
Buckets - Cont..
Each Sector can have 1 or more blocks of data written to it.
When I/O is performed a block of data is transferred.
![Page 13: 1 Tables & File © Dave Bockus. 2 Binary Search Recursive int Bsearch(TableType T, KeyType key, int lt, int rt) { int mid; mid = (lt+rt)/2; if (lt>rt)](https://reader036.vdocuments.net/reader036/viewer/2022062312/5519b1f055034660578b461d/html5/thumbnails/13.jpg)
13
Buckets - Cont..
Each block can contain multiple records. When a block is read multiple records are pulled into memory
1 23 4 5 6
Block of data read into memory
![Page 14: 1 Tables & File © Dave Bockus. 2 Binary Search Recursive int Bsearch(TableType T, KeyType key, int lt, int rt) { int mid; mid = (lt+rt)/2; if (lt>rt)](https://reader036.vdocuments.net/reader036/viewer/2022062312/5519b1f055034660578b461d/html5/thumbnails/14.jpg)
14
Buckets - Cont..
When a bucket becomes full a pointer to an Overflow bucket tells the search algorithm where to look.
![Page 15: 1 Tables & File © Dave Bockus. 2 Binary Search Recursive int Bsearch(TableType T, KeyType key, int lt, int rt) { int mid; mid = (lt+rt)/2; if (lt>rt)](https://reader036.vdocuments.net/reader036/viewer/2022062312/5519b1f055034660578b461d/html5/thumbnails/15.jpg)
15
Extendible Hashing
• Suffix describes the element in the bucket
An index is broken into 2 sections
aaabbbbbb: aaa - bbbbbb
Prefix describes the index table
001000 010 011 100 101 110 111
bbbbbb bbbbbb bbbbbb bbbbbb bbbbbb
![Page 16: 1 Tables & File © Dave Bockus. 2 Binary Search Recursive int Bsearch(TableType T, KeyType key, int lt, int rt) { int mid; mid = (lt+rt)/2; if (lt>rt)](https://reader036.vdocuments.net/reader036/viewer/2022062312/5519b1f055034660578b461d/html5/thumbnails/16.jpg)
16
Extendible Hashing• As a bucket becomes full, the prefix is split.
• Assume key 000bbbbbb or 001bbbbbb is inserted causing the bucket to become full.
001000 010 011 100 101 110 111
bbbbbb bbbbbb bbbbbb bbbbbb bbbbbbbbbbbb
![Page 17: 1 Tables & File © Dave Bockus. 2 Binary Search Recursive int Bsearch(TableType T, KeyType key, int lt, int rt) { int mid; mid = (lt+rt)/2; if (lt>rt)](https://reader036.vdocuments.net/reader036/viewer/2022062312/5519b1f055034660578b461d/html5/thumbnails/17.jpg)
17
Extendible HashingIf the index can’t support another bucket, then the index is expanded to include another bit. aaaa-bbbbb
Assume key 000-bbbbbb is inserted, then index can’t support a split so the index must be expanded to 4 bits.
bbbbb bbbbb bbbbb bbbbb bbbbbbbbbbbbbbb
00100000 0100 0110 1000 1010 1100 1110…0001 0011 …
![Page 18: 1 Tables & File © Dave Bockus. 2 Binary Search Recursive int Bsearch(TableType T, KeyType key, int lt, int rt) { int mid; mid = (lt+rt)/2; if (lt>rt)](https://reader036.vdocuments.net/reader036/viewer/2022062312/5519b1f055034660578b461d/html5/thumbnails/18.jpg)
18
Hashing - Open Addressing & Deletions
88
75
64
28
41
38
62
3
49
93
54
Delete 75
1
Do a Search
Set Delete Flag
Search For 49
Search skips deleted cell
Continues until 49 is found
Adding a new value 16 => f(16)=1
16
Add value, if deleted remove delete flag.
Search for an empty or deleted cell.
![Page 19: 1 Tables & File © Dave Bockus. 2 Binary Search Recursive int Bsearch(TableType T, KeyType key, int lt, int rt) { int mid; mid = (lt+rt)/2; if (lt>rt)](https://reader036.vdocuments.net/reader036/viewer/2022062312/5519b1f055034660578b461d/html5/thumbnails/19.jpg)
19
Hashing - Internal Chaining& Deletions
88 -1
75 -1
3
64 -1
28 -1
41 -1
0
38 -1
7
62 -1
4
3 -1
49 -1
5
93 -1
8
54 -1
9
Assume 88 is deleted
1
Insert f(a) =1Delete flag is removed, a is inserted
10
a
Idx on 2 is updated
2
Internal chains coalesce
Insert f(b) =1
Search for an empty cell
b -1 Insert b into cell
Update Internal Chain