1. 3576.
    0
    /* this program binary search */
    #include <stdio.h>
    #include <conio.h>
    #define size 15

    int binarysearch( const int b[], int searchkey, int low, int high );

    /* function main begins program execution */
    int main( void )
    {
    /* initialize a */
    int a[ size ];
    int key; /* value to locate in array a */
    int result; /* variable to hold location of key or -1 */
    int i;

    printf( "input search keyn" );
    scanf("%d",&key);

    printf( "data itemsn" );
    /* generate and print a array */
    for ( i = 0; i < size; i++ ) {
    a[i]= i*2;
    printf( "%4d", a[ i ] );
    } /* end for */

    /* binary search */
    result = binarysearch( a, key, 0, size - 1 );

    /* display results */
    if ( result != -1 ) {
    printf( "n%d found in %d th indicen", key, result );
    } /* end if */
    else {
    printf( "n%d not foundn", key );
    } /* end else */

    getch();
    return 0; /* indicates successful termination */
    }

    int binarysearch( const int b[], int searchkey, int low, int high )
    {
    int middle; /* variable to hold middle element of array */

    /* loop until low subscript is greater than high subscript */
    while ( low <= high ) {

    /* determine middle element of subarray being searched */
    middle = ( low + high ) / 2;

    /* if searchkey matched middle element, return middle */
    if ( searchkey == b[ middle ] ) {
    return middle;
    } /* end if */

    /* if searchkey less than middle element, set new high */
    else if ( searchkey < b[ middle ] ) {
    high = middle - 1; /* search low end of array */
    } /* end else if */

    /* if searchkey greater than middle element, set new low */
    else {
    low = middle + 1; /* search high end of array */
    } /* end else */

    } /* end while */

    return -1; /* searchkey not found */

    } /* end function binarysearch */
    ···
   tümünü göster