-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
#include <bits/time.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <sys/time.h> | ||
#include <time.h> | ||
|
||
/* binsearch: find x in v[0] <= v[1] <= ... <= v[n - 1] */ | ||
int binsearch(int x, int v[], int n); | ||
int binsearch2(int x, int v[], int n); | ||
void genrandseq(int v[], int n); | ||
void test(char fname[], int (*f)(int x, int v[], int n), int v[], int n); | ||
|
||
int main() | ||
{ | ||
const int n = (int)1e4; | ||
int *v = malloc(sizeof(int) * n); | ||
|
||
test("binsearch", binsearch, v, n); | ||
test("binsearch2", binsearch2, v, n); | ||
|
||
return 0; | ||
} | ||
|
||
int binsearch(int x, int v[], int n) | ||
{ | ||
int mid = 0; | ||
int 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; | ||
} | ||
|
||
int binsearch2(int x, int v[], int n) | ||
{ | ||
int mid = 0; | ||
int low = 0, high = n - 1; | ||
while (low < high) { | ||
mid = (low + high) / 2; | ||
|
||
if (x <= v[mid]) { | ||
high = mid; | ||
} else { | ||
low = mid + 1; | ||
} | ||
} | ||
|
||
if (v[low] == x) return low; | ||
return -1; | ||
} | ||
|
||
void genrandseq(int v[], int n) | ||
{ | ||
srand(time(NULL)); | ||
for (int i = 0; i < n; ++i) { | ||
int base = 0; | ||
if (i > 0) base = v[i - 1]; | ||
v[i] = base + rand() / (RAND_MAX / 3); | ||
} | ||
} | ||
|
||
void test(char fname[], int (*f)(int x, int v[], int n), int v[], int n) | ||
{ | ||
const int run = (int)1e4; | ||
struct timespec start, stop; | ||
int tsum = 0; | ||
|
||
for (int i = 0; i < run; ++i) { | ||
genrandseq(v, n); | ||
int target = v[rand() % n]; | ||
clock_gettime(CLOCK_MONOTONIC_RAW, &start); | ||
f(target, v, n); | ||
clock_gettime(CLOCK_MONOTONIC_RAW, &stop); | ||
int elapse = (stop.tv_sec - start.tv_sec) * 1e6 + (stop.tv_nsec - start.tv_nsec) / 1e3; | ||
tsum += elapse; | ||
} | ||
|
||
printf("`%s` used %d ms\n", fname, tsum); | ||
} |