search - fibonacci search

#include "stdio.h"
#include "string.h"
#include "malloc.h"  

#define MAX_LIST 50

typedef struct _SqList {
    int data[MAX_LIST];
    int length;
}SqList;

//The key difference between Fibonacci search and binary search
//is how the anchor point is determined.
bool FibonacciSearch( SqList* L, int key, int& loc )
{
    static int F[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 };
    int low = 0;
    int high = L->length - 1;
    int mid;

    int l = L->length;
    int k = 0;
    for( ; F[k] < L->length; k++ );

    int *buf = L->data;

    //padding with duplicate numbers if necessary
    if( F[k] > MAX_LIST )
    {
        buf = (int*)malloc( F[k]*sizeof(buf[0]) );
        memcpy( buf, L->data, L->length*sizeof(buf[0]) );
    }
    for( ; l < F[k]; l++ )
        buf[l] = L->data[high];

    l = -1;
    high = F[k] - 1;

    while( k > 0 && low <= high )
    {
        mid = low + F[k-1];

        if( key < buf[mid] )
        {
            high = mid - 1;
            k = k - 1;
        }
        else if( key > buf[mid] )
        {
            low = mid + 1;
            k = k -2;
        }
        else
        {
            if( mid < L->length )
                l = mid;
            else
                l = L->length - 1;
            break;
        }
    }   

    if( buf != L->data ) free(buf);
    return ( loc = l ) != -1;;
}

int main()
{
    SqList d;
    int intarr[] = {1,10,23,48,65,67,78,79,98,100};
    memcpy( d.data, intarr, sizeof(intarr));
    d.length = sizeof(intarr)/sizeof(int);
    int index = 0;
    for( ; index < d.length; index++ )
        printf(" %d", d.data[index] );
    int key = 100;
    printf("\nfibonacci search %d...\n", key);
    FibonacciSearch( &d, key, index );
    printf("%d is at position %d\n", key, index);
    printf("\n");
    return 0;
}
时间: 2024-11-05 20:42:01

search - fibonacci search的相关文章

"二分查找(Binary Search)"与"斐波那契查找(Fibonacci Search)"

首先,我们来看一个笔者的拙作,一段二分查找代码 //返回值是key的下标,如果A中不存在key则返回-1 template <class T> int BinSearch(T* A, const T &key, int lo, int hi) {     int mid;     while(lo<hi)     {         mid = lo + (hi-lo)/2;         if(key < A[mid])             hi = mid-1;

AI-Local search&amp;Online search

Local Search Systematic algorithms: achieve systematicity by keeping one or more paths in memory and by recording which alternatives have been explored at each point along the path. When a goal is found, the path to that goal also constitutes a solut

[Math] Beating the binary search algorithm – interpolation search, galloping search

From: http://blog.jobbole.com/73517/ 二分检索是查找有序数组最简单然而最有效的算法之一.现在的问题是,更复杂的算法能不能做的更好?我们先看一下其他方法. 有些情况下,散列整个数据集是不可行的,或者要求既查找位置,又查找数据本身.这个时候,用哈希表就不能实现O(1)的运行时间了.但对有序数组, 采用分治法通常可以实现O(log(n))的最坏运行时间. 在下结论前,有一点值得注意,那就是可以从很多方面“击败”一个算法:所需的空间,所需的运行时间,对底层数据结构的访

正则表达式中pattern.match(),re.match(),pattern.search(),re.search()方法的使用和区别

正则表达式(regular expression)是一个特殊的字符序列,描述了一种字符串匹配的模式,可以用来检查一个串是否含有某种子串. 将匹配的子串替换或者从某个串中取出符合某个条件的子串,或者是在指定的文章中抓取特定的字符串等.Python处理正则表达式的模块是re模块,它是Python语言中拥有全部的正则表达式功能的模块.正则表达式由一些普通字符和一些元字符组成.普通字符包括大小写的字母.数字和打印符号,而元字符是具有特殊含义 正则表达式大致的匹配过程是: 拿正则表达式依次和字符串或者文本

Word Search 和 Word Search Ⅱ

Word Search 和 Word Search Ⅱ Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighbo

[LeetCode] 211. Add and Search Word - Data structure design Java

题目: Design a data structure that supports the following two operations: void addWord(word) bool search(word) search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one le

LeetCode OJ:Add and Search Word - Data structure design(增加以及搜索单词)

Design a data structure that supports the following two operations: void addWord(word) bool search(word) search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one letter

[leedcode 211] Add and Search Word - Data structure design

Design a data structure that supports the following two operations: void addWord(word) bool search(word) search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one letter

Binary search tree system and method

A binary search tree is provided for efficiently organizing values for a set of items, even when values are duplicated. In generating the binary search tree, the value of each item in a set of values is determined. If a particular value is unique and