Download presentation

Presentation is loading. Please wait.

Published byLondon Halden Modified over 2 years ago

1
1 Tables & File © Dave Bockus

2
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
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
4 51 Binary Search Example 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 (Array[3] = 6) 7 so... continue 4lt =rt =mid = 4 (Array[4] = 7) = 7 Found!!!! 4 4

5
5 Hashing - Chaining

6
6 Hashing - Open Addressing

7
7 Hashing - Double Hashing

8
8 Hashing - Internal Chaining

9
9 Buckets Hard Disks consist of platters of magnetic media

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

11
11 Buckets - Cont.. Tracks are broken into sectors

12
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
13 Buckets - Cont.. Each block can contain multiple records. When a block is read multiple records are pulled into memory Block of data read into memory

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

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

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

17
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 … …

18
18 Hashing - Open Addressing & Deletions 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
19 Hashing - Internal Chaining & Deletions 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

Similar presentations

© 2016 SlidePlayer.com Inc.

All rights reserved.

Ads by Google