首页 > 编程语言 > 详细

桶排序

时间:2015-10-15 18:05:52      阅读:262      评论:0      收藏:0      [点我收藏+]
以前的老代码翻出来看,还是喜欢以前写的,精练些。
 
技术分享
        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/p/4882774.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!