#include <stdio.h>
#include <time.h>
#include <malloc.h>
clock_t start, stop; /* clock_t is a built-in type for processor time (ticks) */
double duration; /* records the run time (seconds) of a function once */
double totletime; /* records the totle time */
double ticks; /* records the totle run time (ticks)*/
int IterativeSequnentialSearch(int a[], int N) {
int flag = 0; /* flag use to judge whether the N have been found */
int i = 0;
for (i = 0; i<N; i++) {
if (a[i] == N)
flag = 1; /*flag=1 means N have been found*/
}
if (flag == 0)
return 1; /*return 1 means find N, return 0 means cannot find*/
else
return 0;
}
int RecursiveSequnentialSearch(int index, int a[], int N) { /*the function will test each nummber in the array whether it is N, the variable 'index' is 0 at first*/
if (index >= N)
return 0; /*when index is N, it overflow this array. so this situation means not found.*/
else if (a[index] == N)
return 1; //when this number of the array is N, it means found it
else
return RecursiveSequnentialSearch(index + 1, a, N); //when this number is not N, then it test the next number
}
int IterativeBinarySearch(int N, int a[]) {
int low, mid, high;
low = 0;
high = N - 1;
while (low <= high) {
mid = (low + high) / 2;
if (a[mid]<N) //N is in the left of a[mid]
low = mid + 1; //search left
else if (a[mid]>N) //N is in the right of a[mid]
high = mid - 1; //search right
else return 1; //found it
}
return 0; //not found
}
int RecursiveBinarySearch(int a[], int N, int low, int high) {
int mid = (low + high) / 2;
if (low < high) {
if (a[mid] == N) //find it
return 1;
else if (a[mid] < N) //N is in the left of a[mid]
return RecursiveBinarySearch(a, N, low, mid - 1); //search right
else if (a[mid] > N)
return RecursiveBinarySearch(a, N, mid + 1, high); //N is in the right of a[mid] and search right
}
else
return 0;
}
int main()
{
int m = 0; //to judge whether to output all results togehter or not
int p = 0; //variable p is used to get the return of the function
int K = 0; //to judge how many times the function will run
int *a = NULL;
int i = 0;
int j = 0;
int r = 0;
int n[8] = { 100, 500, 1000, 2000, 4000, 6000, 8000, 10000 }; //set the N which need to be tested
printf("Which kind of results do you want?\n 1.print all the results together(including 4 algorithms with different N)\n 2.To choose the algorithm and print N, then get the result\n Please input the number of types you want to choose (please input 1 or 2)\n");
scanf_s("%d", &m);
if (m == 1) { //output all result
printf("For Binary Search(iterative version):\n");
int i1 = 0;
for (i1 = 0; i1 < 8; i1++) {
a = (int*)malloc(sizeof(int) * n[i1]);
for (j = 0; j < n[i1]; j++) a[j] = j; //set the array from 0 to N-1
K = 10000000;
start = clock(); /* records the ticks at the beginning of the function call */
for (r = 0; r < K; r++) p = IterativeBinarySearch(n[i1], a); //run funciton
stop = clock();
ticks = (double)(stop - start);
totletime = ticks / CLK_TCK; //change the totaltime from ticks to seconds
duration = totletime / K; //get the time of running the funtion once
printf("When N=%5d, K=%d, the Ticks is %3.0f, the Total Time is %7.5f and the Duration is %11.9f\n", n[i1], K, ticks, totletime, duration);
}
printf("\nFor Binary Search(Recursive version):\n");
int i2 = 0;
for (i2 = 0; i2 < 8; i2++) {
a = (int*)malloc(sizeof(int) * n[i2]);
for (j = 0; j < n[i2]; j++) a[j] = j; //set the array from 0 to N-1
K = 10000000;
start = clock();
for (r = 0; r < K; r++) p = RecursiveBinarySearch(a, n[i2], 0, n[i2] - 1);//run funciton
stop = clock();
ticks = (double)(stop - start);
totletime = ticks / CLK_TCK;
duration = totletime / K;
printf("When N=%5d, K=%d, the Ticks is %3.0f, the Total Time is %7.5f and the Duration is %.9f\n", n[i2], K, ticks, totletime, duration);
}
printf("\nFor Sequnential Search(iterative version):\n");
int i3 = 0;
for (i3 = 0; i3 < 8; i3++) {
a = (int*)malloc(sizeof(int) * n[i3]);
for (j = 0; j < n[i3]; j++) a[j] = j;
K = 50000;
start = clock();
for (r = 0; r < K; r++) p = IterativeSequnentialSearch(a, n[i3]);//run funciton
stop = clock();
ticks = (double)(stop - start);
totletime = ticks / CLK_TCK;
duration = totletime / K;
printf("When N=%5d, K=%d, the Ticks is %4.0f, the Total Time is %.5f and the Duration is %.9f\n", n[i3], K, ticks, totletime, duration);
}
printf("\nFor Sequnential Search(Recursiveversion):\n");
int i4 = 0;
for (i4 = 0; i4 < 8; i4++) {
a = (int*)malloc(sizeof(int) * n[i4]);
for (j = 0; j < n[i4]; j++) a[j] = j; //set the array from 0 to N-1
K = 50000;
start = clock();
for (r = 0; r < K; r++) p = RecursiveSequnentialSearch(0, a, n[i4]);//run funciton
stop = clock();
ticks = (double)(stop - start);
totletime = ticks / CLK_TCK;
duration = totletime / K;
printf("When N=%5d, K=%d, the Ticks is %4.0f, the Total Time is %.5f and the Duration is %.9f\n", n[i4], K, ticks, totletime, duration);
}
}
if (m == 2) { //according to the demand to output
int q = 0;
do {
printf("There are four kinds of algorithms to find N in the ordered array a[] as the following\n 1.Iterative Binary Search\n 2.Recursive Binary Search\n 3.Iterative Sequnential Search\n 4.Recursive Sequnential Search\n Please input the number of the algorithm you want to choose (please input 1-4)\n");
int A = 0; //set A to assure which algorithm is chosen
scanf_s("%d", &A);
printf("Please input N\n");
int N = 0;
scanf_s("%d", &N); //get the test number N
a = (int*)malloc(sizeof(int) * N);
for (i = 0; i < N; i++) a[i] = i; //set the array from 0 to N-1
free(a);
printf("How many times do you want the algorithm to run?\n");
scanf_s("%d", &K);
if (A == 1) { //Iterative Binary Search
start = clock();
for (j = 0; j < K; j++) p = IterativeBinarySearch(N, a);
stop = clock();
ticks = (double)(stop - start);
totletime = ticks / CLK_TCK;
duration = totletime / K;
printf("For Binary Search(iterative version) when N=%d run %d times, the Ticks is %.0f, the Total Time is %.5f and the Duration is %.9f\n", N, K, ticks, totletime, duration);
}
else if (A == 2) { //Recursive Binary Search
start = clock();
for (j = 0; j < K; j++) p = RecursiveBinarySearch(a, N, 0, N - 1);
stop = clock();
ticks = (double)(stop - start);
totletime = ticks / CLK_TCK;
duration = totletime / K;
printf("For Binary Search(Recursive version) when N=%d run %d times, the Ticks is %.0f, the Total Time is %.5f and the Duration is %.9f\n", N, K, ticks, totletime, duration);
}
else if (A == 3) { //Iterative Sequnential Search
start = clock();
for (j = 0; j < K; j++) p = IterativeSequnentialSearch(a, N);
stop = clock();
ticks = (double)(stop - start);
totletime = ticks / CLK_TCK;
duration = totletime / K;
printf("For Sequnential Search(iterative version) when N=%d run %d times, the Ticks is %.0f, the Total Time is %.5f and the Duration is %.9f\n", N, K, ticks, totletime, duration);
}
else if (A == 4) { //Recursive Sequnential Search
start = clock();
for (j = 0; j < K; j++) p = RecursiveSequnentialSearch(0, a, N);
stop = clock();
ticks = (double)(stop - start);
totletime = ticks / CLK_TCK;
duration = totletime / K;
printf("For Sequnential Search(Recursiveversion) when N=%d run %d times, the Ticks is %.0f, the Total Time is %.5f and the Duration is %.9f\n", N, K, ticks, totletime, duration);
}
else
printf("Wrong input!\n"); //some wrong in the input
printf("Do you want to try again?(1 means yes, 0 means no)\n Please input 1 or 0\n\n");
scanf_s("%d", &q);
} while (q == 1); // run again
}
return 1;
}
03-14