顺序查找:二分查找,斐波那契查找,插值查找


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

void printerr(){
    cout<<"fc"<<endl;
}
int binary_search(int *arr, int start, int end, int ele){
    while(end >= start){
        int mid = (end-start)/2 + start;
        if(arr[mid] == ele){
            return mid;
        }else if(arr[mid] > ele){
            end = mid - 1;
        }else{
            start = mid + 1;
        }
    }
    return -1;
}

void print(int i){
    cout<<i<<" ";
}
void find_fbnq(int len, vector<int> &fbnq){
    int i = 3;
    fbnq.clear();
    if(len == 1){
        fbnq.push_back(0);
    }else if(len == 2){
        fbnq.push_back(0);
        fbnq.push_back(1);
    }else if(len == 3){
        fbnq.push_back(0);
        fbnq.push_back(1);
        fbnq.push_back(2);
    }else if(len > 3){
        fbnq.push_back(0);
        fbnq.push_back(1);
        fbnq.push_back(2);
        do{
            fbnq.push_back(fbnq[i-1] + fbnq[i-2]);
        }while(len > fbnq[i++]);
    }
}

int fbnq_search(int *arr, int start, int end, int ele){
    vector<int> fbnq;
    int len = end - start +1;
    find_fbnq(len, fbnq);
    int fbnq_size = fbnq.size() - 2;
    while(end >= start){
        int mid = start + fbnq[fbnq_size];
        if(arr[mid] == ele){
            return mid;
        }else if(arr[mid] > ele){
            fbnq_size--;
            end = mid - 1;
        }else{
            start = mid + 1;
            len = end - start + 1;
            find_fbnq(len, fbnq);
            fbnq_size = fbnq.size() - 2;
        }
    }
    return -1;
}
//下面2个函数是第二次写的菲波那契查找。

int fbnq2(int n){//n表示传入的长度。
 if(n == 1) return 0;
 else {
  int i1 = 1;
  int i2 = 1;
  while(n > i1+ i2){
   i2 += i1;
   i1 = i2 - i1;
  }
  return i2;
 }
}
int fbnq_search2(int *arr, int start, int end, int ele){
 int mid = -1,  len = -1;
 while(end >=start){
  len = end - start + 1;
  mid = start + fbnq2(len);
  if(arr[mid] == ele){
   return mid;
  }else if(arr[mid] > ele){
   end = mid - 1;
  }else{
   start = mid + 1;
  }
 }
 return -1;
}
/**
* 插值查找**
* 1. 每次查找都要付出较大代价

* 2. 排好序,呈均匀分布

*/
int insert_search(int * arr, int start , int end, int ele){
 int mid = -1;
 while(end >= start){
  mid = start + ((ele - arr[start])/(arr[end]-arr[start])) * (end - start) + 0.5;
  if(arr[mid] == ele){
   return mid;
  }else if(arr[mid] > ele){
   end = mid - 1;
  }else{
   start = mid + 1;
  }
 }
  return -1;
 }

 void test(const char * des, int *arr, int start, int end, int (*fun)(int*, int, int, int)){
    cout<<"-----"<<des<<"------"<<endl;
    for(int i = start; i <= end ; i++){
        cout<<fun(arr, start, end, arr[i])<<" ";
    }
    cout<<"\n--------------------"<<endl;
    cout<<"-11: "<<fun(arr, start, end, -11)<<endl;//-1
    cout<<"10: "<<fun(arr, start, end, 10)<<endl;//-1
    cout<<"100: "<<fun(arr, start, end, 100)<<endl;//-1
    cout<<"500: "<<fun(arr, start, end, 500)<<endl;//-1
    cout<<"\n--------------------"<<endl;
    cout<<"-11: "<<fun(arr, start + 5, end, -11)<<endl;//找不到
    cout<<"33: "<<fun(arr, start+5, end, 33)<<endl;//找不到
    cout<<"222: "<<fun(arr, start + 3, end  - 5, 222)<<endl;//找不到
    cout<<"44: "<<fun(arr, start + 4, end, 44)<<endl;//找不到
    cout<<"63: "<<fun(arr, start + 4, end  - 1, 63)<<endl;//找到
    cout<<"500: "<<fun(arr, start + 3, end - 2, 500)<<endl;//找不到
    cout<<"\n--------------------"<<endl;
}
int main(){
    int arr[] = {11,22,33,44,52,63,74,86,97,188,222,329, 438};
    test("fbnq", arr, 0, 12, fbnq_search);
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值