經典排序演算法 - 鴿巢排序Pigeonhole sort

來源:互聯網
上載者:User

經典排序演算法 - 鴿巢排序Pigeonhole sort

原理類似桶排序,同樣需要一個很大的鴿巢[桶排序裡管這個叫桶,名字無所謂了]

鴿巢其實就是數組啦,數組的索引位置就表示值,該索引位置的值表示出現次數,如果全部為1次或0次那就是桶排序

例如

var pigeonHole = new int[100];

pigeonHole[0]的值表示0的出現次數...

pigeonHole[1]的值表示1的出現次數...

pigeonHole[2]的值表示2的出現次數...

參考http://hi.baidu.com/wangxvfeng101/blog/item/a2c22560e57260c58cb10d8c.html

代碼僅供參考,歡迎諶誤

        /// 鴿巢排序        /// </summary>        /// <param name="unsorted">待排數組</param>        /// <param name="maxNumber">待排數組中的最大數,如果可以指定的話</param>        /// <returns></returns>        static int[] pogeon_sort(int[] unsorted, int maxNumber = 10)        {            int[] pogeonHole = new int[maxNumber + 1];            foreach (var item in unsorted)            {                pogeonHole[item]++;            }            return pogeonHole;            /*             * pogeonHole[10] = 4; 的含意是             * 在待排數組中有4個10出現,同理其它             */        }        static void Main(string[] args)        {            int[] x = { 99, 65, 24, 47, 47, 50, 99, 88, 66, 33, 66, 67, 31, 18, 24 };            var sorted = pogeon_sort(x, 99);            for (int i = 0; i < sorted.Length; i++)            {                for (int j = 0; j < sorted[i]; j++)                {                    Console.WriteLine(i);                }            }            Console.ReadLine();        }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.