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

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);
}
}
}

C#冒泡排序法学习的更多相关文章

  1. C++学习四 冒泡排序法的一些改进

    冒泡排序法需要两次扫描,所以从时间复杂度来说,是O(n2). 如果用图形表示,是这样的: 但是我们可以加以改进. 首先是,如果在排序中间,整个向量已经达到了有序状态,可以直接跳出来. 这样它的复杂度由 ...

  2. python排序之二冒泡排序法

    python排序之二冒泡排序法 如果你理解之前的插入排序法那冒泡排序法就很容易理解,冒泡排序是两个两个以向后位移的方式比较大小在互换的过程好了不多了先上代码吧如下: 首先还是一个无序列表lis,老规矩 ...

  3. C#数字图像处理算法学习笔记(三)--图像几何变换

    C#数字图像处理算法学习笔记(三)--图像几何变换 几何图像处理包括 图像的平移变换,镜像变换,旋转变换,伸缩变换,在这里仅以水平镜像为例,通过代码来理解其基本操作方式: 翻转前:

  4. C语言 数组输出,冒泡排序法,沉底排序法,二维数组输出,输出字母列长度,从随机数组中找重复数

    #include <stdio.h> #define sum 3+4//宏定义是原封不动的使用used for test4 #include <time.h>//used fo ...

  5. PHP 冒泡排序法

    <?php // 冒泡排序法:将一个数组中的值按照从小到大的顺 序排序 $arr = array(33, 1, 4, 5, 2, 3, 7, 9, 8, 99); $len = count($a ...

  6. 关于Java中的选择排序法和冒泡排序法

    一,这种方法是直接传入一个数组进行排序(选择排序法) public static void selectSort(int arr[]){ for (int i = 0; i < arr.leng ...

  7. java算法之冒泡排序法

    由此可见:N个数字要排序完成,总共进行N-1趟排序,每第 i 趟的排序次数为 (N-i) 次,所以 可以用双重循环语句,外层控制循环多少趟,内层控制每一趟的循环次数,即   for(inti=0;i& ...

  8. PHP冒泡排序法

    冒泡排序(Bubble Sort),是一种计算机科学领域的较简单的排序算法.法如其名,就是像冒泡一样,每次从数组当中 冒一个最大的数出来. 冒泡排序它重复地走访过要排序的数列,一次比较两个元素,如果他 ...

  9. c/c++ 算法之快速排序法 冒泡排序法,选择排序法,插入排序法

    本文详细叙述和实现了快速排序算法,冒泡排序 选择排序 插入排序比较简单,原理在这里不再详述,直接用代码进行了实现. 快速排序法(quicksort)是目前所公认最快的排序方法之一(视解题的对象而定), ...

随机推荐

  1. 【记录】logstash 的filter 使用

    概述 logstash 之所以强大和流行,与其丰富的过滤器插件是分不开的 过滤器提供的并不单单是过滤的功能,还可以对进入过滤器的原始数据进行复杂的逻辑处理,甚至添加独特的新事件到后续流程中 强大的文本 ...

  2. 02.LNMP架构-MySQL源码包编译部署详细步骤

    操作系统:CentOS_Server_7.5_x64_1804.iso 部署组件:Cmake+Boost+MySQL 操作步骤: 一.安装依赖组件 [root@localhost ~]# yum -y ...

  3. 一、RabbitMQ安装与测试连接

    一.下载NuGet支持的RabbitMQ.Client客户端库与安装RabbitMQ服务. 1.安装客户端库操作服务. 2.安装服务. 步骤一.下载Erlang. 步骤二.下载RabbitMQ服务 采 ...

  4. qq新闻:网络安全

    1.立足信息安全良好赛道,并在其他行业拥有强劲增长点的信息安全公司. 深信服:公司多款安全产品市占率领先,其中VPN产品连续七年市占率第一,VPN应用十分广泛,细分市场规模较大,预计2018年防火墙/ ...

  5. 【LeetCode】拓扑排序 topological-sort(共5题)

    [207]Course Schedule [210]Course Schedule II [269]Alien Dictionary [329]Longest Increasing Path in a ...

  6. HTML5:新元素来实现一下网页布局

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  7. React Native框架如何白盒测试-HIPPY接口测试架构篇

    本文转载自腾讯TMQ团队 ,侵权删. 1.开天辟地 Hippy是什么呢?简单点,能用JavaScript来写Android和iOS应用的框架, 类似业界的React Native. 好吧,我们还是严谨 ...

  8. MFC程序执行过程剖析(转)

    一 MFC程序执行过程剖析 1)我们知道在WIN32API程序当中,程序的入口为WinMain函数,在这个函数当中我们完成注册窗口类,创建窗口,进入消息循环,最后由操作系统根据发送到程序窗口的消息调用 ...

  9. Shell输入命令时一些有用的快捷键

    Ctrl + u: 从光标所在位置一直删除到开头 Ctrl + k: 从光标所在位置一直删除到尾 Ctrl + b: 光标向后移动一个字符 Ctrl + f: 光标后前移动一个字符 Alt + b: ...

  10. 51nod 1298:圆与三角形(计算几何)

    题目链接 判断圆和三角形是否相交   可以转化为   判断三条线段是否和圆相交 #include<iostream> #include<cstdio> #include< ...