有事您Q我 #div_digg { position: fixed; bottom: 10px; right: 15px; border: 2px solid #ECD7B1; padding: 10px; width: 140px; background-color: #fff; border-radius: 5px 5px 5px 5px !important; box-shadow: 0 0 0 1px #5F5A4B, 1px 1px 6px 1px rgba(10, 10, 0, 0.5); }

静态查找_Search

#include <stdio.h>

#define MAXSIZE 50
#define OK 1
#define ERROR 0

int F[MAXSIZE];//斐波那契数列 
int count;

typedef int Status;//Stastu为函数返回的类型

/*    a为数组, n为数组要查找的个数,key为要查找的关键字    */



Status Sequential_Search(int *a, int n, int key)
{/*    无哨兵顺序查找, 查询n次a数组中是否与key相同,相同则返回key在数组中的位置,否则返回错误    */
    int i;
    count = 0;
    for(i=0; i<n; i++)
    {
        count ++;    
        if(a[i] == key)
            return i;//查找到则返回位置 
    }
 
    return ERROR;
}



Status Sequential_Search2(int *a, int n, int key)
{/*    有哨兵顺序查找,返回key在a数组中的位置    */
    int i = n;
//    a[28] = key;//设置哨兵 
    count = 1;
    while(a[i] != key)//  若查到 
    {
        count ++;
        i--;
    }
    return i;//则返回 
}



Status Binary_Search(int *a, int n, int key)
{/*    折半查找    */
    int low = 0;//定义最低下标 
    int high = n;//定义最高下标 
    int mid;//折半下标 
    count = 0;
    while(low <= high)
    {
        count ++;
        mid = (low+high)/2;//折半
        if(key < a[mid])//若查找的值比中值小 
            high = mid -1;//最高位置调整中值减一 
        else if(key > a[mid])//若查找的值比中值大 
            low = mid + 1;//最低位置调整到中值加一 
        else            //若查找的值不大于也不小于中值 
            return mid;//那么肯定就是相等,则返回查找值的位置 
    }

    return ERROR;
}

Status Interpolation_Search(int *a, int n, int key)
{/*    插值查找    */
    int low = 1;
    int high = n;
    int mid;
    count = 1;
    while(low <= high)
    {
        count ++;
        mid = low + (high-low)*(key-a[low])/(a[high]-a[low]);//插值 
        if(key < a[mid])//若查找的值比中值小 
            high = mid -1;//最高位置调整中值减一 
        else if(key > a[mid])//若查找的值比中值大 
            low = mid + 1;//最低位置调整到中值加一 
        else            //若查找的值不大于也不小于中值 
            return mid;//那么肯定就是相等,则返回查找值的位置 
    } 
    
    return ERROR;
}

Status Fibonacci_Search(int *a, int n, int key)
{/*    斐波那契序列查找    */
    int mid, i, k=0;
    int low = 0;
    int high = n;

    //查找n的位于斐波那契数列位置    
    while(n > F[k])
        k++;
    
    //把n到比n大的斐波那契数列之间的数组值填充 
    for(i=n; i<F[k]; i++)
         a[i] = a[n];
        
    count = 1; 
     while(low <= high)
     {
         count ++;
         mid = low + F[k-1] - 1;
         if(key < mid)
         {
             high = mid - 1;
             k = k - 1;
         }
         else if(key > mid)
         {
             low = mid + 1;
             k = k - 2;
         }
         else
         {
             if(mid <= n)
                 return mid;
             else
                 return n;
         }
         
     }
    
    return ERROR;
}

int main(void)

{
    int i, result;
    int a[MAXSIZE];
    for(i=0; i<=MAXSIZE; i++)
        a[i] = i;

    result = Sequential_Search(a, MAXSIZE, 8);//若这里要查询的是8 
    printf("Sequential_Search:%d, 查询用了%d次\n", result, count);
    
    result = Sequential_Search2(a, MAXSIZE, 8);//若这里要查询的是8
    printf("Sequential_Search2:%d, 查询用了%d次\n", result, count); 
    
    result = Binary_Search(a, MAXSIZE, 8);//若这里要查询的是8
    printf("Binary_Search:%d, 查询用了%d次\n", result, count);
    
    result = Interpolation_Search(a, MAXSIZE, 8);//若这里要查询的是8
    printf("Interpolation_Search:%d, 查询用了%d次\n", result, count);
    
    //生成斐波那契数列 
    F[0] = 0;
    F[1] = 1;
    for(i=2; i<MAXSIZE; i++)
        F[i] = F[i-1] + F[i-2];
    
    result = Fibonacci_Search(F, MAXSIZE, 8);//若这里要查询的是8
    printf("Fibonacci_Search:%d, 查询用了%d次\n", result, count);
    
    system("PAUSE");
    
    return 0;
}
posted @ 2015-03-16 17:00  次奥哥  阅读(226)  评论(0编辑  收藏  举报
有事您Q我 #div_digg { position: fixed; bottom: 10px; right: 15px; border: 2px solid #ECD7B1; padding: 10px; width: 140px; background-color: #fff; border-radius: 5px 5px 5px 5px !important; box-shadow: 0 0 0 1px #5F5A4B, 1px 1px 6px 1px rgba(10, 10, 0, 0.5); }