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

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. elasticsearch 基础 —— URI搜索

    URI搜索 可以通过提供请求参数使用URI来执行搜索请求.使用此模式执行搜索时,并非所有搜索选项都会暴露.这是一个例子: GET twitter/_search?q=user:kimchy 示例响应: ...

  2. linux ---maven的安装和配置

    linux下的maven的安装和配置:本人使用的是apache-maven-3.3.9-bin.tar.gz------安装maven的前提是JDK安装成功:java -version 测试一下--J ...

  3. PF_INET 与驱动

    https://blog.csdn.net/trustnature/article/details/7849562 ? ? ?

  4. 机器学习——k-近邻(K-Nearest Neighbor)

    目录 K-Nearest neighbor K-近邻分类算法 从文本文件中解析和导入数据 使用python创建扩散图 归一化数值 K-Nearest neighbor (个人观点,仅供参考.) k-近 ...

  5. iView的Message提示框

    全局配置message main.js Vue.prototype.$Message.config({ top: 70, duration:3 }); Vue.prototype.$Message.c ...

  6. 【串线篇】Mybatis缓存之整合第三方缓存

    为什么要用第三方缓存?因为mybatis的缓存机制说白了就是一个map,不够强大.但幸好mybatis有自知之明将其Cache做成了一个接口开放出来,我们可以实现这个接口用第三方专业的缓存框架去自定义 ...

  7. C++ 从txt文本中读取map

    由于存入文本文件的内容都为文本格式,所以在读取内容时需要将文本格式的内容遍历到map内存中,因此在读取时需要将文本进行切分(切分成key和value) 环境gcc #include<iostre ...

  8. Vue中 let 关键字

    let es6新增了let命令,用来声明变量.它的用法类似于var,但是所声明的变量,只在let命令所在的代码块内有效. 不存在变量提升 var命令会发生”变量提升“现象,即变量可以在声明之前使用,值 ...

  9. 英语单词Permissive

    Permissive 来源 [root@centos7 ~]# setenforce usage: setenforce [ Enforcing | Permissive | | ] 翻译 adj. ...

  10. FMXUI TEXTVIEW代码设置IMAGEINDEX

    FMXUI作为一个开源的控件,真是DELPHIER的福音,向作者致敬.​TEXTVIEW非常好用,在属性面板中有ImageIndex属性,可以方便设置图标,在实际应用中图标状态需要改变,但在代码设置时 ...