基数排序

基数排序相当于把一些数放进桶里,有多少位就有多少个桶

蒟蒻自己写的垃圾代码,希望大佬多多指教

 #include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;

typedef long double ld;
 
const ll maxn = 1e5 + 5;

const ll mod = 1e9 + 7;

vector<ll> a[100],plu,sub;

ll getpos(ll x,ll pos)
{
	if(x == 0)return 0;
	else
	{
		while(x)
		{
			pos--;
			if(pos == 0)return x % 10;
			x /= 10;
		}
		if(pos != 0)return 0;
	}
}

void solve1()
{
	for(int i = 1; i <= 9; i ++)
	{
		for(int j = 0; j < plu.size(); j ++)
		{
			a[getpos(plu[j],i)].push_back(plu[j]);
		}
		
		plu.clear();
		for(int k = 0; k < 10; k ++)
		{
			ll w = 0;
			while(w < a[k].size())
			{
				plu.push_back(a[k][w]);
				w++;
			}
			a[k].clear();
		}
	}
}

void solve2()
{
	for(int i = 1; i <= 9; i ++)
	{
		for(int j = 0; j < sub.size(); j ++)
		{
			a[getpos(sub[j],i)].push_back(sub[j]);
		}
		
		sub.clear();
		for(int k = 0; k < 10; k ++)
		{
			ll w = 0;
			while(w < a[k].size())
			{
				sub.push_back(a[k][w]);
				w++;
			}
			a[k].clear();
		}
	}
}
int main() 
{
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);

	ll n,x;
	cin >> n;
	for(int i = 1; i <= n; i ++)
	{
		cin >> x;
		if(x < 0)
		{
			sub.push_back(abs(x));
		}
		else
		{
			plu.push_back(x);
		}
	}
	
	solve2();
	if(!sub.empty())
	{
		
		for(int i = sub.size() - 1; i >= 0; i --)
		{
			cout << -sub[i] << endl;
		}
	}
	
	solve1();
	if(!plu.empty())
	{
		for(int i = 0; i < plu.size(); i ++)
		{
			cout << plu[i] << endl;
		}
	}	
	
	return 0;
}
	

参考大佬博客修改基数排序如下

#include <bits/stdc++.h>

typedef long long ll;

using namespace std;

const ll maxn = 1e5 + 7;

vector<ll> sub,plu;
ll temp[maxn];
ll buc[maxn];

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);

    ll n,x;
    cin >> n;
    for(int i = 1; i <= n; i ++)
    {
        cin >> x;
        if(x >= 0)plu.push_back(x);
        else sub.push_back(-x);
    }

    ll wei = 1;
    for(int i = 1; i <= 9; i ++)
    {
        for(int j = 0; j <= 9; j ++)buc[j] = 0;
        for(int j = 0; j < sub.size(); j ++)
        {
            ll val = sub[j] / wei % 10;
            buc[val]++;
        }

        for(int j = 1; j <= 9; j ++)buc[j] += buc[j - 1];

        for(int j = sub.size() - 1; j >= 0; j --)
        {
            ll val= sub[j] / wei % 10;
            temp[--buc[val]] = sub[j];
        }
        for(int j = 0; j < sub.size(); j ++)sub[j] = temp[j];
        wei *= 10;
    }

    if(!sub.empty())
    for(int i = sub.size() - 1; i >= 0; i --)
    {
        cout << -sub[i] << endl;
    }

    wei = 1;
    for(int i = 1; i <= 9; i ++)
    {
        for(int j = 0; j <= 9; j ++)buc[j] = 0;
        for(int j = 0; j < plu.size(); j ++)
        {
            ll val = plu[j] / wei % 10;
            buc[val]++;
        }

        for(int j = 1; j <= 9; j ++)buc[j] += buc[j - 1];

        for(int j = plu.size() - 1; j >= 0; j --)
        {
            ll val= plu[j] / wei % 10;
            temp[--buc[val]] = plu[j];
        }
        for(int j = 0; j < plu.size(); j ++)plu[j] = temp[j];
        wei *= 10;
    }

    if(!plu.empty())
    for(int i = 0; i < plu.size(); i ++)
    {
        cout << plu[i] << endl;
    }

	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值