实验内容:
(1)编程实现两种查找方法:折半查找和二叉排序树。若查找成功,返回元素在有序数组中的位置和查找次数;若查找失败,返回出错标志和查找次数;
(2)在9种排序算法(直接插入排序、折半插入排序、希尔排序、冒泡排序、快速排序、简单选择排序、堆排序、归并排序和基数排序)中选择5种排序算法进行编程实现。
#include <iostream>
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW 2
#define MAXSIZE 100
#define MAXSIZE1 20
typedef int Status;
typedef int KeyType;
typedef int InfoType;
//线性表的查找——折半查找
typedef struct{
KeyType key;
InfoType info;
}ElemType;
//顺序表定义
typedef struct{
ElemType *R;
int length;
}SSTable;
//初始化顺序表
Status InitList(SSTable &ST)
{
ST.R=new ElemType[MAXSIZE];
if(!ST.R) exit(OVERFLOW);
ST.length=0;
return OK;
}
//给顺序表赋值
void Fuzhi(SSTable &ST){
int n;
cout<<"请输入顺序表元素的个数:"<<endl;
cin>>n;
cout<<"依次输入元素:"<<endl;
for(int i=0;i<n;i++){
cin>>ST.R[i].key;
ST.length++;
}
cout<<"赋值成功!"<<endl;
}
//显示顺序表
void getList(SSTable ST){
cout<<"顺序表为:"<<endl;
for(int i=0;i<ST.length;i++){
cout<<ST.R[i].key<<" ";
}
cout<<endl;
}
//折半查找
int Search_Bin(SSTable ST,KeyType key){
int low = 1, high =ST.length;
int num=0,mid;
while (low <= high)
{
mid = (low + high) / 2;
if (key==ST.R[mid].key){
num++;
cout<<"已找到,查找次数为:"<<num<<endl;
return mid;
}else if (key<ST.R[mid].key){
num++;
return high=mid-1;
}else{
num++;
return low = mid + 1;
}
}
cout<<"未找到,查找次数为:"<<num<<endl;
return 0;
}
//二叉排序树查找
//二叉排序树的链表表示
//定义二叉树的结构
typedef struct BSTNode{
char data;
struct BSTNode* lchild, * rchild;
} BSTNode, *BSTree;
//在二叉查找树中插入新结点
bool InsertBST(BSTree &T, KeyType ch){
if (T == NULL) {
T = new BSTNode;
T->data = ch;
T->lchild = T->rchild = NULL;
return true;
}
else if (T->data == ch){
cout << "已存在该元素,请重新输入!" << endl;
return false;
}
else if (ch< T->data)
return InsertBST(T->lchild,ch);
else
return InsertBST(T->rchild,ch);
}
//创建二叉树
BSTree CreateBST(BSTree &T,int a[]){
T = NULL;
int ch;
int n;
cout<<"请输入元素个数:"<<endl;
cin>>n;
for (int i = 0; i < n; ++i){
cout << "请输入第" << i + 1 << "个数字: ";
cin >> ch;
a[i]=ch;
if (!InsertBST(T, ch))
--i;
}
cout << endl;
cout<<"现在数组为:"<<endl;
for (int i = 0; i < n; ++i){
cout << a[i]<<" ";
}
cout<<endl;
return T;
}
//在数组中的位置
int SearchA(int a[],int ch){
for(int i=1;i<=MAXSIZE;i++){
if(a[i]==ch){
cout<<"在数组中为第"<<i+1<<"个元素"<<endl;
break;
}
}
return 0;
}
//二叉排序树的查找
int BSTnum=0;
BSTree SearchBST(BSTree T, int ch){
if (T == NULL || T->data == ch){
cout<<"查找成功"<<endl;
BSTnum++;
cout<<"查找次数为:"<<BSTnum<<endl;
return T;
}
if (ch < T->data){
BSTnum++;
return SearchBST(T->lchild, ch);
}
else{
BSTnum++;
return SearchBST(T->rchild, ch);
}
}
//排序操作
typedef struct{
KeyType key;
InfoType info;
}RedType;
//顺序表定义
typedef struct{
RedType r[MAXSIZE1];
int length;
}SqList;
//给顺序表赋值
void Fuzhi1(SqList &L){
int n;
L.length=0;
cout<<"请输入集合元素的个数:"<<endl;
cin>>n;
cout<<"输入元素:"<<endl;
for(int i=1;i<=n;i++){
cin>>L.r[i].key;
L.length++;
}
cout<<"赋值成功!"<<endl;
}
//输出列表
void getList1(SqList L){
cout<<"顺序表为:"<<endl;
for(int i=1;i<=L.length;i++){
cout<<L.r[i].key<<" ";
}
cout<<endl;
}
//直接插入排序
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];
}
}
cout<<"直接插入排序后,顺序表为:"<<endl;
getList1(L);
}
//折半插入排序
void BInsertSort(SqList L){
int i, j, low, high, m;
for (i = 2; i <=L.length; ++i){
if (L.r[i].key < L.r[i - 1].key){
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]; }
}
cout<<"折半插入排序后,顺序表为:"<<endl;
getList1(L);
}
//希尔排序
void ShellSort(SqList L, int d){
int i, j;
for (i = d+1; i <= L.length; ++i){
if (L.r[i].key < L.r[i - d].key){
L.r[0] = L.r[i];
for (j=i-d; j>0&&L.r[0].key<L.r[j].key ; j = j - d){
L.r[j + d] = L.r[j];
}
L.r[j + d] = L.r[0];
}
}
cout<<"希尔排序后,顺序表为:"<<endl;
getList1(L);
}
//冒泡排序
void BubbleSort(SqList L){
int j,m,flag;
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;
L.r[0] = L.r[j];
L.r[j] = L.r[j + 1];
L.r[j + 1] = L.r[0];
}
}
--m;
}
cout<<"冒泡排序后,顺序表为:"<<endl;
getList1(L);
}
//快速排序
int Partition(SqList L, int low, int high){
L.r[0] = L.r[low];
int pKey=L.r[low].key;
while (low < high){
while (low < high&&L.r[high].key>=pKey) --high;
L.r[low] = L.r[high];
while (low < high&&L.r[low].key<= pKey) ++low;
L.r[high]=L.r[low];
}
L.r[low] = L.r[0];
return low;
}
void QSort(SqList L, int low, int high){
int i;
if (low < high){
i = Partition(L, low, high);
QSort(L, low, i - 1);
QSort(L, i + 1, high);
}
}
void QuickSort(SqList L){
QSort(L,1,L.length);
cout<<"快速排序后,顺序表为:"<<endl;
getList1(L);
}
int Menu(){
cout<<"--1.建立顺序表"<<endl;
cout<<"--2.建立顺序表后——折半查找操作"<<endl;
cout<<"--3.建立二叉排序树"<<endl;
cout<<"--4.建立二叉排序树后——查找操作"<<endl;
cout<<"--5.排序操作准备——建立顺序表"<<endl;
cout<<"--6.排序操作——直接排序"<<endl;
cout<<"--7.排序操作——折半排序"<<endl;
cout<<"--8.排序操作——希尔排序"<<endl;
cout<<"--9.排序操作——冒泡排序"<<endl;
cout<<"-10.排序操作——快速排序"<<endl;
cout<<"--0.退出程序"<<endl;
}
int main(){
Menu();
while(true){
SSTable ST;
BSTree T;
SqList L;
int a[MAXSIZE];
int Choice;
cout<<"--请输入您要执行的操作编号:"<<endl;
cin>>Choice;
switch(Choice){
case 1:
InitList(ST);
Fuzhi(ST);
getList(ST);
break;
case 2:
cout<<"请输入要查找的元素:"<<endl;
KeyType key1;
cin>>key1;
cout<<"该元素在顺序表中的位置为:"<<Search_Bin(ST,key1);
break;
case 3:
cout<<"创建二叉树"<<endl;
CreateBST(T,a);
break;
case 4:
cout<<"请输入查找的元素:"<<endl;
KeyType key2;
cin>>key2;
SearchA(a,key2);
SearchBST(T,key2);
break;
case 5:
Fuzhi1(L);
getList1(L);
break;
case 6:
InsertSort(L);
break;
case 7:
BInsertSort(L);
break;
case 8:
int q;
cout<<"请输入一个增量:"<<endl;
cin>>q;
ShellSort(L,q);
break;
case 9:
BubbleSort(L);
break;
case 10:
QuickSort(L);
break;
case 0:
exit(0);
break;
}
system("pause");
}
return 0;
}