首页 > 代码库 > 哨兵查找法0.0

哨兵查找法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;}

哨兵查找法0.0