search - binary search

Posted 小马识图

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了search - binary search相关的知识,希望对你有一定的参考价值。

#include "stdio.h"
#include "string.h"

#define MAX_LIST 50

typedef struct _SqList 
    int data[MAX_LIST];
    int length;
SqList;

int BinarySearch( SqList* L, int key )

    int low = 0;
    int high = L->length - 1;
    int mid;

    while( low <= high )
    
        mid = ( low + high ) /2;

        if( key < L->data[mid] )
            high = mid - 1;
        else if( key > L->data[mid] )
            low = mid + 1;
        else
            return mid;
       
    return MAX_LIST;;


int main()

    SqList d;
    int intarr[] = 1,10,23,48,65,67,78,79,98,100;
    memcpy( d.data, intarr, sizeof(intarr));
    d.length = sizeof(intarr)/sizeof(int);  
    int index = 0;
    for( ; index < d.length; index++ )
        printf(" %d", d.data[index] );
    int key = 67;
    printf("\\nbinary search %d...\\n", key);
    index = BinarySearch( &d, key );
    printf("%d is at position %d\\n", key, index);
    printf("\\n");
    return 0;

以上是关于search - binary search的主要内容,如果未能解决你的问题,请参考以下文章

700. Search in a Binary Search Tree

binary search

search - binary search

[Lintcode]95. Validate Binary Search Tree/[Leetcode]98. Validate Binary Search Tree

search - binary search/sort tree

[LeetCode] 173. Binary Search Tree Iterator_Medium_tag: Binary Search Tree