练习 3-1 在上面有关折半查找的例子中,while循环语句内共执行了两次测试,其实只要一次就足够(代价是将更多的测试在循环外执行)。重写该函数,使得在循环内部只执行一次测试。
2017-08-08 22:06
736 查看
原文中的折半查找函数的例子如下:
下面的解法来源于作者的解法,具体见下面的网址:
http://clc-wiki.net/wiki/K%26R2_solutions:Chapter_3:Exercise_1
里面包括了计算执行时间的代码部分,因为实际情况下代码执行速度非常快,所以将折半查找的方法重复了1000000次。运行之后发现,将更多的测试在循环外执行,这样的运算时间更短。原因很简单,减少循环体内的测试,在循环体之外只需要执行一到两次。
/* binsearch: find x in v[0] <= v[1] <= ... <= v[n-1] */ int binsearch(int x, int v[], int n) { int low, high, mid; low = 0; high = n - 1; while (low <= high) { mid = (low+high)/2; if (x < v[mid]) high = mid - 1; else if (x > v[mid]) low = mid + 1; else /* found match */ return mid; } return -1; /* no match */ }
下面的解法来源于作者的解法,具体见下面的网址:
http://clc-wiki.net/wiki/K%26R2_solutions:Chapter_3:Exercise_1
里面包括了计算执行时间的代码部分,因为实际情况下代码执行速度非常快,所以将折半查找的方法重复了1000000次。运行之后发现,将更多的测试在循环外执行,这样的运算时间更短。原因很简单,减少循环体内的测试,在循环体之外只需要执行一到两次。
#include <stdio.h> #include <time.h> int binsearch(int x, int v[], int n); /* Original K&R function */ int binsearch2(int x, int v[], int n); /* Our new function */ #define MAX_ELEMENT 20000 /* Outputs approximation of processor time required for our two binary search functions. We search for the element -1, to time the functions' worst case performance (i.e. element not found in test data) */ int main(void) { int testdata[MAX_ELEMENT]; int index; /* Index of found element in test data */ int n = 10; /* Element to search for */ int i; clock_t time_taken; /* Initialize test data */ for ( i = 0; i < MAX_ELEMENT; ++i ) testdata[i] = i; /* Output approximation of time taken for 100,0000 iterations of binsearch() */ for ( i = 0, time_taken = clock(); i < 1000000; ++i ) { index = binsearch(n, testdata, MAX_ELEMENT); } time_taken = clock() - time_taken; if ( index < 0 ) printf("Element %d not found.\n", n); else printf("Element %d found at index %d.\n", n, index); printf("binsearch() took %lu clocks (%lu seconds)\n", (unsigned long) time_taken, (unsigned long) time_taken / CLOCKS_PER_SEC); /* Output approximation of time taken for 100,000 iterations of binsearch2() */ for ( i = 0, time_taken = clock(); i < 1000000; ++i ) { index = binsearch2(n, testdata, MAX_ELEMENT); } time_taken = clock() - time_taken; if ( index < 0 ) printf("Element %d not found.\n", n); else printf("Element %d found at index %d.\n", n, index); printf("binsearch2() took %lu clocks (%lu seconds)\n", (unsigned long) time_taken, (unsigned long) time_taken / CLOCKS_PER_SEC); return 0; } /* Performs a binary search for element x in array v[], which has n elements */ int binsearch(int x, int v[], int n) { int low, mid, high; low = 0; high = n - 1; while ( low <= high ) { mid = (low+high) / 2; if ( x < v[mid] ) high = mid - 1; else if ( x > v[mid] ) low = mid + 1; else return mid; } return -1; } /* Implementation of binsearch() using only one test inside the loop */ int binsearch2(int x, int v[], int n) { int low, high, mid; low = 0; high = n - 1; mid = (low+high) / 2; while ( low <= high && x != v[mid] ) { if ( x < v[mid] ) high = mid< 4000 /span> - 1; else low = mid + 1; mid = (low+high) / 2; } if ( x == v[mid] ) return mid; else return -1; }
相关文章推荐
- 练习 3-1 在上面有关折半查找的例子中,while循环语句内共执行了两次测试,其实只要一次就足够(代价是将更多的测试在循环外执行)。重写该函数,使得在循环内部只执行一次测试。
- 循环语句至少执行一次do-while
- 编写一段程序,从标准输入读取string对象的序列直到连续出现两个相同的单词或者所有单词都读完为止。使用while循环一次读取一个单词,当一个单词连续出现两次是使用break语句终止循环。输出连续重复出现的单词,或者输出一个消息说明没有人任何单词是重复出现的。
- shell 脚本中while 循环只执行一次的问题
- C#循环语句-先判断后执行-while循环
- 条件判断式if和循环语句while及其函数的应用
- MySQL和MSSQl使用While 语句循环生成测试数据
- python 表达式和语句及for、while循环练习实例
- 循环语句基本练习 求100以内的整数和,奇数偶数和,与其有关的整数和
- MySQL循环语句之while循环测试
- 一次寻常测试While循环碰到“ * ”——字串处理方式
- 练习2-2 在不使用运算符&&或者||的条件下编写一个与上面的for循环语句等价的循环语句。
- PDOstament对象执行execute()函数,只要是sql语句正确都是返回true
- while循环语句中break和continue语句的作用范围测试
- C/C++中,写在while循环体的cin.getline()语句执行一次问题
- 练习 2-2 在不使用运算符&&或||的条件下编写一个与上面的for 循环语句等价的循环语句。
- 15个常用sql语句 分支 循环 子查询 存储过程 事务 常用函数 sql语句执行过程(转载备用)
- java for循环里面执行sql语句操作,有效结果只有一次,只执行了一次sql
- java for循环里面执行sql语句操作,有效结果只有一次,只执行了一次sql mybatis 循环执行update生效一次 实际只执行一次
- 练习2-2 在不使用运算符&&或|| 的条件下编写一个与上面的for循环语句等价的循环语句