算法一:
利用二叉搜索数进行查找数字;
#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();
}