C语言二分查找法(指针和数组实现)
2014-07-25 23:20
295 查看
/* * 编写一个函数,对一个已排序的整数表执行二分查找。 * 函数的输入包括各异指向表头的指针,表中的元素个数,以及待查找的数值。 * 函数的输出时一个指向满足查找要求的元素的指针,当未查找到要求的数值时,输出一个NULL指针 * 用两个版本实现,一个用的是数组小标,第二个用的是指针 * 他们均采用了不对称边界 * Copyright (c) 2012 LiMing Author: LiMing 2012-06-21 referenced C Traps and Pitfaills Chinese Edition Page 132-137 * * 查找的元素为x,数组下表是k,开始时0 <= k < n * 接下来缩小范围lo <= k < hi, * if lo equals hi, we can justify the element "x" is not in the array * */ #include <stdio.h> int array[] = { 0,1,2,3,4,5,6,7 }; int *bsearch_01(int *t, int n, int x); int *bsearch_01(int *t, int n, int x) { int lo = 0; int hi = n; while(lo < hi) { //int mid = (hi + lo) / 2; int mid = (hi + lo) >> 1; if(x < t[mid]) hi = mid; else if(x > t[mid]) lo = mid + 1; else return t + mid; } return NULL; } int *bsearch_02(int *t, int n, int x); int *bsearch_02(int *t, int n, int x) { int lo = 0; int hi = n; while(lo < hi) { //int mid = (hi + lo) / 2; int mid = (hi + lo) >> 1; int *p = t + mid; //用指针变量p存储t+mid的值,这样就不需要每次都重新计算 if(x < *p) hi = mid; else if(x > *p) lo = mid + 1; else return p; } return NULL; } //进一步减少寻址运算 /* * Suppose we want to reduce address arithmetic still further * by using pointers instead of subscripts throughout the program. * * */ int *bsearch_03(int *t, int n, int x); int *bsearch_03(int *t, int n, int x) { int *lo = t; int *hi = t + n; while(lo < hi) { //int mid = (hi + lo) / 2; int *mid = lo + ((hi - lo) >> 1); if(x < *mid) hi = mid; else if(x > *mid) lo = mid + 1; else return mid; } return NULL; } /* * The resulting program looks somewhat neater because of the symmetry * */ int *bsearch_04(int *t, int n, int x); int *bsearch_04(int *t, int n, int x) { int lo = 0; int hi = n - 1; while(lo <= hi) { //int mid = (hi + lo) / 2; int mid = (hi + lo) >> 1; if(x < t[mid]) hi = mid - 1; else if(x > t[mid]) lo = mid + 1; else return t + mid; } return NULL; } int main(int argc, char **argv) { int * ret = NULL; int *ret2 = NULL; int *ret3 = NULL; int *ret4 = NULL; ret = bsearch_01(array, 8, 3); ret2 = bsearch_02(array, 8, 6); ret3 = bsearch_03(array, 8, 4); ret4 = bsearch_04(array, 8, 2); printf("The result is %d\n", *ret); printf("The result is %d\n", *ret2); printf("The result is %d\n", *ret3); printf("The result is %d\n", *ret4); printf("hello world\n"); return 0; }
相关文章推荐
- C语言二分查找法(指针和数组实现)
- 用C语言关于学生管理系统的几种实现方法(一位数组,二维数组,指针,结构体)
- 第22节 C语言结构体之结构体嵌套、结构体指针与结构体数组的代码实现
- 用c语言指针实现给整形数组冒泡排序
- C语言通过指针和数组实现字符串倒序
- C语言中使用指针实现数组排序
- C语言用函数指针的数组简单实现计算器功能
- C语言用指针实现两个数组值互换
- 一.数组和指针的差别二.函数的好处以及使用函数的注意项。三.用c语言如何实现面向对象?c语言哪些点体现了面向对象。
- 用c语言指针实现给整形数组冒泡排序
- 水滴石穿C语言之指针、数组和函数(转载)
- C语言中的指针与数组
- C语言专题――数组与指针
- 用C语言来实现的类似C++函数的重载特性-----void*指针闲谈
- C语言中指针和数组名小趣
- C语言之指针、数组和函数
- C语言数组和指针的剖析
- C语言之指针、数组和函数
- C语言中字符数组和字符串指针分析
- C语言之指针、数组和函数