哨兵查找法0.0

#include "stdio.h"

int search(int ary[], int value, int len);

int main(void)
{
    int ary[6] = { 1, 2, 3, 4, 5, 6 };

    int index = search(ary, 4, 6);
    printf("%d\n", index);
    index = search(ary, 7, 6);
    printf("%d", index);


    scanf("%d", &index);

    return 0;
}


int search(int ary[], int value, int len)
{
    if (ary[len - 1] == value)
    {
        return len - 1;
    }

    ary[len - 1] = value;

    int i = 0;
    while (ary[i] != value)
    {
        i++;
    }


    return (i < len - 1) ?  i : -1;

}
posted @ 2016-09-14 11:28  盘子脸  阅读(507)  评论(0编辑  收藏  举报