Fibonacci Search Searching in an Ordered Table

J.E.D.I lower = 2, upper = 2, middle = 2: k = k middle 34567 == successful search Since the search area is reduced logarithmically, i.e., halved each time the size is reduced, the time complexity of the algorithm is Olog 2 n. The algorithm may be used if the table uses indexed sequential organization. However, it can only be used with ordered tables stored as an array.

8.4.3.3 Multiplicative Binary Search

Multiplicative binary search is similar to the previous algorithm but avoids the division needed to find the middle key. To do that, there is a need to rearrange the records in the table: 1. Assign keys K 1 K 2 K 3 … K n to the nodes of a complete binary tree in inorder sequence 2. Arrange the records in the table according to the corresponding level-order sequence in the binary tree. The Search Algorithm The comparison begins at the root of the binary tree, j=0, which is the middle key in the original table. Accepts a set of keys represented as a complete binary tree int multiplicativeBinarySearchint k, int key[]{ int i = 0; while i key.length{ if k == key[i] return i; successful search else if k key[i] i = 2 i + 1; go left else i = 2 i + 2; go right } return -1; unsuccessful search } Since computation for the middle element is eliminated, multiplicative binary search is faster than the traditional binary search. However, there is a need to rearrange the given set of keys before the algorithm can be applied.

8.4.3.4 Fibonacci Search

Fibonacci search uses the simple properties of the Fibonacci number sequence defined by the following recurrence relation: F = 0 F 1 = 1 F j = F i-2 + F i-1 , i ≥ 2 Data Structures 167 J.E.D.I That is, the sequence 0, 1, 1, 2, 3, 5, 8, 13, 21, ... In java, int Fibonacciint i{ if i == 0 return 0; else if i == 1 return 1; else return Fibonaccii-1 + Fibonaccii-2; } In the search algorithm, two auxiliary variables, p and q, are used: p = F i-1 q = F i-2 K j is initially chosen such that j = F i , where F i is the largest Fibonacci number less than or equal to the table size n. It is an assumption that the table is of size n=F i+1 -1. Three states of comparison are possible: • If K = K j , stop: successful search • If K K j 1. Discard all the keys with indexes greater than j. 2. Set j = j – q 3. Shift p and q one place to the left of the number sequence • If K K j , • Discard all the keys with indexes less than j • Set j = j + q • Shift p and q two places to the left of the number sequence • K K j and q=0 or K K j and p=1: search unsuccessful This algorithm finds the element from index 1 to n, and since indexing in Java starts at 0, there is a need to handle the case where k = key[0]. For example, search for key k = 34567: 1 2 3 4 5 6 12345 23456 34567 45678 56789 67890 78901 0 1 1 2 3 5 8 13 F 0 1 2 3 4 5 6 7 i i = 5; F i = 5; Assumption table size = F i+1 - 1 = 7 j = 5, p = 3, q = 2: k key[j] j = 3, p = 2, q = 1: k key[j] j = 2, p = 1, q = 1: k = key[j] Successful Another example, search for key = 15: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 Data Structures 168 J.E.D.I i = 7; F i = 13; Assumption table size = F i+1 - 1 = 20 j = 13; p = 8, q=5: k key[j] j = 18; p = 3; q=2: k key[j] j = 15; p = 2; q=1: k = key[j] Successful The following is the algorithm: int fibonacciSearchint k, int key[]{ int i; int F[] = new int[key.length]; int temp; Find Fi, the largest Fibonacci number = table size for i=0; ikey.length; i++{ F[i] = Fibonaccii; if key.length F[i] { i--; break; } } int p = F[i-1]; int q = F[i-2]; int j = F[i]; if k == key[0] return 0; while true{ if k == key[j] return j; successful search else if k key[j] if q == 0 return -1; unsuccessful search else { adjust i, p and q j = j - q; temp = p; p = q; q = temp - q; } else if p == 1 return -1; unsuccessful search else{ adjust i, p and q j = j + q; p = p - q; q = q - p; } } } Data Structures 169 J.E.D.I

8.5 Summary

• A table is a group of elements, each of which is called a record • Two types of keys are internal or embedded key, and external key • Tables may be represented using sequential allocation, link allocation or both • Searching is eliminated in direct-address table. Also, insertion and deletion are relatively straightforward • A table may be ordered or unordered. Linear search is used if the table is unordered, while indexed sequential search, binary search or Fibonacci search is used if the table is ordered

8.6 Lecture Exercises

Using the following methods of searching, a Sequential Search b Multiplicative Binary Search c Binary Search d Fibonacci Search Search for 1. A in S E A R C H I N G 2. D in W R T A D E Y S B 3. T in C O M P U T E R S

8.7 Programming Exercise

1. Extend the Java class defined in this chapter to contain methods for insertion at a specified location and deletion of an item with key K. Use sequential representation. Data Structures 170