您的位置:首页 > 编程语言 > C语言/C++

练习 3-1 在上面有关折半查找的例子中,while循环语句内共执行了两次测试,其实只要一次就足够(代价是将更多的测试在循环外执行)。重写该函数,使得在循环内部只执行一次测试。

2017-08-08 22:06 736 查看
原文中的折半查找函数的例子如下:

/* 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c语言
相关文章推荐