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

推荐订阅源

Martin Fowler
Martin Fowler
Jina AI
Jina AI
J
Java Code Geeks
Microsoft Security Blog
Microsoft Security Blog
Recent Announcements
Recent Announcements
I
InfoQ
L
LangChain Blog
The Cloudflare Blog
IT之家
IT之家
博客园 - 叶小钗
Apple Machine Learning Research
Apple Machine Learning Research
B
Blog
A
About on SuperTechFans
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Last Week in AI
Last Week in AI
Blog — PlanetScale
Blog — PlanetScale
罗磊的独立博客
云风的 BLOG
云风的 BLOG
Microsoft Azure Blog
Microsoft Azure Blog
Engineering at Meta
Engineering at Meta
F
Fortinet All Blogs
博客园 - 聂微东
美团技术团队
博客园_首页

博客园 - 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

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