当前位置:网站首页>A half search method for sequential tables

A half search method for sequential tables

2022-06-25 12:45:00 yyy_ zxc

#include "seqlist.cpp"        // Basic algorithm of inclusion sequence table

// Half search algorithm
int BinSearch(RecType R[],int n,KeyType k){
    int low=0,high=n-1,mid,count=0;
    while(low<=high){        // When low>high when , Indicates that the search failed  
        mid=(low+high)/2;
        printf(" The first %d Compare it to : stay 【%d,%d】 Compare elements in R[%d]:%d\n",
            ++count,low,high,mid,R[mid].key);
        if(R[mid].key==k)    // Search successful return
            return mid+1;
        if(R[mid].key > k)    // Continue to R[low...mid-1] Search for
            high=mid-1;
        else                // Continue to R[mid+1...high] Search for  
            low=mid+1; 
    }
    return 0; 

int main(){
    RecType R[MAXL];
    KeyType k = 9;
    int a[]={1,2,3,4,5,6,7,8,9,10};
    int i,n=10;
    CreateList(R,a,n);        // Create a sequence table
    printf(" Keyword sequence :");
    DispList(R,n);
    printf(" lookup %d The comparison process is as follows :\n",k);
    if((i=BinSearch(R,n,k))!=0)
        printf(" Elements %d The position is %d\n",k,i);
    else
        printf(" Elements %d Not in table \n",k);
    return 1; 
}

 

原网站

版权声明
本文为[yyy_ zxc]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206251207590689.html