Bootstrap

二分查找法与字符数组

二分法

#include<stdio.h>

int main(void)
{
    int n;
    scanf("%d",&n);
    int a[] = {1,3,5,7,4,2,};
    int i,len =sizeof(a) / sizeof(a[0]);
    for(i = 1;i <= len-1;++i)
    {
        int t,j;
        j = i;
        t = a[i];
        while(j > 0 && t < a[j - 1])
        {
            a[j] = a[j - 1];
            --j;                                
        }
        a[j] = t;
    }                                       //排序

    for(i = 0;i < len;++i)
    {
        printf("%d\t\n",a[i]);
    }
    
    int begin,end,mid;
    begin = 0;
    end =len -1;
    while(begin <= end)
    {
        mid = (begin + end) / 2;
        if(a[mid] > n)
        {
            end = mid - 1;
        }
        else if(a[mid]<n)
        {
            begin = mid + 1;
        }
        else
        {
            break;
        }                                 
    }c                                     //二分查找
    
    if(begin <= end)
    {
        printf("found:%d,index = %d\n",a[mid],mid + 1);
    }
    else
    {
        printf("Not found\n");
    }
    return 0;
}

字符数组的表示及相关函数的使用

;