Exercise 1.8 Binary Search - Function Problems

Exercise 1.8 Binary Search - Function Problems

Problem-solving Code

Position BinarySearch( List L, ElementType X )
{
    Position left=0,right=L->Last;
    Position loc,ret=NotFound;
    while(left<=right){
        loc=(left+right)/2;
        if(L->Data[loc]==X){
            ret=loc;
            break;
        }else if(L->Data[loc]>X){
            right=loc-1;
        }else{
            left=loc+1;
        }
    }
    return ret;
}

Test Results

Here Insert Picture Description

Finishing problem

Published 10 original articles · won praise 0 · Views 90

Guess you like

Origin blog.csdn.net/Aruasg/article/details/104793382