一,冒泡排序法理解:就是将一个集合里的数据当前位置和后一位比较,然当前位置大于后一位,则两个位置替换,直到排序完成

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace MaoPao
{
class Program
{
static void Sort(int[] sArray)
{
bool sw = true;
do
{
sw = false;
for (int i = ; i < sArray.Length - ; i++)
{
if (sArray[i] > sArray[i + ])
{
int temp = sArray[i];
sArray[i] = sArray[i + ];
sArray[i + ] = temp;
sw = true;
}
}
} while (sw);
} static void Main(string[] args)
{
int[] sArray = new int[] { , , , , , , , };
Sort(sArray);
foreach (var temp in sArray)
{
Console.Write(temp + " ");
}
Console.ReadKey(); }
}
}

二,冒泡排序拓展

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace MaoPao
{
class Program
{
static void CommonSort<T>(T[] sArray, Func<T, T, bool> compareMethod)
{
bool sw = true;
do
{
sw = false;
for (int i = ; i < sArray.Length - ; i++)
{
if (compareMethod(sArray[i], sArray[i + ]))
{
T temp = sArray[i];
sArray[i] = sArray[i + ];
sArray[i + ] = temp;
sw = true;
}
}
} while (sw);
}
static void Main(string[] args)
{ Employee[] employees = new Employee[]
{
new Employee("张三",),
new Employee("李四",),
new Employee("陈五",),
new Employee("李六",),
new Employee("王七",)
};
CommonSort<Employee>(employees, Employee.Compare);
foreach (Employee em in employees)
{
Console.WriteLine(em);
}
Console.ReadKey();
}
}
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace MaoPao
{
class Employee
{
public string Name { get; private set; }
public int Comp { get; private set; } public Employee(string name, int comp)
{
this.Name = name;
this.Comp = comp;
}
//如果e1大于e2的话,返回true,否则返回false
public static bool Compare(Employee e1, Employee e2)
{
if (e1.Comp > e2.Comp) return true;
return false;
} public override string ToString()
{
return Name + ":" + Comp;
}
}
}

三,泛型的冒泡排序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections; namespace Maopao
{
class Program
{
static void Main(string[] args)
{
People[] pl = new People[]{
new People(, "小明3"),
new People(, "小明1"),
new People(, "小明2"),
new People(, "小明5"),
new People(, "小明4"),
new People(, "小明9")
}; Sort s = new Sort();
int[] t = { , , , , , , };
int[] t1 = s.CompareTo(t);
People[] t3 = s.CompareSort<People>(pl);
for (int i = ; i < t1.Length; i++)
{
Console.WriteLine(t1[i].ToString());
} //用泛型实现冒泡程序
for (int i = ; i < t3.Length; i++)
{
Console.WriteLine(t3[i].Name);
}
Console.ReadKey();
}
}
public class Sort
{
public int[] CompareTo(int[] a)
{
int temp;
for (int i = ; i < a.Length - ; i++)
{
for (int j = i + ; j < a.Length; j++)
{
if (a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
return a;
} public T[] CompareSort<T>(T[] t) where T : IComparable
{
T temp;
for (int i = ; i < t.Length - ; i++)
{
for (int j = i + ; j < t.Length; j++)
{
if (t[i].CompareTo(t[j]) > )
{
temp = t[i];
t[i] = t[j];
t[j] = temp;
}
}
}
return t;
}
}
public class People : IComparable
{
public People(int id, string name)
{
this.Id = id;
this.Name = name;
} public int Id
{
set;
get;
}
public string Name
{
get;
set;
}
public int CompareTo(object obj)
{
People p = (People)obj;
return this.Id.CompareTo(p.Id);
}
}
}

最新文章

  1. Eclipse安装Spring-tool-suite
  2. 非对称加密算法--DH
  3. Python用法摘要 BY 四喜三顺
  4. [转]怎么在MVC中使用自定义Membership
  5. GO语言练习:组合的用法
  6. java转换unicode,筛选文件中的insert语句并把日期给转换为可以直接在数据库执行的语句
  7. html标签属性
  8. HDU 1217 Arbitrage (Floyd)
  9. redis资料汇总
  10. jstack:将Process Explorer中看到的进程ID做16进制转换,到ThreadDump中加上0x 前缀即能找到对应线程(转)
  11. ORACLE PL/SQL编程之六:把过程与函数说透(穷追猛打,把根儿都拔起!)
  12. [转]Python跳过第一行读取文件内容
  13. js中style的属性
  14. 小试牛刀JavaScript鼠标事件
  15. Linux - 简明Shell编程12 - 定制输出(ColorOutput)
  16. 201521123072《Java程序设计》第6周学习总结
  17. PXE安装windows系统,pxe-e55:ProxyDhcp service did not reply to request on port 4011
  18. django 1.开发接口环境搭建
  19. Django--cookie(登录用)
  20. 设计模式,Let&#39;s “Go”! (上)

热门文章

  1. Beta阶段成果展示——第八组
  2. python中字典类型的使用
  3. vue,一路走来(4)--vuex
  4. find命令使用详解
  5. Sass--混合宏 VS 继承 VS 占位符
  6. 从零开始之uboot、移植uboot2017.01(一、移植前的准备)
  7. 人生苦短_我用Python_dict(字典)_003
  8. boost circularBuffer
  9. LDD快速参考
  10. Invalid bound statement (not found)错误