【C#】1.演算法溫故而知新 - 簡單的桶排序

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   ar   for   2014   div   

缺點:

1.不適用於小數

2.當數值過多,太浪費空間,比如數值範圍為0~99999,那需申請100000個變數,也就是要寫成a[1000000]。

 

 

代碼如下:

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication1{    public class Program    {        public static void Main(string[] args)        {            int[] nums = new int[9] { 2, 1, 9, 2, 7, 3, 1, 8, 2 };//初始化一個數組,其中有9個數,每個數都不大於10,這裡假定是我們輸入的數,需要從小到大排序            int[] a = new int[11];//因為每個數都不大於10,所以初始化一個包含11個數的數組a            int i, j, t;            for (i = 0; i <= 10; i++) a[i] = 0;//給a數組賦值都為0            for (i = 0; i < nums.Length; i++)            {                t = nums[i];//擷取當前的數                a[t]++;//進行計數            }            for (i = 0; i <= 10; i++)//依次判斷a[0]~a[10]                for (j = 1; j <=a[i]; j++)//出現了幾次就輸出幾次                    Console.Write("  " + i);        }    }}

 

【C#】1.演算法溫故而知新 - 簡單的桶排序

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.