查找算法

//顺序查找
//时间复杂度:O(n)
int SequenceSearch(int a[], int value, int n)
{
     
    int i;
    for(i=0; i<n; i++)
        if(a[i]==value)
            return i;
    return -1;
}

//二分查找
//时间复杂度:O(logn)
int BinarySearch1(int a[], int value, int n)
{
     
    int low, high, mid;
    low = 0;
    high = n-1;
    while(low<=high)
    {
     
        mid = (low+high)/2;
        if(a[mid]==value)             //取中间量
            return mid;
        else if(a[mid]>value)
            high = mid-1;            //从前半部分查找
        else
            low = mid+1;            //从后半部分查找
    }
    return -1;
}

//插值查找
//时间复杂度:O(logn)
int InsertionSearch(int a[], int value, int n)
{
     
	int low = 0, mid, high = n-1;
 	while(low<=high)
    {
     
        mid = low+(value-a[low])/(a[high]-a[low])*(high-low);
        if(a[mid]==value)            
        	return mid;
        else if(a[mid]>value)
            high = mid-1;          
        else
            low = mid+1;            
    }
    return -1;
}

//斐波那契查找


你可能感兴趣的:(c算法)