Experiment 6 The realization of search and sorting

Experimental nature: comprehensive experiment

Claim:

(1) Use adjacency matrix/adjacency table to build graph;
(2) Use depth-first/breadth-first search method to traverse the graph;
(3) Programmatically implement Dijkstra's shortest path algorithm.

purpose:

(1) Master the adjacency matrix and adjacency table storage methods
of the graph ; (2) Master the traversal algorithm of the graph;
(3) Master the practical application of the graph-the shortest path algorithm.

Code

#include <iostream>
using namespace std;
//**折半查找**//
typedef struct {
    
    
    int key;//查找表中每个数据元素的值
}ElemType;
typedef struct{
    
    
    ElemType *elem;//存放查找表中数据元素的数组
    int length;//记录查找表中数据的总数量
}SSTable;
//**二叉排序树**//
int m=-1;   //二叉排序树初始化计数
int n=0;    //二叉排序树关键字序列下标和查找次数计数
typedef struct {
    
    
    string key;//查找表中每个数据元素的值
    int position;//结点在关键字序列中的位置
}ElemType2;
typedef struct BSTNode
{
    
    
    ElemType2 data;
    struct BSTNode *lchild,*rchild; //左右孩子指针
}BSTNode,*BSTree;
//**排序**//
#define MAXSIZE 100
typedef  struct {
    
    
    int  key;   //关键字项
}RedType;                     //记录类型
typedef  struct {
    
    
    RedType  r[MAXSIZE+1]; //r[0]闲置
    int  length;  //顺序表长度
}SqList;  //顺序表类型


void CreateList(SSTable &ST);   //创建有序线性表
void Search_Bin(SSTable ST,int key);   //折半查找
void CreateTree(BSTree &T); //创建二叉链表
void InOrderTraverse(BSTree T); //中序遍历
BSTree SearchBST(BSTree T,  string key);//二叉树的递归查找
void InitList(SqList &L);//初始化及赋初始值
void OutputKey(SqList &L);  //输出当前顺序表数值
void InsertSort(SqList &L);//直接插入排序
void BInsertSort(SqList &L);  //折半插入排序
void Menu();  //文字菜单提示信息

