查找数字的三种算法的时间

 算法一:

利用二叉搜索数进行查找数字;

#include <cstdio>
#include <cstdlib>
typedef struct Node {
	int data;
	Node *left,*right;
	Node *parent;
}BinTreeNode, *BinTree;

void insert(BinTree *root,int key) {
	BinTree p = (BinTree)malloc(sizeof(BinTreeNode));
	p->data = key;
	p->left = NULL;
	p->right = NULL;
	p->parent = NULL;

	if ((*root) ==NULL) {
		*root = p;
		return ;
	}
	if ((*root)->left == NULL && (*root)->data > key) {
		p->parent = (*root);
		(*root)->left = p;
		return ;
	}
	if ((*root)->right == NULL && (*root)->data < key) {
		p->parent = (*root);
		(*root)->right = p;
		return ;
	}

	if ((*root)->data > key) {
		insert(&(*root)->left,key);
	}

	else if ((*root)->data < key) 
		insert(&(*root)->right,key);
	else
		return ;
} 

BinTree search(BinTree root,int key) {
	if (root == NULL)
		return NULL;
	if (key > root->data)
		return search(root->right,key);
	else if (key < root->data)
		return search(root->left,key);
	else 		
		return root;
}  


void creat(BinTree *T,int *a,int len) {
	for (int i = 0; i <len; i++)
		insert(T,a[i]);
} 

算法二:

运用二分查找法进行查找数字,前提是该线性表必须是有序的排列;


#include <cstdio>
template <class T,class E> 
int BinSearch(T *array, int low, int high, E target) {	
	while (high >= low) {
		int  mid = (high + low) / 2;
		if (target < array[mid]) high = mid - 1;
		else if (target > array[mid]) low = mid +1;
		else return mid + 1;
	}
	return -1;
}
template <class T>  //进行排列(冒泡法)
void BubbleSort(T *list,int len) 
{ 
	int i,j,temp; 
	for(i=0;i<len;i++) 
		for(j=0;j<len-i;j++) 
		{ 
			if(list[j]>list[j+1]) 
			{ 
				temp=list[j]; 
				list[j]=list[j+1]; 
				list[j+1]=temp; 
			} 
		} 
} 



算法三:

最普通的查找法:


#include <cstdio>
#define N 100000
template <class T,class E>
int Search(T *array, E target) {
	int flag = -1;
	for (int i = 0; i < N ;i++) {
		if (array[i] == target) {
			flag = i+1;
			break;
		}	 
}
		return flag;
}

那么之后就需要把数据创建,以及像扑克牌那样打乱;


#include <cstdio >
#include <cstdlib>


void swap(int& a, int& b ){
	a = a^b;
	b = a^b;
	a = a^b;

}

size_t  shuffle22(int *s, int n)
{

	size_t  t=0;
	int i;
	for ( i=0; i<n; i++){
		t++;
		s[i] = i;

	}

	for (i=n-1; i>0; --i) {
		t++;
		int num = rand()%(i+1);
		if (num != i) {
			swap(s[num],s[i]);
		}

	}

	return t;

}



之后就是main.cpp


#include <iostream>
#include "Binsearch.h"
#include <cstring>
#include <ctime>
#include <sys/timeb.h>
#include "Bintreesearch.h"
#include "rand.h"
#include "search.h"
#define N  100000
using namespace std;
int k;
int target = 9999;
struct timeb startTime , endTime;

void BT() {

	 int *s = new int [N];
	for (int i = 0; i < N; i++) 
		s[i] = i;
	shuffle22(s,N);

	BinTree root = NULL, p;
	creat(&root,s,N);
	ftime(&startTime);
	for (int i = 0; i < N; i++)  
	  p = search(root,target);
	ftime(&endTime);
 	if (p == NULL) printf("No find\n");
 	else printf("Find the number %d\n",p->data);
	int t = (endTime.time-startTime.time)*1000 + (endTime.millitm - startTime.millitm);
	cout << "the time is "<<t<< "ms" << endl << endl <<endl ;

}

void sear() {
	 int *s = new int [N];
	for (int i = 0; i < N; i++) 
		s[i] = i;
	shuffle22(s,N);
	ftime(&startTime);
	for (int i = 0; i < N; i++) 
	    k = Search<int,int>(s,target);
	ftime(&endTime);
	if (k == -1) printf("No find\n");
		else printf("Find the number %d in %d\n",target,k);
	int t = (endTime.time-startTime.time)*1000 + (endTime.millitm - startTime.millitm);
	cout << "the time is "<<t<< "ms" << endl << endl << endl;
}
void BS() {
	 int *s = new int [N];
	for (int i = 0; i < N; i++) 
		s[i] = i;
	shuffle22(s,N);

	BubbleSort<int>(s,N);
	ftime(&startTime);
	int k;
	for (int i = 0; i < N; i++) 
	  k = BinSearch<int ,int> (s,0,N-1,target);
	ftime(&endTime);
	if (k == -1) printf("No find\n");
		else printf("Find the number %d in %d\n",target,k);
	int t = (endTime.time-startTime.time)*1000 + (endTime.millitm - startTime.millitm);
	cout << "the time is "<<t<< "ms" << endl << endl << endl;
}

int main () {
	printf ("      N = 100000\n\n\n");  
	printf("无序线性表中查找元素number, N 次需要的时间是(N个数据):\n");
    sear();
	printf("二分查找中查找元素number, N 次需要的时间是(N个数据):\n");
    BS();
	printf("二叉搜索树查找元素number, N 次需要的时间是(N个数据): \n");
	BT();

}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值