111

来源:互联网 发布:淘宝如何设置去提问 编辑:程序博客网 时间:2024/06/06 16:33
#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*/elsereturn 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 itelsereturn 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 leftelse if (a[mid]>N)     //N is in the right of a[mid]high = mid - 1;    //search rightelse 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 itreturn 1;else if (a[mid] < N)   //N is in the left of a[mid]return RecursiveBinarySearch(a, N, low, mid - 1);    //search rightelse if (a[mid] > N)return RecursiveBinarySearch(a, N, mid + 1, high);    //N is in the right of a[mid] and search right}elsereturn 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 functionint K = 0;    //to judge how many times the function will runint *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 testedprintf("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 resultprintf("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-1K = 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 funcitonstop = clock();ticks = (double)(stop - start);    totletime = ticks / CLK_TCK;     //change the totaltime from ticks to secondsduration = totletime / K;    //get the time of running the funtion onceprintf("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-1K = 10000000;start = clock();for (r = 0; r < K; r++) p = RecursiveBinarySearch(a, n[i2], 0, n[i2] - 1);//run funcitonstop = 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 funcitonstop = 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-1K = 50000;start = clock();for (r = 0; r < K; r++) p = RecursiveSequnentialSearch(0, a, n[i4]);//run funcitonstop = 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 outputint 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 chosenscanf_s("%d", &A);printf("Please input N\n");int N = 0;scanf_s("%d", &N);     //get the test number Na = (int*)malloc(sizeof(int) * N);for (i = 0; i < N; i++) a[i] = i;    //set the array from 0 to N-1free(a);printf("How many times do you want the algorithm to run?\n");scanf_s("%d", &K);if (A == 1) {  //Iterative Binary Searchstart = 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 Searchstart = 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 Searchstart = 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 Searchstart = 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);}elseprintf("Wrong input!\n");   //some wrong in the inputprintf("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;}