zoukankan      html  css  js  c++  java
  • 桶排序

    以前的老代码翻出来看,还是喜欢以前写的,精练些。
     
            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
  • 相关阅读:
    WebApi-JSON序列化循环引用
    Android ImageSwitcher
    Android Gallery
    理解URI
    WebApi入门
    URL的组成
    Http协议
    python __new__和__init__的区别
    11.6
    win7 32位用pyinstaller打包Python和相关html文件 成exe
  • 原文地址:https://www.cnblogs.com/gw2010/p/4882774.html
Copyright © 2011-2022 走看看