C语言编程 排序

2025-06-23 04:38:16
推荐回答(4个)
回答1:

/*有一种排序方法叫Radix Sort,就是针对这种多关键字的排序的
时间复杂度线性的,但是有个缺点就是必须知道关键字的范围,不知道题主的关键字范围是多少?

好吧,假设我的关键字最多有100个,基数最大不超过999
程序如下:   */

//Radix Sort算法,采取LSD(低位优先)


#include

#include

#define KEYNUM 100                 //关键字的最大个数

#define RADIX 1000                 //基数的范围是0-RADIX-1

typedef struct Node

{

    int ele[KEYNUM+1];

    struct Node *next;

} Node;


Node *e[RADIX],*f[RADIX];          //链队列的首指针和尾指针表


void init(Node* head,int n,int m)   //初始化输入链表

{

    int i,j;

    Node *p,*q=head;

    for(i=0; i

    {

        p=(Node*)malloc(sizeof(Node));

        if(!p)

            return;

        for(j=1; j<=m; j++)

            scanf("%d",&p->ele[j]);

        p->next=NULL;

        q->next=p;

        q=q->next;

    }

}


void distribute(Node* head,int locate,int r)      //进行Radix排序

{

    Node *p,*q;

    int i;

    while(head->next!=NULL)

    {

        q=head->next;

        head->next=q->next;

        q->next=NULL;

        p=f[q->ele[locate]];

        while(p->next!=NULL)

            p=p->next;

        p->next=q;

        e[q->ele[locate]]->next=q;

    }

    p=head;

    for(i=0; i<=r; i++)

    {

        while(f[i]->next!=e[i]->next)

        {

            q=f[i]->next;

            f[i]->next=q->next;

            q->next=NULL;

            p->next=q;

            p=p->next;

        }

        q=f[i]->next;

        if(q)

        {

            f[i]->next=q->next;

            q->next=NULL;

            p->next=q;

            p=p->next;

        }

        f[i]->next=e[i]->next=NULL;

    }

}


void display(Node* head,int m)              //显示链表各个节点

{

    int i;

    Node* p=head->next;

    if(!head)

        return;

    while(p)

    {

        for(i=1; i<=m; i++)

            printf("%d ",p->ele[i]);

        printf("\n");

        p=p->next;

    }

}

void Delete(Node* head)                        //释放链表节点

{

    Node *p;

    if(!head)

        return;

    while(head->next)

    {

        p=head->next;

        head->next=p->next;

        free(p);

    }

}

int main()

{

    Node* head;

    int n,m,r,i;

    for(i=0; i

    {

        f[i]=(Node*)malloc(sizeof(Node));

        e[i]=(Node*)malloc(sizeof(Node));

        if(!f[i]||!e[i])

            exit(1);

        f[i]->next=e[i]->next=NULL;

    }

    head=(Node*)malloc(sizeof(Node));

    if(!head)

        return 1;

    printf("待排序个数以及关键字的个数:");

    scanf("%d%d",&n,&m);

    printf("输入数据:\n");

    init(head,n,m);

    printf("输入基数范围0-n:");

    scanf("%d",&r);

    for(i=m; i>=1; i--)

        distribute(head,i,r);                              //从低位到高位进行Radix排序

    display(head,m);

    for(i=0; i

    {

        free(f[i]);

        free(e[i]);

    }

    Delete(head);

    free(head);

    return 0;

}


题主的答案:

更复杂的情况:


回答2:

注释加上了,看看行不行
#include
#include
#include
#include
using namespace std;

int ran(int n) //随机函数,输出是1~n的随机整数
{
return rand()%n+1;
}
/*c/c++语言中,生成随机数要先生成种子,这点如果你不明白不要紧,日后一定会明白的
我大一的时候听说之后也不明白*/
int main()
{
int num[200],i,n=200;
ofstream Aout("a.dat"),Bout("b.dat"); //两个文件流,就是对两个文件进行写操作的对象
//其中ofstream代表output,file,stream
srand((unsigned)time(NULL)); //生成随机数的种子
cout<<"before sort:"< for(i=0;i { //cout是向控制台输出,Aout是向a.dat输出
num[i] = ran(1000);
cout< Aout< if((i+1)%10 == 0) //这个的意思是每十个数一行,endl相当于换行
{
cout< Aout< }
}
cout< sort(num,num+n); //对这些数排序,快速排序方法,在algorithm头文件中,O(nlogn)复杂度
cout<<"after sort:"< for(i=0;i { //这个for循环是和上面的形式一样的
cout< Bout< if((i+1)%10 == 0)
{
cout< Bout< }
}
cout< return 0;
}

回答3:

#include 
#include 
#define NUMM 3
#define NUMN 3

int data[NUMM][NUMN];
int xu[NUMM];//m是行数 按照1列值排序 这个序列的XU[M]值 对应的拍好的第几行
int cmp(int a,int b)//如果列A 小于列B 则返回-1 如果 等于 返回0 如果大于 返回1
{
    int n,va,vb;
    for(n=0;n    {
        va=data[a][n];
        vb=data[b][n];
        if(va        {
            return -1;
        }
        else if(va>vb)
        {
            return 1;
        }
        else if (va==vb && n==NUMN-1)
        {
            return 0;
        }
    }
    return 0;
}

void paixu()
{
    int n,lie;
    for(n=0;n    {
        xu[n]=n;
    }
    for(lie=0;lie    {
        for(n=lie+1;n        {
            if(cmp(n,xu[n])>0)
            {
                xu[n]=xu[lie];
                xu[lie]=n;
            }
        }
    }
}
void show()
{
    int n,x;
    for(n=0;n    {
        for(x=0;x        {
            printf("%d ",data[n][x]);
        }
        printf("\n");
    }
}
int main()
{
    int x,y,t;
    printf("手动输入矩阵\n");
    for(x=0;x    {
        for(y=0;y        {
            scanf("%d",&t);
            data[x][y]=t;
        }
    }
    paixu();
    show();
    return 0;
}

回答4:

什么东西。。。