• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
gw2010
重新编程
博客园    首页    新随笔    联系   管理    订阅  订阅
桶排序
以前的老代码翻出来看,还是喜欢以前写的,精练些。
 
        static void Main(string[] args)
        {
            int[] a = { 32, 324, 645, 856, 3, 56, 8, 6, 4, 368, 87, 55, 78, 434, 12, 345, 787, 90, 123, 304, 90 };
            int bit = 3;

            Foo(a, bit);

            for (int i = 0; i < a.Length; i++)
            {
                Console.Write("{0}  ", a[i]);
            }

            Console.Read();
        }

        private static void Foo(int[] a, int bit)
        {
            List<int>[] t = new List<int>[10];
            for (int i = 0; i < 10; i++)
            {
                t[i] = new List<int>();
            }

            while (bit > 0)
            {
                for (int i = 0; i < a.Length; i++)
                {
                    //这里比较重要, 数%10^x/10^(x-1)
                    t[a[i] % ((int)Math.Pow(10, 4 - bit)) / ((int)Math.Pow(10, (3 - bit)))].Add(a[i]);
                }
                Proc(t, a);
                bit--;
            }
        }

        static void Proc(List<int>[] t, int[] a)
        {
            int x = 0;

            for (int i = 0; i < t.Length; i++)
            {
                foreach (int l in t[i])
                {
                    a[x++] = l;
                }
            }

            for (int i = 0; i < 10; i++)
            {
                t[i] = new List<int>();
            }
        }
View Code

 

声明本博客文章未特殊注明均为原创,转载请注明作者和原地址。 博客地址:http://www.cnblogs.com/gw2010/ 博客首发:http://www.zhou2019.cn
posted on 2015-10-15 16:25  gw2010  阅读(179)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3