实验目的:
(1)掌握折半查找和二叉排序树两种查找方法;
(2)掌握各种不同的排序方法。
实验内容:
(1)编程实现两种查找方法:折半查找和二叉排序树;
若查找成功,返回元素在有序数组中的位置和查找次数;
若查找失败,返回出错标志和查找次数。
(2)从教材中选择两种排序算法进行编程实现。
实验代码:
#include<iostream>
using namespace std;
typedef struct{
int *R;
int length;
}STable;
int Init(STable &ST)
{
ST.R=new int[100];
if(!ST.R) return 0;
ST.length=0;
return 1;
}
int Create(STable &ST)
{
int n;
cout<<"输入元素个数:"<<endl;
cin>>n;
ST.length=n;
cout<<"输入元素:"<<endl;
for(int i=0;i<ST.length;i++)
{
int e;
cin>>e;
ST.R[i]=e;
}
}
int Search_Bin(STable ST,int key,int &count)
{
int low=0;
int high=ST.length-1;
while(low<=high)
{
int mid=(low+high)/2;
count++;
if(key==ST.R[mid]) return mid;
else if (key<ST.R[mid]) high=mid-1;
else low=mid+1;
}
return -1;
}
//二叉排序树
typedef struct BSTNode
{
int data;
struct BSTNode *l,*r;
}BSTNode,*BSTree;
//二叉排序树的创建
void InsertBST(BSTree &T,int e)
{
if(!T)
{
BSTNode *S=new BSTNode;
S->data=e;
S->l=S->r=NULL;
T=S;
}
else if(e<T->data)
{
InsertBST(T->l,e);
}
else
{
InsertBST(T->r,e);
}
}
void CreateBST(BSTree &T,int n,int a[])
{
T=NULL;
for(int i=0;i<n;i++)
{
InsertBST(T,a[i]);
}
}
BSTree SearchBST(BSTree &T,int key,int &count)
{
BSTree q;
q=T;
while(q)
{
count++;
if(q->data==key)
{
return q;
q=NULL;
}
else if(q->data>key)
{
q=q->l;
}
else
{
q=q->r;
}
}
return NULL;
}
void midTraverse(BSTree &T)
{
if (T)
{
midTraverse(T->l);
cout<<T->data<<" ";
midTraverse(T->r);
}
}
//直接插入排序
void InsertSort(int a[],int l)
{
int temp;
int j;
for(int i=1;i<l;i++)
{
if(a[i]<a[i-1])
{
temp=a[i];
for(j=i-1;j>=0&&temp<a[j];j--)
{
a[j+1]=a[j];
}
a[j+1]=temp;
}
}
}
//快速排序
void QuickSort(int *num,int l,int r){
if(l+1>=r){
return ;
}
int first=l,last=r-1,key=num[first];
while(first<last){
while(first<last&&num[last]>=key){
--last;
}
num[first]=num[last];
while(first<last&&num[first]<key){
++first;
}
num[last]=num[first];
}
num[first]=key;
QuickSort(num,l,first);
QuickSort(num,first+1,r);
}
int main()
{
cout<<"二叉排序树:"<<endl;
BSTree T;
int num;
cout<<"请输入元素个数:"<<endl;
cin>>num;
cout<<"请输入元素:"<<endl;
int *arr=new int[num];
for(int i=0;i<num;i++)
{
cin>>arr[i];
}
CreateBST(T,num,arr);
cout<<"中序遍历二叉搜索树结果:"<<endl;//可不写
midTraverse(T);
cout<<endl;
int key;
cout<<"请输入要查找的元素:"<<endl;
cin>>key;
int count=0;
BSTree result=SearchBST(T,key,count);
if(result==NULL)
{
cout<<"查找失败,查找次数为:"<<count-1<<endl;
}
else{
cout<<"查找成功,查找值为:"<<result->data<<",查找次数为:"<<count-1<<endl;
}
cout<<"折半查找:"<<endl;
STable ST;
Init(ST);
Create(ST);
count=0;
int key2;
cout<<"请输入要查找的元素:"<<endl;
cin>>key2;
int result2=Search_Bin(ST,key2,count);
cout<<"count:"<<count<<endl;
if(result2==-1)
{
cout<<"查找失败,查找次数为:"<<count<<endl;
}
else
{
cout<<"查找成功,查找位置为:"<<result2+1<<",查找次数为:"<<count<<endl;
}
//排序
int a[10]={2,5,8,3,6,9,1,4,7};
cout<<"原数组:"<<endl;
for(int k=0;k<10;k++)
cout<<a[k]<<" ";
InsertSort(a,10);
cout<<"\n使用插入排序后:"<<endl;
for(int k=0;k<10;k++)
cout<<a[k]<<" ";
cout<<endl;
int b[10]={2,5,9,3,6,7,4,1,0,8};
cout<<"原数组:"<<endl;
for(int k=0;k<10;k++)
cout<<b[k]<<" ";
QuickSort(b,0,10);
cout<<"\n使用快速排序后:"<<endl;
for(int k=0;k<10;k++)
cout<<a[k]<<" ";
return 0;
}