DS内排—基数排序

DS内排—基数排序

提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加
例如:第一章 Python 机器学习入门之pandas的使用


提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档


题目描述

给定一组数据,对其进行基数升序排序。


输入

测试次数t

每组测试数据一行:数字个数n,后跟n个数字(整数)

注:如果序列中有负数,则每个数字加上最小负数的绝对值,使序列中的数均大于等于0。排序后再减去最小负数的绝对值。


输出

对每组测试数据,输出每趟分配、收集的结果。若分配中该位没有数字,输出NULL。具体输出格式见样例。每组测试数据间以空行分隔。

如果序列中有负数,最后输出减去最小负数绝对值的序列值。


输入样例

2
10 278 109 63 930 589 184 505 269 8 83
6 57 0 93 19 18 99

输出样例

0:->930->^
1:NULL
2:NULL
3:->63->83->^
4:->184->^
5:->505->^
6:NULL
7:NULL
8:->278->8->^
9:->109->589->269->^
930 63 83 184 505 278 8 109 589 269
0:->505->8->109->^
1:NULL
2:NULL
3:->930->^
4:NULL
5:NULL
6:->63->269->^
7:->278->^
8:->83->184->589->^
9:NULL
505 8 109 930 63 269 278 83 184 589
0:->8->63->83->^
1:->109->184->^
2:->269->278->^
3:NULL
4:NULL
5:->505->589->^
6:NULL
7:NULL
8:NULL
9:->930->^
8 63 83 109 184 269 278 505 589 930

0:->0->^
1:NULL
2:NULL
3:->93->^
4:NULL
5:NULL
6:NULL
7:->57->^
8:->18->^
9:->19->99->^
0 93 57 18 19 99
0:->0->^
1:->18->19->^
2:NULL
3:NULL
4:NULL
5:->57->^
6:NULL
7:NULL
8:NULL
9:->93->99->^
0 18 19 57 93 99

代码

#include<iostream>
#include<queue>
using namespace std;

void Sort(int *a,int n,int time,int min) {
    queue<int> q[10];
    queue<int> p;
    for(int i=0;i<n;i++)
    {
        int temp;
        temp = a[i]%time/(time/10);
        q[temp].push(a[i]);
    }
    for(int i=0;i<10;i++)
    {
        if(!q[i].empty())
        {
            cout<<i<<":->";
            while(!q[i].empty())
            {
                int temp = q[i].front();
                q[i].pop();
                p.push(temp);
                cout<<temp<<"->";
            }
            cout<<"^"<<endl;
        }
        else
            cout<<i<<":NULL"<<endl;
    }
    for(int i=0;i<n;i++)
    {
        int temp = p.front();
        a[i] = temp;
        p.pop();
        if(min>=0)
        {
            if(i!=n-1)
                cout<<a[i]<<" ";
            else
                cout<<a[i]<<endl;
        }
        else
        {
            if(i!=n-1)
                cout<<a[i]+min<<" ";
            else
                cout<<a[i]+min<<endl;
        }
    }
}

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int a[n];
        for(int i=0;i<n;i++)
            cin>>a[i];
        int min = 0;
        int max = 0;
        for(int i=0;i<n;i++)
        {
            if(a[i]<min)
                min = a[i];
            if(a[i]>max)
                max = a[i];
        }
        if(min<0)
        {
            for(int i=0;i<n;i++)
            {
                a[i] += (-1)*min;
                if(a[i]>max)
                    max = a[i];
            }
        }
        int g;
        for(g=100000000000;g>=10;g/=10)
            if(max/g)
                break;
        int sum = 0;
        for(int k=10;k<=g*10;k*=10)
        {
            sum++;
            Sort(a,n,k,min);
        }
        //cout<<sum<<endl;

        cout<<endl;
    }
    return 0;
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值