Created
November 12, 2009 06:19
-
-
Save shaobin0604/232653 to your computer and use it in GitHub Desktop.
tcpl_ex-3-1.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Exercise 3-1. Our binary search makes two tests inside the loop, when one | |
* would suffice (at the price of more tests outside.) Write a version with | |
* only one test inside the loop and measure the difference in run-time. | |
*/ | |
#include <stdio.h> | |
#include <time.h> | |
#define MAX_ELEMENT 20000 | |
int binsearch2(int x, int v[], int n); | |
int binsearch(int x, int v[], int n); | |
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 - 1; | |
else | |
low = mid + 1; | |
mid = (low + high) / 2; | |
} | |
if (x == v[mid]) | |
return mid; | |
else | |
return -1; | |
} | |
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; | |
} | |
/* 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 = MAX_ELEMENT/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,000 iterations of binsearch() */ | |
for ( i = 0, time_taken = clock(); i < 100000; ++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 < 100000; ++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; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment