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

推荐订阅源

aimingoo的专栏
aimingoo的专栏
B
Blog RSS Feed
Recent Announcements
Recent Announcements
Vercel News
Vercel News
M
MIT News - Artificial intelligence
阮一峰的网络日志
阮一峰的网络日志
L
LangChain Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Microsoft Security Blog
Microsoft Security Blog
H
Help Net Security
T
The Blog of Author Tim Ferriss
Y
Y Combinator Blog
G
Google Developers Blog
罗磊的独立博客
爱范儿
爱范儿
宝玉的分享
宝玉的分享
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园_首页
S
SegmentFault 最新的问题
WordPress大学
WordPress大学
月光博客
月光博客
人人都是产品经理
人人都是产品经理
Apple Machine Learning Research
Apple Machine Learning Research

博客园 - smallnest

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

1 using System;
2  using System.Collections.Generic;
3
4
5  namespace Com.Colobu.Algorithm.Exchange
6 {
7 /// <summary>
8 /// <b>奇偶排序</b>的思路是在数组中重复两趟扫描。
9 /// 第一趟扫描选择所有的数据项对,a[j]和a[j+1],j是奇数(j=1, 3, 5……)。
10 /// 如果它们的关键字的值次序颠倒,就交换它们。
11 /// 第二趟扫描对所有的偶数数据项进行同样的操作(j=2, 4,6……)。
12 /// 重复进行这样两趟的排序直到数组全部有序。
13 ///
14 /// 平均时间复杂度:O(n^2)
15 /// Stability:Yes
16 /// </summary>
17 public class OddEvenSortAlgorithm
18 {
19 public static void OddEvenSort<T>(IList<T> szArray) where T : IComparable
20 {
21 bool sorted = false;
22 while (!sorted)
23 {
24 sorted = true;
25 // odd-even
26 for (int i = 1; i < szArray.Count - 1; i += 2)
27 {
28 if (szArray[i].CompareTo(szArray[i + 1]) > 0)
29 {
30 Swap(szArray, i, i + 1);
31 sorted = false;
32 }
33 }
34 // even-odd
35 for (int j = 0; j < szArray.Count - 1; j += 2)
36 {
37 if (szArray[j].CompareTo(szArray[j + 1]) > 0)
38 {
39 Swap(szArray, j, j + 1);
40 sorted = false;
41 }
42 }
43 }
44 }
45 private static void Swap<T>(IList<T> szArray, int i, int j)
46 {
47 T tmp = szArray[i];
48 szArray[i] = szArray[j];
49 szArray[j] = tmp;
50 }
51 }
52 }
53

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