黑龙江大学《数据结构与算法》实验九 排序和查找

 这学期的最后一次实验了捏。

快排的话用起来感觉有点像是二分法,首先就是找一个基准数,也就是第一个数,然后从前往后找一个大于基准数的数,再从后往前找小于基准数的数
然后交换两个数。直到没得再换,就把基准数和中间那个比它小的数进行交换,把这串数字分成两部分,左边和右边,然后左右重复进行上面的操作,直到
排序完成

因为老师的要求,归并改成希尔排序了,这个排序比较简单,我就不多说了。

二叉排序树也简单,和很早之前写的实验五有异曲同工之妙。

我的实验偏向于意识流,代码长只可意会不可言传,本人又菜写的bug又多,发出来仅供参考,能自己写最好。

因为是最后一次实验了,也不知道能不能检查到我的,所以写的比较随意,但还是写了。

代码如下

#include <iostream>
#include<bits/stdc++.h>
#include<ctime>
#include<conio.h>
#define N 100010
using namespace std;

int a[N];
int b[N];


typedef struct Node{
    int date;
    struct Node*lc;
    struct Node*rc;
}Tree;

Tree*root;

void mnue();
void charu(Tree**node,int x);
void build();
void shuchu(Tree*node);
void zzz();
void ca();
void zhao(Tree*node,int x,int c);
void chazhao();
void chu(Tree**node,int x);
void san(Tree**node,int x);
void sanchu();
void treem();

void qsort(int a[],int l,int r){
    if(l>r)return;
    int tp=a[l];
    int i=l;
    int j=r;
    while(i!=j){
        while(a[j]>=tp&&i<j){
            j--;
        }
        while(a[i]<=tp&&i<j){
            i++;
        }
        if(i<j){
            int t=a[i];
            a[i]=a[j];
            a[j]=t;
        }
    }
    a[l]=a[i];
    a[i]=tp;
    qsort(a,l,i-1);
    qsort(a,i+1,r);
    return;
}

void kp(int a[],int n){
    qsort(a,1,n);
    cout<<"打印快排之后的数:";
    for(int i=1;i<=n;i++){
        cout<<a[i]<<" ";
    }
    cout<<endl;
}

void seele(int a[],int n){
    int s=n;
    int k=n;
    while(k>1){
        k=(k+1)/2;
        for(int i=1;i<=s-k;i++){
            int r=i;
            int tp=a[r+k];
            while(r>=1){
                if(a[r]>tp){
                    a[r+k]=a[r];
                    r-=k;
                }
                else break;
            }
            a[r+k]=tp;
        }
    }
    cout<<"输出希尔排序后的数:";
    for(int i=1;i<=n;i++){
        cout<<a[i]<<" ";
    }
    cout<<endl;
    return;
}

void mnue(){
    cout<<"......................."<<endl;
    cout<<"         1.建立        "<<endl;
    cout<<"         2.输出        "<<endl;
    cout<<"         3.插入        "<<endl;
    cout<<"         4.查找        "<<endl;
    cout<<"         5.删除        "<<endl;
    cout<<"         6.退出        "<<endl;
    cout<<"......................."<<endl;
}

void charu(Tree**node,int x){
    if(x<(*node)->date){
            if((*node)->lc==NULL){
                (*node)->lc=new Tree;
                (*node)->lc->date=x;
                (*node)->lc->lc=NULL;
                (*node)->lc->rc=NULL;
            }
            else
            charu(&((*node)->lc),x);
    }
    else if(x>=(*node)->date){
            if((*node)->rc==NULL){
                (*node)->rc=new Tree;
                (*node)->rc->date=x;
                (*node)->rc->lc=NULL;
                (*node)->rc->rc=NULL;
            }
            else
            charu(&((*node)->rc),x);
    }
}

void build(){
    cout<<"输入要生成的数的个数:";
    int n;
    cin>>n;
    cout<<"输入要生成的数的范围:";
    int l,r;
    cin>>l>>r;
    for(int i=1;i<=n;i++){
        b[i]=(l+rand()%r);
    }
    root=new Tree;
    root->lc=NULL;
    root->rc=NULL;
    root->date=b[1];
    for(int i=2;i<=n;i++){
        charu(&root,b[i]);
    }
    getch();
    treem();
}

void shuchu(Tree*node){
    if(node!=NULL){
    shuchu(node->lc);
    cout<<node->date<<" ";
    shuchu(node->rc);
    }
}

void zzz(){
   shuchu(root);
   cout<<endl;
   getch();
   treem();
}

void ca(){
    cout<<"输入要插入的数:";
    int x;
    cin>>x;
    charu(&root,x);
    getch();
    treem();
}

void zhao(Tree*node,int x,int c){
    c++;
    if(node->date==x){
        cout<<c<<endl;
        return;
    }
    else{
        if(x<node->date){
            if(node->lc==NULL){
                cout<<"没有找到"<<endl;
                return;
            }
            else{
                zhao(node->lc,x,c);
            }
        }
        else if(x>=node->date){
            if(node->rc==NULL){
                cout<<"没有找到"<<endl;
                return;
            }
            else{
                zhao(node->rc,x,c);
            }
        }
    }
}

void chazhao(){
    cout<<"输入要查找的数:"<<endl;
    int x;
    cin>>x;
    int c=0;
    cout<<"输出比较次数:"<<endl;
    zhao(root,x,c);
    getch();
    treem();
}

void chu(Tree**node,int x){
    if((*node)->lc==NULL&&(*node)->rc==NULL){
        *node=NULL;
        return;
    }
    else if((*node)->lc==NULL&&(*node)->rc!=NULL){
        *node=(*node)->rc;
    }
    else if((*node)->lc!=NULL&&(*node)->rc==NULL){
        *node=(*node)->lc;
    }
    else{
        Tree**d,**b;
         *b=*node;
         *d=(*node)->rc;
         while((*d)->lc!=NULL){
            *b=*d;
            *d=(*d)->lc;
         }
         if(*b!=*node)
         (*b)->lc=NULL;
         else
         (*b)->rc=NULL;
         (*d)->lc=(*node)->lc;
         (*d)->rc=(*node)->rc;
         *node=*d;
    }
}

void san(Tree**node,int x){
    if(x==(*node)->date){
        chu(&(*node),x);
    }
    else{
        if(x<(*node)->date){
        if((*node)->lc!=NULL){
            san(&((*node)->lc),x);
        }
        }
        if(x>=(*node)->date){
        if((*node)->rc!=NULL){
            san(&((*node)->rc),x);
        }
        }
    }

}

void sanchu(){
    cout<<"输入要删除的数:";
    int x;
    cin>>x;
    san(&root,x);
    getch();
    treem();
}


void treem(){
     while(1){
        system("cls");
        mnue();
        int xuan;
        cin>>xuan;
        switch(xuan)
        {
            case 1:{
                build();
                break;
            }
            case 2:{
                zzz();
                break;
            }
            case 3:{
                ca();
                break;
            }
            case 4:{
                chazhao();
                break;
            }
            case 5:{
                sanchu();
                break;
            }
            case 6:{
                return;
                break;
            }
        }
    }
}

int main()
{
    srand(time(NULL));
    int n;
    cout<<"输入要随机生成的数的数量:";
    cin>>n;
    for(int i=1;i<=n;i++){
        a[i]=rand()%n+1;
    }
    cout<<"打印所有生成的数:";
    for(int i=1;i<=n;i++){
        cout<<a[i]<<" ";
    }
    cout<<endl;
    kp(a,n);
    seele(a,n);
    getch();
    treem();
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值