C#冒泡排序法学习
一,冒泡排序法理解:就是将一个集合里的数据当前位置和后一位比较,然当前位置大于后一位,则两个位置替换,直到排序完成
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#冒泡排序法学习的更多相关文章
- C++学习四 冒泡排序法的一些改进
冒泡排序法需要两次扫描,所以从时间复杂度来说,是O(n2). 如果用图形表示,是这样的: 但是我们可以加以改进. 首先是,如果在排序中间,整个向量已经达到了有序状态,可以直接跳出来. 这样它的复杂度由 ...
- python排序之二冒泡排序法
python排序之二冒泡排序法 如果你理解之前的插入排序法那冒泡排序法就很容易理解,冒泡排序是两个两个以向后位移的方式比较大小在互换的过程好了不多了先上代码吧如下: 首先还是一个无序列表lis,老规矩 ...
- C#数字图像处理算法学习笔记(三)--图像几何变换
C#数字图像处理算法学习笔记(三)--图像几何变换 几何图像处理包括 图像的平移变换,镜像变换,旋转变换,伸缩变换,在这里仅以水平镜像为例,通过代码来理解其基本操作方式: 翻转前:
- C语言 数组输出,冒泡排序法,沉底排序法,二维数组输出,输出字母列长度,从随机数组中找重复数
#include <stdio.h> #define sum 3+4//宏定义是原封不动的使用used for test4 #include <time.h>//used fo ...
- PHP 冒泡排序法
<?php // 冒泡排序法:将一个数组中的值按照从小到大的顺 序排序 $arr = array(33, 1, 4, 5, 2, 3, 7, 9, 8, 99); $len = count($a ...
- 关于Java中的选择排序法和冒泡排序法
一,这种方法是直接传入一个数组进行排序(选择排序法) public static void selectSort(int arr[]){ for (int i = 0; i < arr.leng ...
- java算法之冒泡排序法
由此可见:N个数字要排序完成,总共进行N-1趟排序,每第 i 趟的排序次数为 (N-i) 次,所以 可以用双重循环语句,外层控制循环多少趟,内层控制每一趟的循环次数,即 for(inti=0;i& ...
- PHP冒泡排序法
冒泡排序(Bubble Sort),是一种计算机科学领域的较简单的排序算法.法如其名,就是像冒泡一样,每次从数组当中 冒一个最大的数出来. 冒泡排序它重复地走访过要排序的数列,一次比较两个元素,如果他 ...
- c/c++ 算法之快速排序法 冒泡排序法,选择排序法,插入排序法
本文详细叙述和实现了快速排序算法,冒泡排序 选择排序 插入排序比较简单,原理在这里不再详述,直接用代码进行了实现. 快速排序法(quicksort)是目前所公认最快的排序方法之一(视解题的对象而定), ...
随机推荐
- java String练习题
package java07; /* 题目: 定义一个方法,把数组{1,2,3}按照指定格式拼接成一个字符串,格式参照如下:[word1#word2#word3] 思路: 1.首先准备一个int[]数 ...
- chrome插件研发手册
chrome插件研发手册 一:需求前景 对于研发的小伙伴来说,总会遇到这样的需求,想要通过代码操作已有网站的行为动作,如:自动填充表格内容(表单内容太多,想一键将表单内容填充):自动登录网站(网站登录 ...
- MFC的Dlg和App什么区别?应用程序类与对话框类
MFC里有个app类..他是一个项目工程类,有一个全局的实例化.theApp你可以理解为整个项目的实例,它重载了入口函数,所有的窗口神马的,都是在这个类里实例化的. dlg是对话框,是一个窗口.一个程 ...
- C++链接器
链接器把多个二进制的目标文件(object file)链接成一个单独的可执行文件 在链接过程中,它必须把符号(变量名.函数名等一些列标识符)用对应的数据的内存地址(变量地址.函数地址等)替代,以完成程 ...
- Java中的heap和stack
heap和stack Java的内存分为两类,一类是栈内存,一类是堆内存. 栈内存:是指程序进入一个方法时,会为这个方法单独分配一块私属存储空间,用于存储这个方法内部的局部变量,当这个方法结束时,分配 ...
- NET Core+win10+Jenkins+Gogs+open ssh持续集成
背景 阿里云测试环境一台,带宽1M跟不上,Jenkins安装一个插件耗时很长,于是想在本地搭建Jenkins服务,将生成的安装文件同步到目标服务器上. 技术点有: win10:本地环境是win10,测 ...
- VS2015 Bad Request解决方法
新获取的项目,使用vs2015启动项目,遇到只能用localhost:xxxx的方式访问,使用192.168.**.**:xxxx这样ip+端口的方式无法访问的情况 原因:vs没有做出相应的配置 解决 ...
- 【leetcode】1008. Construct Binary Search Tree from Preorder Traversal
题目如下: Return the root node of a binary search tree that matches the given preorder traversal. (Recal ...
- python多个装饰器
'''在装饰器中加上参数:1.实现在源代码中加上时间统计:函数timmer2.实现用户名认证功能:函数auth23.实现一次认证,刷新后自动登录功能,index函数已经认证并登录,在执行home函数时 ...
- 重磅干货免费下载!阿里云RDS团队论文被数据库顶会SIGMOD 2018收录
ACM SIGMOD数据管理国际会议是由美国计算机协会(ACM) 数据管理专业委员会(SIGMOD)发起.在数据库领域具有最高学术地位的国际性学术会议. SIGMOD和另外两大数据库会议VLDB.IC ...