15.04.2018 Views

programming-for-dummies

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Sequential Search 415<br />

Fibonacci numbers are a series of numbers that are calculated by adding the<br />

last two numbers in a series to determine the next number in the series. So<br />

the first Fibonacci number is 0, the second is 1, the third is 1 (0 + 1), the fourth<br />

is 2 (1 + 1), the fifth is 3 (1 + 2), the sixth is 5 (2 + 3), and so on like this:<br />

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, and so on . . . .<br />

Fibonacci numbers tend to occur in nature, such as measuring the branching<br />

of trees or the curves of waves. So the idea behind Fibonacci searching is<br />

rather than divide a list in half, like binary searching, Fibonacci searching<br />

divides a sorted list into progressively smaller lists, based on Fibonacci numbers,<br />

until it finally finds the data or confirms that the data doesn’t exist.<br />

Surprisingly, this method works consistently faster than binary searching.<br />

Fibonacci searching works like this, as shown in Figure 2-6:<br />

Divide list by five (5) items.<br />

18 46 89 93<br />

102 146 198<br />

18 46 89 93 102<br />

Divide list by three (3) items.<br />

Figure 2-6:<br />

Fibonacci<br />

numbers<br />

divide and<br />

search a list<br />

more<br />

efficiently<br />

than a<br />

binary<br />

search.<br />

18 46 89<br />

93 102<br />

93<br />

Divide list by one (1) item.<br />

Fibonacci searching divides a list into parts that correspond to<br />

Fibonacci numbers starting with 5, 3, and finally 1.<br />

Book IV<br />

Chapter 2<br />

1. Determine the size of the sorted list (dubbed n).<br />

2. Find the largest Fibonacci number that’s less than the size of the<br />

sorted list (dubbed p).<br />

3. Examine the value stored at the pth location of the sorted list.<br />

If this value is the one you want to find, stop.<br />

Searching<br />

Algorithms

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!