线性表排序


1.顺序表

    #include<stdio.h>  
    #include<stdlib.h>  
      
    #define MAX 20  
      
    typedef struct  
    {  
        int elem[MAX];  
        int length;  
    }Seqlist;  
      
    Seqlist Initlist()//顺序表的初始化  
    {  
        Seqlist L;//定义一个顺序表  
        L.length = 0;  
        return L;  
    }  
    Seqlist Create(Seqlist L)//顺序表的建立  
    {  
        int x;  
        //printf("输入元素,输入-1时结束输入");  
        scanf_s("%d", &x);  
        while (x != -1)  
        {  
            L.elem[L.length] = x;  
            L.length++;  
            scanf_s("%d", &x);  
        }  
        printf("创建成功\n");  
        return L;  
    }  
    Seqlist Paixu(Seqlist L)//使用冒泡法进行排序  
    {  
        int i, j, t;  
        for (i = 0; i<L.length - 1; i++)  
        for (j = 0; j<L.length - 1 - i; j++)  
        {  
            if (L.elem[j]>L.elem[j + 1])  
            {  
                t = L.elem[j]; L.elem[j] = L.elem[j + 1]; L.elem[j + 1] = t;  
            }  
        }  
        return L;  
    }  
      
    void outline(Seqlist L)  
    {  
        int i = 0;  
        for (i = 0; i<L.length; i++)  
        {  
            printf("%2d", L.elem[i]);  
        }  
      
    }  
    Seqlist Merge(Seqlist La, Seqlist Lb)  
    {  
        Seqlist Lc;  
        int i = 0, k = 0, j = 0;  
        while (i<La.length&&j<Lb.length)  
        {  
            if (La.elem[i]<Lb.elem[j])  
                Lc.elem[k++] = La.elem[i++];  
            else Lc.elem[k++] = Lb.elem[j++];  
        }  
        while (i<La.length) Lc.elem[k++] = La.elem[i++];  
        while (j<Lb.length) Lc.elem[k++] = Lb.elem[j++];  
        Lc.length = k;  
        return Lc;  
    }  
    int main()  
    {  
        Seqlist La, Lb,Lc;  
        La = Initlist();  
        Lb = Initlist();//对La,Lb初始化  
        printf("创建La,当输入-1时终止\n");  
        La = Create(La);  
        printf("经排序后La的元素为:");  
        La=Paixu(La);  
        outline(La);  
        printf("\n创建Lb,当输入-1时终止\n");  
        Lb = Create(Lb);  
        printf("经排序后Lb的元素为:");  
        Lb=Paixu(Lb);  
        outline(Lb);  
        Lc = Merge(La, Lb);  
        printf("\nLc的元素为:");  
        outline(Lc);  
        return 0;  
    }  


2.链表

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

struct Lnode
{
    int data;
    struct Lnode *next;
};
struct Lnode *CreatEND()//尾插法建立链表
{
    struct Lnode *head,*p,*p2;
    int x;
    head=(struct Lnode *)malloc(sizeof(struct Lnode));
    head->next=NULL;
    p=head;
    printf("输入元素,-1停止:");
    scanf("%d",&x);
    while(x!=-1)
    {
        p2=(struct Lnode *)malloc(sizeof(struct Lnode));
        p2->data=x;
        p->next=p2;
        p=p->next;
        scanf("%d",&x);
    }
    p->next=NULL;
    return head;
};
struct Lnode *Paixu(struct Lnode *head)//进行选择排序
{
    struct Lnode *p,*q,*small;
    int temp;
    for(p = head->next; p->next != NULL; p = p->next)
    {
        small = p;
        for(q = p->next; q; q = q->next)
        {
            if(q->data < small->data)
            {
                small = q;
            }
        }
        if(small != p)
        {
            temp = p->data;
            p->data = small->data;
            small->data = temp;
        }
    }
      return head;
}

void Outline(struct Lnode *head)//输出链表元素
{
    struct Lnode *p;
    p=head->next;
    while(p!=NULL)
    {
        printf(" %d ",p->data);
        p=p->next;
    }
}
struct Lnode *MergeList(struct Lnode *La,struct Lnode *Lb,struct Lnode *Lc)
{
    struct Lnode *a,*b,*c;
    a=La->next;b=Lb->next;
    Lc=La;
    c=Lc;
    while(a&&b)
    {
        if(a->data<=b->data)
        {
         c->next=a;
         c=c->next;
         a=a->next;
        }
        else
        {
         c->next=b;
        c=c->next;
        b=b->next;
        }
    }
    c->next=a?a:b;
   return Lc;
}
int main()
{
     struct Lnode *La,*Lb,*Lc;
     La=Lb=Lc=NULL;
     printf("创建La\n");
     La=CreatEND();
     Outline(La);
     La=Paixu(La);
     printf("\n经排序后元素为:");
     Outline(La);
     printf("\n创建Lb\n");
     Lb=CreatEND();
     Lb=Paixu(Lb);
     printf("经排序后元素为:");
     Outline(Lb);
     Lc=MergeList(La,Lb,Lc);
     printf("\n归并排序后LC元素:");
     Outline(Lc);
    return 0;
}

妈的,给单链表排个序这么难吗,网上都是些什么啊,罗里吧嗦的还没成果,不对就不要乱发,真是的,我实在是心情不好,浪费这么多时间。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值