数据结构最后一次机测

博客介绍了多种信息技术相关内容,包括快排、堆排序等排序算法,以及二叉排序树、二叉平衡树等数据结构。还提及顺序查找过程,从表最后元素开始逐个比较关键字。此外,给出了哈希表的公式及线性探测再散列方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

快排

void qqsort(int a[],int l ,int r)
{
    if(l<r)
    {
        int x=a[l];
        int i=l,j=r;
        while(i<j)
        {
            while(a[j]>=x&&i<j) j--;
            if(i<j) a[i]=a[j];
            while(a[i]<=x&&i<j) i++;
            if(i<j) a[j]=a[i];
    }
    a[i]=x;
    qqsort(a,l,i-1);
    qqsort(a,i+1,r);
}

求逆序数


long long int sum;     //!至少长整型;


void merge(int l1,int r1,int l2,int r2)
{
    int i;
    int k=l1;
    int p1=l1;
    int p2=l2;
    while(p1<=r1&&p2<=r2)
    {
        if(a[p1]<=a[p2])
            b[k++]=a[p1++];
        else
        {
            b[k++]=a[p2++];
            sum+=r1-p1+1;
        }
    }
    while(p1<=r1)
        b[k++]=a[p1++];
    while(p2<=r2)
        b[k++]=a[p2++];
    for(i=l1;i<=r2;i++)
    {
        a[i]=b[i];
    }
}

void sort(int l,int r)
{
    if(l<r)
    {
        int mid=(l+r)/2;
        sort(l,mid);
        sort(mid+1,r);
        merge(l,mid,mid+1,r);
    }
}



int a[101];
int main()
{
    int n,i,x;
    scanf("%d",&n);
    memset(a,0,sizeof(a));
    for(i=0; i<n; i++)
    {
        scanf("%d",&x);
        if(x>=100)
            a[100]++;
        else
            a[x]++;
    }
    for(i=0; i<=100; i++)
    {
        if(a[i]!=0)
        {
            printf("%d %d\n",i,a[i]);
        }
    }
    return 0;
}

堆排序

#include <stdio.h>
#include <stdlib.h>
int a[11],m;
void heapjust(int a[],int i,int len);
void heapsort(int a[])
{
    int i;
    for(i=m/2-1;i>=0;i--)
    {
        heapjust(a,i,m-1);
    }
    for(i=m-1;i>0;i--)
    {
        int t=a[0];
        a[0]=a[i];
        a[i]=t;
        heapjust(a,0,i-1);
    }
}

void heapjust(int a[],int i,int len)
{
    int x=a[i],k;
    for( k=2*i; k<=len; k=k*2)
    {
        if(k+1<=len&&a[k]<a[k+1]) k++;
        if(x<a[k])
        {
            a[i]=a[k];
            i=k;
        }
        else break;
    }
       a[i]=x;
}

int main()
{
    int n,i,j,x;
    int k=0;
    scanf("%d %d",&n,&m);
    for(i=0; i<n; i++)
    {
        scanf("%d",&x);
        if(k<m)
        {
            a[k++]=x;
        }
        else
        {
            int min=0;
            for(j=1;j<m;j++)
            {
                if(a[j]<a[min]) min=j;
            }
            if(x>a[min]) a[min]=x;
        }
    }
        heapsort(a);
        for(i=m-1; i>0; i--)
        {
            printf("%d ",a[i]);
        }
            printf("%d\n",a[i]);
    return 0;
}

二叉排序树


struct node
{
    int data;
    struct node*lchild;
    struct node*rchild;
};
struct node *creat(struct node *t, int x)
{
    if(t==NULL)
    {
        t=(struct node*)malloc(sizeof(struct node));
        t->data=x;
        t->rchild=NULL;
        t->lchild=NULL;
    }
    else
    {
        if(x>t->data) t->rchild=creat(t->rchild,x);
        else t->lchild=creat(t->lchild,x);
    }
    return t;
}
int equal(struct node *t1,struct node *t2)
{
    if(t1==NULL&&t2==NULL) return 1;
    if(t1==NULL||t2==NULL) return 0;
    if(t1->data==t2->data)
        return equal(t1->lchild,t2->lchild)&&equal(t1->rchild,t2->rchild);
    else return 0;
}

int main()
{
    int n,m,i,x;
    struct node *t1, *t2;
    while(scanf("%d",&n)&&n!=0)
    {
        scanf("%d",&m);
        t1=NULL;
        for(i=0;i<n;i++)
        {
            scanf("%d",&x);
            t1=creat(t1,x);
        }
        while(m--)
        {
           t2=NULL;
           for(i=0;i<n;i++)
          {
            scanf("%d",&x);
            t2=creat(t2,x);
          }
          if(equal(t1,t2)==1) printf("Yes\n");
            else printf("No\n");
        }
    }
        return 0;
}

顺序查找的查找过程为:

从表中的最后一个数据元素开始,逐个同记录的关键字做比较,如果匹配成功,则查找成功;

二叉平衡树

要么它是一棵空树,要么它的左子树和右子树都是平衡二叉树,且左子树和右子树的深度之差的绝对值不超过1。

https://blog.youkuaiyun.com/isunbin/article/details/81707606

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct node
{
    int data;
    int h;
    struct node *lc,*rc;  //平衡二叉树 需要一个 h 来记录平衡因子
};

int max(int x ,int y)  
{
    if(x > y) return x;
    else return y;
}

int fin(struct node *root)   // 返回这个结点的平衡因子,也就是左右子树的差值
{
    if(root == NULL) return -1;
    else return root -> h;
}

struct node *LL(struct node *root)  // 如果是左左型,也就是呈现 根 - 左子树 p - 左子树2 , 我们要把 根 变成 p 的右子树 
{
    struct node *p = root -> lc;
    root -> lc = p -> rc;
    p -> rc = root;
    p -> h = max(fin(p->lc),fin(p->rc)) + 1;  // 更新这两个点的平衡因子
    root -> h = max(fin(root->lc),fin(root->rc)) + 1;
    return p;  //别忘记返回 p
}

struct node *RR(struct node *root)  // 同上相反
{
    struct node *p = root -> rc;
    root -> rc = p -> lc;
    p -> lc = root;
    p -> h = max(fin(p->lc),fin(p->rc)) + 1;
    root -> h = max(fin(root->lc),fin(root->rc)) + 1;
    return p;
}

struct node *LR(struct node *root)  //LR型, 形如 根 - 左子树 - 右子树 
{
    root -> lc = RR(root -> lc);
    return LL(root);
}

struct node  *RL(struct node *root)
{
    root -> rc = LL(root -> rc);
    return RR(root);
}

struct node *creat(struct node *root, int x)  // 建树的过程
{
    if(root == NULL)  //如何到底层或者到可以放这个数的地方
    {
        root = (struct node *)malloc(sizeof(struct node));
        root -> data = x;
        root -> lc = root -> rc = NULL; // 左右孩子、h 初始化
        root -> h = 0;
    }
    else if(root -> data > x)  // 如果小的话,找左子树,
    {
        root -> lc = creat(root -> lc, x);
        if(fin(root->lc) - fin(root->rc) > 1) // 如果找完之后,放进去之后,判断时候不平衡了,如果不平衡,判断是什么样子的类型,再旋转
        {
            if(root -> lc -> data > x) root = LL(root);  // LL型,因为如果 root -> lc -> data > x,那么 x 是放在了这个的左子树
            else root = LR(root);  //相反,这样子会放在右子树
        }
    }
    else if(root -> data < x)
    {
        root -> rc = creat(root -> rc, x);
        if(fin(root->rc) - fin(root->lc) >1)
        {
            if(root -> rc -> data < x) root = RR(root);
            else root = RL(root);
        }
    }
    root -> h = max(fin(root->lc),fin(root->rc)) + 1;  // 没插入一次新值,更新 root 的平衡因子
    return root;
}
int main()
{
    int n,m;
    scanf("%d",&n);
    struct node *root = NULL;
    for(int i = 0; i < n; i ++)
    {
        scanf("%d",&m);
        root = creat(root,m);
    }
    printf("%d\n",root->data);
    return 0;
}

哈希表

Hi=(H(key)+di)MODm      m为表长 
线性探测再散列 
di=c∗i ;
 


int main()
{
    int a[3000];
    int n,p,i,x;
    while(scanf("%d %d",&n,&p)!=EOF)
    {
        memset(a,0,sizeof(a));
        for(i=0; i<n; i++)
        {
            scanf("%d",&x);
            int h=x%p;
            if(a[h]==0||a[h]==x)
            {
                a[h]=x;
                if(i==n-1)
                    printf("%d",h);
                else
                    printf("%d ",h);
            }
            else
            {
                int d=0;
                int hi=(h+d)%p;
                while(1)
                {
                    if(a[hi]==0||a[hi]==x)
                        break;
                    d++;
                    hi=(h+d)%p;
                }
                a[hi]=x;
                if(i==n-1)
                    printf("%d",hi);
                else
                    printf("%d ",hi);
            }
        }
        printf("\n");
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值