查找和排序的实现
实验内容:
(1)编程实现两种查找方法:折半查找和二叉排序树。若查找成功,返回元素在有序数组中的位置和查找次数;若查找失败,返回出错标志和查找次数;
(2)在9种排序算法(直接插入排序、折半插入排序、希尔排序、冒泡排序、快速排序、简单选择排序、堆排序、归并排序和基数排序)中选择5种排序算法进行编程实现。
#include<iostream>
using namespace std;
typedef int KeyType;
typedef int InfoType;
typedef struct
{
KeyType key;
InfoType otherinfo;
} ElemType;
typedef struct
{
ElemType R[20];
int length;
} SSTable;
typedef struct BSTNode
{
ElemType data;
struct BSTNode* lchild, * rchild;
} BSTNode,*BSTree; //BSTNode为节点,BSTree为结构体指针
//创建二叉排序树
int Search_Bin(SSTable ST,KeyType key)//折半查找
{
int low=0;
int high=ST.length-1;
int mid;
int locate;
int count=0;
while(low<=high)
{
mid=(low+high)/2;
count++;
if(key==ST.R[mid].key)
{
break;
}
else if(key<ST.R[mid].key)
{
high=mid-1;
}
else
{
low=mid+1;
}
}
locate=mid+1;
if(ST.R[mid].key==key)
{
cout<<"查找成功"<<" "<<"一共查找了"<<count+1<<"次"<<endl;
cout<<"查找的位置为"<<locate<<endl;
}
else
{
cout<<"查找失败"<<" "<<"一共查找了"<<count<<"次"<<endl;
}
}
void CreateBSTree(BSTree& T,KeyType e)
{
if (!T)
{
BSTree S;
S = new BSTNode;
S->data.key = e;
S->lchild = NULL;
S->rchild = NULL;
T = S;
}
else if (e<T->data.key)
{
CreateBSTree(T->lchild,e);
}
else if (e>T->data.key)
{
CreateBSTree(T->rchild,e);
}
}
void InOrderTraverse(BSTree T)//中序遍历的递归算法
{
if(T)
{
InOrderTraverse(T->lchild);
cout<<T->data.key<< " ";
InOrderTraverse(T->rchild);
}
}
int count1 = 0,count2=0;
//二叉排序树的查找
BSTree SearchBST(BSTree T, KeyType key)
{
if ((!T) || key == T->data.key)
{
count2++;
return T;
}
else if(key<T->data.key)
{
return SearchBST(T->lchild,key);
}
else
{
return SearchBST(T->rchild, key);
}
}
int main()
{
cout<<"折半查找算法"<<endl;
SSTable ST;
int i,n1,k,key1;
cout<<"输入表的长度:";
cin>>ST.length;
cout<<"请输入一个有序的数列"<<endl;
for(i=0; i<ST.length; i++)
{
cin>>ST.R[i].key;
}
for(int i=0; i<2; i++)
{
cout<<"请输入要查找的数字:";
cin>>key1;
cout<<endl;
Search_Bin(ST,key1);
}
cout<<endl;
cout<<endl;
cout<<"二叉排序树查找"<<endl;
BSTree T =NULL;
int n2;
cout << "请输入结点总数:";
cin >> n2;
//int a[666];
cout << "请分别输入各个结点:";
int e;
for (int i = 0; i < n2; i++)
{
cin >> e;
CreateBSTree(T,e);
}
cout<<"输出二叉排序树的结果:"<<endl;
InOrderTraverse(T);
cout<<endl;
for(int i=0; i<2; i++)
{
cout << "请输入要查找的元素:";
int key;
cin >> key;
BSTree address = SearchBST(T, key);
if (!address)
{
cout << "该元素不存在,比较次数为:" << count1-5;
}
else
{
cout <<"查找成功,比较次数为" <<count2<< "次"<<endl;
}
}
return 0;
}
#include <iostream>
using namespace std;
typedef int KeyType;
typedef int InfoType;
typedef struct
{
KeyType key;
InfoType otherinfo;
} ElemType;
typedef struct BSTNode
{
ElemType data;
struct BSTNode* lchild, * rchild;
} BSTNode,*BSTree; //BSTNode为节点,BSTree为结构体指针
//创建二叉排序树
void CreateBSTree(BSTree& T,KeyType e)
{
if (!T)
{
BSTree S;
S = new BSTNode;
S->data.key = e;
S->lchild = NULL;
S->rchild = NULL;
T = S;
}
else if (e<T->data.key)
{
CreateBSTree(T->lchild,e);
}
else if (e>T->data.key)
{
CreateBSTree(T->rchild,e);
}
}
void InOrderTraverse(BSTree T)//中序遍历的递归算法
{
if(T)
{
InOrderTraverse(T->lchild);
cout<<T->data.key<< " ";
InOrderTraverse(T->rchild);
}
}
int count = 0,count2=0;
//二叉排序树的查找
BSTree SearchBST(BSTree T, KeyType key)
{
if ((!T))
{
return T;
}
if( key == T->data.key)
{
count2=count2+2;
return T;
}
count=count+1;
if(key<T->data.key)
{
return SearchBST(T->lchild,key);
}
else
{
return SearchBST(T->rchild, key);
}
}
int main()
{
BSTree T =NULL;
int n2;
cout << "请输入结点总数:";
cin >> n2;
//int a[666];
cout << "请分别输入各个结点:";
int e;
for (int i = 0; i < n2; i++)
{
cin >> e;
CreateBSTree(T,e);
}
cout<<"输出二叉排序树的结果:"<<endl;
InOrderTraverse(T);
cout<<endl;
while(1)
{
cout << "请输入要查找的元素:";
int key;
cin >> key;
BSTree address = SearchBST(T, key);
if (!address)
{
cout << "该元素不存在,比较次数为:" << count;
}
else
{
cout <<"查找成功,比较次数为" <<count2<< "次"<<endl;
}
}
}
#include<iostream>
#include<cstdlib>
#include<cmath>
using namespace std;
#define MAXSIZE 20
typedef int KeyType;
typedef int InfoType;
typedef struct
{
KeyType key;
InfoType otherinfo;
} RedType;
typedef struct
{
RedType r[MAXSIZE+1];
int length;
} SqList;
void InsertSort(SqList &L)//直接插入排序法
{
int i,j;
for(i=2; i<=L.length; i++)
if(L.r[i].key<L.r[i-1].key)
{
L.r[0]=L.r[i];
L.r[i]=L.r[i-1];
for( j=i-2; L.r[0].key<L.r[j].key; --j)
L.r[j+1]=L.r[j];
L.r[j+1]=L.r[0];
}
}
void BInsertSort(SqList &L)//折半插入排序法
{
int i,j,low,high,m;
for(i=2; i<=L.length; i++)
{
L.r[0]=L.r[i];
low=1;
high=i-1;
while(low<=high)
{
m=(low+high)/2;
if(L.r[0].key<L.r[m].key)high=m-1;
else low=m+1;
}
for(j=i-1; j>=high+1; --j)L.r[j+1]=L.r[j];
L.r[high+1]=L.r[0];
}
}
void BubbleSort(SqList &L)//冒泡排序
{
int m,flag,i,j;
RedType t;
m=L.length-1;
flag=1;
while((m>0)&&(flag==1))
{
flag=0;
for(j=1; j<=m; j++)
{
if(L.r[j].key>L.r[j+1].key)
{
flag=1;
t=L.r[j];
L.r[j]=L.r[j+1];
L.r[j+1]=t;
}
}
--m;
}
}
int Partition(SqList &L,int low,int high)//快速排序
{
int pivotkey;
L.r[0]=L.r[low];
pivotkey=L.r[low].key;
while(low<high)
{
while(low<high&&L.r[high].key>=pivotkey)--high;
L.r[low]=L.r[high];
while(low<high&&L.r[low].key<=pivotkey)++low;
L.r[high]=L.r[low];
}
L.r[low]=L.r[0];
return low;
}
void QSort(SqList &L,int low,int high)
{
int pivotkey;
if(low<high)
{
pivotkey=Partition(L,low,high);
QSort(L,low,pivotkey-1);
QSort(L,pivotkey+1,high);
}
}
void QuickSort(SqList &L)
{
QSort(L,1,L.length);
}
void SelectSort(SqList &L)//选择排序
{
int i,j,k;
RedType t;
for(i=1; i<L.length; i++)
{
k=i;
for(j=i+1; j<=L.length; ++j)
if(L.r[j].key<L.r[k].key)k=j;
if(k!=i)
{
t=L.r[i];
L.r[i]=L.r[k];
L.r[k]=t;
}
}
}
void print(SqList &L)
{
int i;
for(i=1; i<=L.length; i++)
{
cout<<L.r[i].key<<" ";
}
cout<<endl;
}
int main()
{
SqList L;
int a;
int n,i;
while(1)
{
cout<<"请输入一个数,表示线性表长度"<<endl;
cin>>n;
L.length=n;
cout<<"请输入线性表元素"<<endl;
for(i=1; i<=n; i++)
{
cin>>L.r[i].key;
}
cout<<"请输入一个数字,1表示直接插入,2表示折半插入,3表示冒泡排序,4表示快速排序,5表示选择排序"<<endl;
cin>>a;
if(a==1)
{
cout<<"直接插入排序法结果;"<<endl;
InsertSort(L);
for(i=1; i<=L.length; i++)
cout<<L.r[i].key<<" ";
cout<<endl;
}
if(a==2)
{
cout<<"折半插入排序法结果: "<<endl;
BInsertSort(L);
print(L);
}
if(a==3)
{
cout<<"冒泡排序法结果: "<<endl;
BubbleSort(L);
print(L);
}
if(a==4)
{
cout<<"快速排序法结果: "<<endl;
QuickSort(L);
print(L);
}
if(a==5)
{
cout<<"选择排序法结果: "<<endl;
SelectSort(L);
print(L);
}
}
return 0;
}