Experimental requirements
Generate 100 key values of data elements to be searched with random numbers.
-
Test the actual machine execution time for each of the following sort functions:
(1) Sequential search
(2) Binary sort tree search
(3) Half search
Tips:
(1) And (2) use the same experimental data; (3) the data elements must be ordered, so the sorting algorithm should be used first (bubble sorting can be used)
Program code
#include <bits/stdc++.h>
#define nmax 100
#define maxtime 5000000
#define EQ(a,b) ((a) == (b))
#define LT(a,b) ((a) < (b))
#define LQ(a,b) ((a) <= (b))
#define ElemType int
#define KeyType int
#define TElemType int
#define Status int
#define ERROR 0
#define OK 1
#define FALSE 0
#define TRUE 1
using namespace std;
int cnt = 0;
typedef struct{
ElemType *elem;
int length;
}SSTable;
typedef struct BiTNode{
TElemType data;
struct BiTNode * lchild, * rchild;
}BiTNode, *BiTree;
int Search_Seq(SSTable ST, KeyType key)
{
ST.elem[0] = key;
int i;
for(i=ST.length;!EQ(ST.elem[i],key);--i);
return i;
}
void init(SSTable &st)
{
st.elem = (int*)malloc(sizeof(int) * (nmax)+1);
st.length = nmax;
srand((int)time(NULL));
for(int i = 1; i<=nmax;++i){
st.elem[i] = rand()%500;
}
}
int Search_Bin(SSTable st, KeyType key)
{
int low = 1, high = st.length;
while(low<=high){
int mid = (low + high) /2;
if(EQ(key,st.elem[mid])) return mid;
else if(LT(key,st.elem[mid])) high = mid-1;
else low = mid+1;
}
return 0;
}
Status SearchBST(BiTree T,KeyType key, BiTree f, BiTree & p)
{
if(!T) {p = f; return false;}
else if(EQ(key,T->data)) { p = T;return true;}
else if(LT(key,T->data)) return SearchBST(T->lchild,key,T,p);
else return SearchBST(T->rchild,key,T,p);
}
int Search_Bin_None(SSTable st, KeyType key,int low , int high)
{
int mid = (low+high)/2;
if(low>=high) return ERROR;
if(EQ(key,st.elem[mid])) return mid;
else if(LT(key,st.elem[mid])) return Search_Bin_None(st,key,low,mid-1);
else return Search_Bin_None(st,key,mid+1,high);
}
void printtime(double start, double finish,int &cnt )
{
double duration;
duration = (double)((finish - start) / CLOCKS_PER_SEC);
printf("Total time consuming%fsecond\n",duration);
printf("Where search succeeded%dsecond\n Search failed%dsecond\n\n",cnt,maxtime - cnt);
cnt = 0;
}
Status InsertBST(BiTree & T, ElemType e)
{
BiTree p;
if(!SearchBST(T,e,NULL,p)){
BiTree s = (BiTree) malloc(sizeof(BiTNode));
s->data = e; s->lchild = s->rchild = NULL;
if(!p) T = s;
else if(LT(e,p->data)) p->lchild = s;
else p->rchild = s;
return true;
}
else return false;
}
void InorderTraverse(BiTree T)
{
if(T){
InorderTraverse(T->lchild);
cnt++;
printf("%d ",T->data);
if(cnt %10 ==0) printf("\n");
InorderTraverse(T->rchild);
}
}
void findelem(SSTable st,BiTree T)
{
init(st);
double start = 0,finish = 0;
printf("Sequential search%dSecondary case\n",maxtime);
start = clock();
int cnt = 0;
for(int i = 0; i<maxtime;++i){
if(Search_Seq(st,(i+(rand()%100)/3+1)%750)!=0)
cnt++;
}
finish = clock();
printtime(start,finish,cnt);
printf("Binary search (non recursive)%dSecondary case\n",maxtime);
sort(st.elem,st.elem+nmax);
start = clock();
for(int i = 0; i<maxtime;++i){
if(Search_Bin(st,(i+(rand()%100)/3+1)%750)){
cnt++;
}
}
finish = clock();
printtime(start,finish,cnt);
printf("Binary search (recursive)%dSecondary case\n",maxtime);
start = clock();
for(int i = 0; i<maxtime;++i){
if(!Search_Bin_None(st,(i+(rand()%100)/3+1)%750,1,st.length)){
cnt++;
}
}
finish = clock();
printtime(start,finish,cnt);
printf("Binary sort tree search%dSecondary case\n",maxtime);
for(int i = nmax/2;i>=0;--i){
InsertBST(T,st.elem[nmax/2-i]);
InsertBST(T,st.elem[nmax/2+i]);
}
printf("The result of order traversal in binary sort tree is\n",maxtime);
InorderTraverse(T);
printf("\n");
for(int i = 0; i<maxtime;++i){
if(InsertBST(T,(i+(rand()%100)/3+1)%750)){
cnt++;
}
}
finish = clock();
printtime(start,finish,cnt);
}
int main()
{
SSTable st;
BiTree T = (BiTree)malloc(sizeof(BiTNode));
T->lchild = T->rchild = NULL;
init(st);
findelem(st,T);
return 0;
}