C# 冒泡排序法示例代码(包含泛型方法)
2007-05-31 14:55
351 查看
using System;
using System.Collections.Generic;
using System.Text;
namespace bleb_sort
{
class Program
{
public class SwapObj
{
public static void swap<T>(ref T a, ref T b)
{
T c;
c=a;
a=b;
b=c;
}
}
static void Main(string[] args)
{
//冒泡排序:
int[] num = new int[] { 12, 3, 6, 16, 19, 7, 33, 21, 20, 100, 100, 115, 55, 58, 77};
foreach (int i in num)
{
Console.Write(i.ToString() + ", ");
}
Console.WriteLine();
Console.WriteLine("排序后:");
int counter1 = 0;
int counter2 = 0;
for(int i=0; i<num.Length-1;i++)
{
for(int j=i+1; j<num.Length; j++)
{
if(num[i]<num[j])
{
SwapObj.swap<int>(ref num[i], ref num[j]);
counter1++;
}
counter2++;
}
}
Console.WriteLine( "循环了{0}次。", counter2);
Console.WriteLine("其中交换了{0}次。", counter1);
foreach (int i in num)
{
Console.Write(i.ToString() + ", ");
}
Console.ReadLine();
}
}
}
电脑学习网 - http://www.why100000.com
张庆, 2007.5.31
using System.Collections.Generic;
using System.Text;
namespace bleb_sort
{
class Program
{
public class SwapObj
{
public static void swap<T>(ref T a, ref T b)
{
T c;
c=a;
a=b;
b=c;
}
}
static void Main(string[] args)
{
//冒泡排序:
int[] num = new int[] { 12, 3, 6, 16, 19, 7, 33, 21, 20, 100, 100, 115, 55, 58, 77};
foreach (int i in num)
{
Console.Write(i.ToString() + ", ");
}
Console.WriteLine();
Console.WriteLine("排序后:");
int counter1 = 0;
int counter2 = 0;
for(int i=0; i<num.Length-1;i++)
{
for(int j=i+1; j<num.Length; j++)
{
if(num[i]<num[j])
{
SwapObj.swap<int>(ref num[i], ref num[j]);
counter1++;
}
counter2++;
}
}
Console.WriteLine( "循环了{0}次。", counter2);
Console.WriteLine("其中交换了{0}次。", counter1);
foreach (int i in num)
{
Console.Write(i.ToString() + ", ");
}
Console.ReadLine();
}
}
}
电脑学习网 - http://www.why100000.com
张庆, 2007.5.31
相关文章推荐
- C# 冒泡排序法示例代码(包含泛型方法)
- C#中关于泛型方法和泛型委托的示例代码
- C#判断程序是否是管理员权限运行的方法代码示例
- 使用C#发送带附件的电子邮件的方法的代码示例分析
- .NET/C#利用反射调用含ref或out参数的方法示例代码
- C#.NET示例读写xml所有节点的代码实现方法和读取xml节点的数据总结
- C#访问及调用类中私有成员与方法示例代码
- 编写高质量代码改善C#程序的157个建议[为泛型指定初始值、使用委托声明、使用Lambda替代方法和匿名方法]
- 编写高质量代码改善C#程序的157个建议[为泛型指定初始值、使用委托声明、使用Lambda替代方法和匿名方法]
- JAVA通过JNI调用C#dll方法说明(包含示例)
- C# 代码示例详尽剖析异步调用的两种方法
- php示例代码之类似于C#中的String.Format方法
- C#使用互斥量(Mutex)实现多进程并发操作时多进程间线程同步操作(进程同步)的简单示例代码及使用方法
- php示例代码之类似于C#中的String.Format方法
- C#.NET示例读写xml所有节点的代码实现方法和读取xml节点的数据总结
- C# 写的一个泛型类和泛型方法示例
- C#.NET示例读写xml所有节点的代码实现方法和读取xml节点的数据总结
- c#获取字符串宽度的示例代码(字节数方法)
- [导入]Asp.net 2.0 用C# 创建 PDF文件[引用] (示例代码下载)
- xp_cmdshell 存储过程应用示例(C#),包含 DataReader 转换到 DataSet 过程