• 二分查找练习


    #include <stdio.h>
    /*非递归二分*/
    int HalfSearch(int a[], int low, int high, int key)
    {
        while(low <= high)
        {
            int mid = (low + high) / 2;
            if(a[mid] > key)
            {
                high = mid - 1;
            }
            else if(a[mid] < key)
            {
                low = mid + 1;
            }
            else
            {
                return mid;
            }
        }
    }
    /*递归二分*/
    int HalfSearch2(int a[], int low, int high, int key)
    {
        int mid = (low + high) / 2;
        if (low >= high)
        {
            return mid;
        }
        if (a[mid] > key) mid = HalfSearch2(a, low, mid - 1, key);
        else if(a[mid] < key) mid = HalfSearch2(a, mid + 1, high, key);
    }
    
    /*找到第一个等于key的下标*/
    int HalfSearch3(int a[], int low, int high, int key)
    {
        while(low <= high)
        {
            int mid = (low + high) / 2;
            if(a[mid] > key)
            {
                high = mid - 1;
            }
            else if(a[mid] < key)
            {
                low = mid + 1;
            }
            else
            {
                if ((0 == mid) || (a[mid - 1] != key))
                {
                    return mid;
                }
                else
                {
                    high = mid - 1;
                }
            }
        }
    }
    
    /*找到最后一个等于key的下标*/
    int HalfSearch4(int a[], int low, int high, int key)
    {
        int n = high;
        while(low <= high)
        {
            int mid = (low + high) / 2;
            if(a[mid] > key)
            {
                high = mid - 1;
            }
            else if(a[mid] < key)
            {
                low = mid + 1;
            }
            else
            {
                if ((n == mid) || (a[mid + 1] != key))
                {
                    return mid;
                }
                else
                {
                    low = mid + 1;
                }
            }
        }
    }
    
    /*找到第一个大于或等于key的下标*/
    int HalfSearch5(int a[], int low, int high, int key)
    {
        while(low <= high)
        {
            int mid = (low + high) / 2;
            if(a[mid] >= key)
            {
                if ((0 == mid) || (a[mid - 1] < key))
                {
                    return mid;
                }
                else
                {
                    high = mid - 1;
                }
            }
            else if(a[mid] < key)
            {
                low = mid + 1;
            }
        }
    }
    
    /*找到最后一个小于或等于key的下标*/
    int HalfSearch6(int a[], int low, int high, int key)
    {
        int n = high;
        while(low <= high)
        {
            int mid = (low + high) / 2;
            if(a[mid] >= key)
            {
                high = mid - 1;
            }
            else if(a[mid] <= key)
            {
                if ((n == mid) || (a[mid + 1] > key))
                {
                    return mid;
                }
                else
                {
                    low = mid + 1;
                }
            }
        }
    }
    
    int main()
    {
        int i = 0;
        int a[10] = {2, 9, 4, 1, 3, 55, 46, 15, 10, 32};
        int b[10] = {1, 22, 30, 30, 30, 56, 60, 60, 90, 91};
        // for (i = 0; i < 10; i++)
        // {
            // printf("a[%d]:%d
    ", i, a[i]);
        // }s
        printf("%d
    ", HalfSearch6(b, 0, 9, 50));
        return 0;
    }
  • 相关阅读:
    拷贝目录下文件,但某种类型文件例外
    编译个性化的openwrt固件
    -exec和|xargs
    OpenMP多线程linux下的使用,简单化
    clock_gettime的使用,计时比clock()精确
    openvswitch安装和使用 --修订通用教程的一些错误
    树莓派配置AP模式
    win7下的mstsc ubuntu下的rdesktop
    微信小程序-商品列表左=>右联动
    Vue.js最佳实践(五招让你成为Vue.js大师)
  • 原文地址:https://www.cnblogs.com/zzdbullet/p/10512555.html
Copyright © 2020-2023  润新知