二进制搜索

二进制搜索用于在

值的排序列表

它选择排序值数组中的中间元素,并将其与目标值进行比较; 这就是我们在数组中寻找的关键。

如果它低于目标值,则在中间元素之后搜索,直到数组末尾。 如果相等,则找到目标值并返回中间值。 否则,从数组的开头到中间-1进行搜索。 如果下限大于上限(确定搜索区域的上限),则目标值不在数组中。

我提供了一个二进制搜索功能的示例代码。

 
/**
 *
 *
 *    FILE: bsearch.h
 *
 *
 *    Description:
 *
 *        Function prototype for binarySearch function.
 *
 *
 */   
#ifndef _bsearch_h   
#define _bsearch_h     
/**
 *
 *    Synopsis:
 *
 *        int binarySearch( int a[], int key, int size )
 *
 *
 *    Description:
 *
 *        Function binarySearch performs a binary search in a 
 *        sorted array a searching for a key.
 *
 *
 *    Parameters:
 *
 *        a[]: sorted array of integers.
 *
 *        key: key to be searched for in a[].
 *
 *        size: size of a[].
 *
 *
 *    Assertions:
 *
 *        none.
 *
 *
 *    Returns:
 *
 *        Function binarySearch returns -1 if key is not in a[].
 *        Otherwise in returns the i, for which a[ i ] == key.
 *
 *
 */
int binarySearch( int a[], int key, int size );     
#endif 

/**
 *
 *
 *    FILE: binarySearch.c
 *
 *
 *    Description:
 *
 *        File contains binarySearch functions declared in bsearch.h file.
 *
 *
 */     
#include "bsearch.h"     
/**
 *------------------------------------------------------------
 *
 *    Function _binarySearch is called by binarySearch function with
 *    low = 0 and high = size - 1, to secure the search boundaries of 
 *    the array.
 *
 *------------------------------------------------------------
 */
static int _binarySearch( int a[], int key, int low, int high );     
int binarySearch( int a[], int key, int size ){   
    return ( _binarySearch( a, key, 0, size - 1 ) );   
}    //    int binarySearch( int a[], int key, int size )    
/**
 *------------------------------------------------------------
 *
 *    Search key value in a[].
 *
 *------------------------------------------------------------
 */
static int _binarySearch( int a[], int key, int low, int high ){   
    if ( low > high ){   
        return ( -1 );                                /** key not found in a[]                                                */   
    }
    else{   
        int middle = ( low + high ) / 2;   
        if ( a[ middle ] == key ){   
            return ( middle );                        /** key found in a[]                                                    */   
        }
        else if ( a[ middle ] < key ){   
            /**
             *
             *    key we are searching is larger than the value of a[ middle ]
             *    and since array is sorted, the search will continue in the side
             *    after the middle index; that is from middle + 1 till high.
             *
             */
            return ( _binarySearch( a, key, middle + 1, high ) );   
        }
        else{   
            /**
             *
             *    key we are searching is lower than the value of a[ middle ]
             *    and since array is sorted, the search will continue in the side
             *    before the middle index; that is from low till high - 1.
             *
             */
            return ( _binarySearch( a, key, low, middle - 1 ) );   
        }   
    }   
}    //    static int _binarySearch( int a[], int key, int low, int high ) 

/**
 *
 *
 *    FILE: main.c
 *
 *
 *    Description:
 *
 *        Test binarySearch function.
 *
 *
 */     
#include "bsearch.h"   
#include <assert.h>     
/**
 *
 *    Simple check for binarySearch function
 *
 */
int main( void ){   
    int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };   
    int i;   
    for ( i = 0 ; i < 21 ; ++i ){   
        assert( binarySearch( a, i, 21 ) == i );   
    }   
    for ( ; i < 50 ; ++i ){   
        assert( binarySearch( a, i, 21 ) == -1 );   
    }   
    return ( 0 );   
}    //    int main( void ) 

From: https://bytes.com/topic/c/insights/799745-binary-search

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值