using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Xsl;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List<int> list = new List<int>() { 8, 1, 4, 2, 9, 5, 3 };
Console.WriteLine("\n排序前 => {0}\n", string.Join(",", list));
list = CombSort(list);
Console.WriteLine("\n排序后 => {0}\n", string.Join(",", list));
Console.Read();
}
/// <summary>
/// 梳排序
/// </summary>
/// <param name="list"></param>
/// <returns></returns>
static List<int> CombSort(List<int> list)
{
//获取最佳排序尺寸: 比率为 1.3
var step = (int)Math.Floor(list.Count / 1.3);
while (step >= 1)
{
for (int i = 0; i < list.Count; i++)
{
//如果前者大于后者,则进行交换
if (i + step < list.Count && list[i] > list[i + step])
{
var temp = list[i];
list[i] = list[i + step];
list[i + step] = temp;
}
//如果越界,直接跳出
if (i + step > list.Count)
break;
}
//在当前的step在除1.3
step = (int)Math.Floor(step / 1.3);
}
return list;
}
}
}