惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Microsoft Azure Blog
Microsoft Azure Blog
WordPress大学
WordPress大学
Google DeepMind News
Google DeepMind News
美团技术团队
大猫的无限游戏
大猫的无限游戏
H
Help Net Security
小众软件
小众软件
aimingoo的专栏
aimingoo的专栏
博客园 - 聂微东
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
M
MIT News - Artificial intelligence
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - Franky
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Microsoft Security Blog
Microsoft Security Blog
F
Fortinet All Blogs
A
About on SuperTechFans
Recent Announcements
Recent Announcements
D
Docker
Vercel News
Vercel News
Engineering at Meta
Engineering at Meta
腾讯CDC
Martin Fowler
Martin Fowler
阮一峰的网络日志
阮一峰的网络日志

博客园 - smallnest

希尔排序 插入排序 Gnome sort 鸡尾酒排序 奇偶排序 快速排序 冒泡排序 algorithm in c# 开发人员最喜爱的十大免费的Visual Studio插件(下) 开发人员最喜爱的十大免费的Visual Studio插件(上) 一种获取重载泛型方法的方式 [游戏]五子连珠 发布一个记账软件---流水记账 轻松编写您自己的拖拉机算法,进行算法大战 拖拉机大战更新了 拖拉机大战1.1.0.320发布,更多新功能 拖拉机大战新春贺岁版发布 翻译助手0.1 visual studio 2005 常用类型的图标
Comb排序
smallnest · 2009-12-19 · via 博客园 - smallnest

类别:排序-交换排序
参看 维基百科的定义

1 using System;
2  using System.Collections.Generic;
3
4 namespace Com.Colobu.Algorithm.Exchange
5 {
6 /// <summary>
7 /// <b>Comb sort</b> improves on bubble sort, and rivals algorithms like Quicksort.
8 /// The basic idea is to eliminate turtles, or small values near the end of the list,
9 /// since in a bubble sort these slow the sorting down tremendously.
10 ///
11 /// 平均时间复杂度:O(nlogn)
12 /// Stability:No
13 /// </summary>
14 public class CombSortAlgorithm
15 {
16 public static void CombSort<T>(IList<T> szArray) where T : IComparable
17 {
18 int gap = szArray.Count;
19 bool swapped = true;
20
21 while (gap > 1 || swapped)
22 {
23 if (gap > 1)
24 {
25 gap = (int)(gap / 1.25);
26 }
27
28 int i = 0;
29 swapped = false;
30 while (i + gap < szArray.Count)
31 {
32 if (szArray[i].CompareTo(szArray[i + gap]) > 0)
33 {
34 T t = szArray[i];
35 szArray[i] = szArray[i + gap];
36 szArray[i + gap] = t;
37 swapped = true;
38 }
39 i++;
40 }
41 }
42 }
43 }
44 }
45