int main()
{
    
    
    SSTable ST;
    BSTree T;
    SqList L;
    Menu();
    int i;  //输入的数字
    int key;string key2;    //输入的待查元素
    cout<<"请输入操作代码:";
    cin>>i;
    while(true)
    {
    
    
        switch(i)
        {
    
    
            case 1:
                CreateList(ST);
                break;
            case 2:
                cout<<"请输入待查元素:";
                cin>>key;
                Search_Bin(ST,key);
                break;
            case 3:
                CreateTree(T);
                cout<<"二叉排序树创建成功"<<endl;
                cout<<"其中序遍历结果为:";
                n=0;    //置二叉排序树关键字序列下标从0开始
                InOrderTraverse(T);
                cout<<endl;
                break;
            case 4:
                cout<<"请输入待查元素:";
                cin>>key2;
                n=0;    //置查找次数为0
                cout<<"待查元素为第"<<SearchBST(T,key2)->data.position+1<<"个元素,";
                cout<<"共查找了"<<n<<"次"<<endl;
                break;
            case 5:
                cout<<"顺序表初始化成功,";
                InitList(L);
                break;
            case 6:
                InsertSort(L);
                break;
            case 7:
                BInsertSort(L);
                break;
            default:
                if(i<0)
                    return 0;
                else
                {
    
    
                    cout<<"输入的位置非法,请重新输入";
                    break;
                }
        }
        cout<<"请输入操作代码:";
        cin>>i;
    }
    return 0;
}
//**折半查找**//
void CreateList(SSTable &ST)
{
    
    
    ST.elem=new ElemType[MAXSIZE];
    if(!ST.elem)    cout<<"存储分配失败"<<endl;
    ST.length=11;
    int a[11]={
    
    5,16,20,27,30,36,44,55,60,67,71};
    cout<<"有序线性表创建成功,其数值为:";
    for (int i=1; i<=ST.length; i++)    //从下标1开始存储
    {
    
    
        ST.elem[i].key=a[i-1];    //设置初始顺序表数值
        cout<<ST.elem[i].key<<" ";
    }
    cout<<endl;
}
void Search_Bin(SSTable ST,int key)
{
    
    
    int low=1;int high=ST.length;int mid;
    int times=0;bool isHave=false;
    while(low<=high)
    {
    
    
        mid=(low+high)/2;
        times++;    //查找次数
        if(key==ST.elem[mid].key)
        {
    
    
            cout<<"待查元素为第"<<mid<<"个元素,共查找了"<<times<<"次"<<endl;
            isHave=true;
            break;
        }
        else if(key<ST.elem[mid].key)   high=mid-1; //在前面找
        else low=mid+1; //在后面找
    }
    if(!isHave)
        cout<<"待查元素不在有序表中,共查找了"<<times<<"次"<<endl;
}
//**二叉排序树**//
void CreateTree(BSTree &T)
{
    
    
    string ch[13]={
    
    "45","24","12","#","#","37","#","#","53","#","93","#","#"};
    m+=1;
    if(ch[m]=="#")
        T=NULL;
    else
    {
    
    
        T=new BSTNode;
        T->data.key=ch[m];
        CreateTree(T->lchild);    //递归创建左子树
        CreateTree(T->rchild);    //递归创建右子树
    }
}
void InOrderTraverse(BSTree T) //中序遍历
{
    
    
    if(T)
    {
    
    
        InOrderTraverse(T->lchild); //中序遍历左子树
        cout<<T->data.key<<" ";  //访问根节点
        T->data.position=n;
        n++;
        InOrderTraverse(T->rchild);
    }
}
BSTree SearchBST(BSTree T,  string key)//二叉树的递归查找
{
    
    
    n++;
    if((!T) || key==T->data.key) return T;
    else if (key<T->data.key)
        return SearchBST(T->lchild,key); //在左子树中继续查找
    else
        return SearchBST(T->rchild,key); //在右子树中继续查找
}
//**排序**//
void InitList(SqList &L)//初始化且赋初始值
{
    
    
    int a[8]={
    
    49,38,65,97,76,13,27,49};
    L.length=0;
    for(int i=1;i<=8;i++)
    {
    
    
        L.r[i].key=a[i-1];
        L.length++;
    }
    OutputKey(L);
}
void OutputKey(SqList &L) //输出当前顺序表数值
{
    
    
    cout<<"当前顺序表值为:";
    for(int i=1;i<=8;i++)
    {
    
    
        cout<<L.r[i].key<<" ";
    }
    cout<<endl;
}
void InsertSort(SqList &L)//直接插入排序
{
    
    
    for(int i=2;i<=L.length;++i)
    {
    
    
        if( L.r[i].key<L.r[i-1].key)//将L.r[i]插入有序子表
        {
    
    
            L.r[0]=L.r[i]; // 复制为哨兵
            L.r[i]=L.r[i-1];
            int j;
            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<<"直接插入排序成功,";
    OutputKey(L);
}
void BInsertSort(SqList &L )  //折半插入排序
{
    
    
    for (int i = 2;i<=L.length; ++i )
    {
    
    
         L.r[0] = L.r[i];
         int low = 1 ; int 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 (int j=i-1; j>=high+1; --j)
            L.r[j+1] = L.r[j];
        L.r[high+1] = L.r[0];
     }
     cout<<"折半插入排序成功,";
     OutputKey(L);
}
void Menu()
{
    
    
    cout << "---------------------------" << endl;
    cout << "********by 夏日********" << endl;
    cout << "---------------------------" << endl;
    cout << "1-----创建有序线性表(折半查找前提)"<< endl;
    cout << "2-----折半查找" << endl;
    cout << "3-----创建二叉排序树 "<< endl;
    cout << "4-----二叉排序树查找" << endl;
    cout << "5-----顺序表初始化及赋初始值" << endl;
    cout << "6-----直接插入排序" << endl;
    cout << "7-----折半插入排序" << endl;
    cout << "   退出,输入一个负数!" << endl;
}

Sample output

1. Create an ordered linear table
Insert picture description here
2. Binary search
Insert picture description here
3. Create binary sort tree
Insert picture description here
4. Binary sort tree search
Insert picture description here
5. Sequence table initialization
Insert picture description here
6. Direct insertion sort
Insert picture description here
7. Binary insertion sort
Insert picture description here

The core principle of data structure and algorithm application


150 talks to easily get a Python web crawler

Guess you like

Origin blog.csdn.net/zss192/article/details/106713108