1 Tables & File © Dave Bockus. 2 Binary Search Recursive int Bsearch(TableType T, KeyType key, int...

19
1 Tables & File © Dave Bockus

Transcript of 1 Tables & File © Dave Bockus. 2 Binary Search Recursive int Bsearch(TableType T, KeyType key, int...

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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