How to write the complete C code of Fibonacci search algorithm
This article will explain in detail how to write the complete C code of Fibonacci search algorithm, the content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.
/ * Fibonacci search method * / # include # include int Fib (int k) {if (1 = = k | | 2 = = k) return 1; else return Fib (KMI 1) + Fib (KMUE 2);} int FibSearch (int * a, int n, int key) {int k = 1; int nFib; int * b; int low, mid, high While (Fib (k)
< n ) //找到Fib[k] k++; nFib = Fib(k); b = (int *)realloc( a, sizeof(int)*nFib ); //扩充数组的大小 for( int i=n; i key ) { k = k - 1; high = mid; } if( b[mid] < key ) { k = k-2; low = mid+1; } if( b[mid] == key ) { if( mid >= nmur1 & & mid