ããè¦ãã®ã¯ã¤ãã¯ã½ã¼ããæãåºããªããæ¸ãã¦ã¿ã
æ¥ã«æãç«ã£ã¦ãC#ã§ã¯ã¤ãã¯ã½ã¼ããå®è£
ãã¦ã¿ãã
ãã©ã¤
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { var sample = new List<int>() { 3, 12, 4, 0, -3, 2, 8, 1, 1000, -21 }; var result = Program.QuickSort(sample); foreach (var item in result) { Console.Write(item + " "); } Console.ReadKey(); } private static IEnumerable<int> QuickSort(IEnumerable<int> array) { if (array.Count() == 0) { return array; } else { int border = array.First(); var left = new List<int>(); var right = new List<int>(); foreach (var item in array.Skip(1)) { if (item < border) left.Add(item); else right.Add(item); } return QuickSort(left) .Concat(new List<int>() { border }) .Concat(QuickSort(right)); } } } }
LINQã®é¨åã¯ããã£ã¨ã·ã³ãã«ã«æ§è½ã®ãããã®ãæ¸ããããªæ°ããããã¾ããã¨ããããã¯ããã§ã
å¾ã§èª¿ã¹ãã¨ããããããªè¨äºãçºè¦ => ãneue cc - C# LinqでクイックソートããLINQã極ããã¨ããããªã¹ãã¼ãã«æ¸ããã¨ããä¾ã
ã¯ã¤ãã¯ã½ã¼ãã¯æ¸ããã¹ãã
å®çªã®ã¢ã«ã´ãªãºã ãå®è£
ãããã³ã«æããã¨ããããããããæåãªã¢ã«ã´ãªãºã ã¯ãããæ¸ããããã«ã²ã¨éãè¦ãã¦ããã¹ãããããã¨ãããã¡ãã¡è¦ããã¨ã調ã¹ã¦æ¸ããã°ããã§ããããããã«èããã°ããã®ãã
å人çã«ã¯ãæ±ç¨çãªã¢ã«ã´ãªãºã ã®"ãã¶ã¤ã³"ãä½ã«æã¿è¾¼ã¾ãããã¨ãä¸çªå¤§äºã ã¨æã£ã¦ãããä¸è¨ã®ã¯ã¤ãã¯ã½ã¼ãã®ä¾ã§è¨ãã¨ãããã¯å帰ã¢ã«ã´ãªãºã ã®å®çªã®æ¸ãæ¹*1ã«ãªã£ã¦ãããåã
ã®ã¢ã«ã´ãªãºã ãè¦ãã¦ãã¦ãããªãã¦ãããããã®å
±éã®"ãã¶ã¤ã³"ãä½ã§èº«ã«ã¤ãããã¨ã¯ç¡é§ã«ãªããªãã¨æãã
*1:func(x) { if (åæ¢æ¡ä»¶ãæºãã) then return 0; else return x + func (x-1);} ã®ããã«ãå帰çå®ç¾©ããã®ã¾ã¾å®è£ ããæ¸ãæ¹ã