# 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)

## Presentation on theme: "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)"— Presentation transcript:

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) //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); }

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 Search Iterative

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 2lt =rt =mid = 1 2 3lt =rt =mid = (Array[5] = 8) 7 so… continue (Array[2] = 4) 7 so… continue 4 4 3 3 (Array[3] = 6) 7 so... continue 4lt =rt =mid = 4 (Array[4] = 7) = 7 Found!!!! 4 4

5 Hashing - Chaining 887564284138623 49 9354

6 Hashing - Open Addressing 88 75 64 28 41 38 62 3 49 93 54

7 Hashing - Double Hashing 88 75 64 28 41 38 62 3 49 93 54

8 Hashing - Internal Chaining 88 75 3 64 28 41 0 38 7 62 4 3 49 5 93 8 54 9

9 Buckets Hard Disks consist of platters of magnetic media

10 Buckets - Cont.. Tracks are written to the surface of the disk

11 Buckets - Cont.. Tracks are broken into sectors

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.

13 Buckets - Cont.. Each block can contain multiple records. When a block is read multiple records are pulled into memory 1 2 3 4 5 6 Block of data read into memory

14 Buckets - Cont.. When a bucket becomes full a pointer to an Overflow bucket tells the search algorithm where to look.

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 001000010011100101110111 bbbbbb

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. 001000010011100101110111 bbbbbb

17 Extendible Hashing If the index cant support another bucket, then the index is expanded to include another bit. aaaa-bbbbb Assume key 000-bbbbbb is inserted, then index cant support a split so the index must be expanded to 4 bits. bbbbb 00100000010001101000101011001110 … 00010011 …

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.

19 Hashing - Internal Chaining & Deletions 88 75 3 64 28 41 0 38 7 62 4 3 49 5 93 8 54 9 Assume 88 is deleted 1 Insert f(a) =1 Delete 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 Insert b into cell Update Internal Chain

Download ppt "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)"

Similar presentations