C语言-二分查找法
使用二分法进行查找,通过中间位置的判断进行二分查找,将p_head 和 p_tail的位置进行改变,一直到p_head > p_tail 停止循环
/* 折半排序查找 */ #include <stdio.h> int half_search(int* p_val, int size, int val) { int* p_head = p_val; int* p_tail = p_val + size - 1; while (p_head < p_tail) { int* p_mid =p_head + (p_tail - p_head) / 2; if (*p_mid > val) { p_tail = p_mid; p_tail--; } else if(*p_mid < val) { p_head = p_mid; p_head++; } else if(*p_mid == val) { return (p_mid - p_val); } } return -1; } int main(void) { int val[5] = {1, 2, 3, 4, 5}; int result = half_search(val, 5, 6); if (result == -1) { printf("没有查到\n"); } else { printf("%d\n", result); } }
每天更新pyQt5内容